Socket
Socket
Sign inDemoInstall

bole

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bole - npm Package Compare versions

Comparing version 3.0.2 to 4.0.0

.travis.yml

197

bole.js

@@ -1,12 +0,14 @@

var _stringify = require('fast-safe-stringify')
, individual = require('individual')('$$bole', { fastTime: false }) // singleton
, format = require('./format')
, levels = 'debug info warn error'.split(' ')
, hostname = require('os').hostname()
, hostnameSt = _stringify(hostname)
, pid = process.pid
, hasObjMode = false
, scache = []
'use strict'
levels.forEach(function (level) {
const _stringify = require('fast-safe-stringify')
const individual = require('individual')('$$bole', { fastTime: false }) // singleton
const format = require('./format')
const levels = 'debug info warn error'.split(' ')
const hostname = require('os').hostname()
const hostnameSt = _stringify(hostname)
const pid = process.pid
let hasObjMode = false
const scache = []
for (const level of levels) {
// prepare a common part of the stringified output

@@ -16,13 +18,14 @@ scache[level] = ',"hostname":' + hostnameSt + ',"pid":' + pid + ',"level":"' + level

if (!Array.isArray(individual[level]))
if (!Array.isArray(individual[level])) {
individual[level] = []
})
}
}
function stackToString (e) {
var s = e.stack
, ce
let s = e.stack
let ce
if (typeof e.cause === 'function' && (ce = e.cause()))
if (typeof e.cause === 'function' && (ce = e.cause())) {
s += '\nCaused by: ' + stackToString(ce)
}

@@ -32,34 +35,29 @@ return s

function errorToOut (err, out) {
out.err = {
name : err.name
, message : err.message
, code : err.code // perhaps
, stack : stackToString(err)
name: err.name,
message: err.message,
code: err.code, // perhaps
stack: stackToString(err)
}
}
function requestToOut (req, out) {
out.req = {
method : req.method
, url : req.url
, headers : req.headers
, remoteAddress : req.connection.remoteAddress
, remotePort : req.connection.remotePort
method: req.method,
url: req.url,
headers: req.headers,
remoteAddress: req.connection.remoteAddress,
remotePort: req.connection.remotePort
}
}
function objectToOut (obj, out) {
var k
for (k in obj) {
if (Object.prototype.hasOwnProperty.call(obj, k))
for (const k in obj) {
if (Object.prototype.hasOwnProperty.call(obj, k) && obj[k] !== undefined) {
out[k] = obj[k]
}
}
}
function objectMode (stream) {

@@ -69,14 +67,13 @@ return stream._writableState && stream._writableState.objectMode === true

function stringify (level, name, message, obj) {
var k
, s = '{"time":'
+ (individual.fastTime ? Date.now() : ('"' + new Date().toISOString() + '"'))
+ scache[level]
+ '","name":'
+ name
+ (message !== undefined ? (',"message":' + _stringify(message)) : '')
let s = '{"time":' +
(individual.fastTime ? Date.now() : ('"' + new Date().toISOString() + '"')) +
scache[level] +
'","name":' +
name +
(message !== undefined ? (',"message":' + _stringify(message)) : '')
for (k in obj)
for (const k in obj) {
s += ',' + _stringify(k) + ':' + _stringify(obj[k])
}

@@ -90,18 +87,18 @@ s += '}'

function extend (level, name, message, obj) {
var k
, newObj = {
time : individual.fastTime ? Date.now() : new Date().toISOString()
, hostname : hostname
, pid : pid
, level : level
, name : name
}
const newObj = {
time: individual.fastTime ? Date.now() : new Date().toISOString(),
hostname: hostname,
pid: pid,
level: level,
name: name
}
if (message !== undefined)
if (message !== undefined) {
obj.message = message
}
for (k in obj)
for (const k in obj) {
newObj[k] = obj[k]
}

@@ -111,38 +108,48 @@ return newObj

function levelLogger (level, name) {
var outputs = individual[level]
, nameSt = _stringify(name)
const outputs = individual[level]
const nameSt = _stringify(name)
return function namedLevelLogger (inp, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) {
if (outputs.length === 0)
if (outputs.length === 0) {
return
}
var out = {}
, objectOut
, i = 0
, l = outputs.length
, stringified
, message
const out = {}
let objectOut
let i = 0
const l = outputs.length
let stringified
let message
if (typeof inp === 'string' || inp == null) {
if (!(message = format(inp, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16)))
if (!(message = format(inp, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16))) {
message = undefined
}
} else {
if (!(message = format(a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16)))
message = undefined
if (typeof inp === 'boolean')
message = String(inp)
else if (inp instanceof Error) {
errorToOut(inp, out)
} else if (typeof inp === 'object') {
if (inp.method && inp.url && inp.headers && inp.socket)
requestToOut(inp, out)
else
objectToOut(inp, out)
if (inp instanceof Error) {
if (typeof a2 === 'object') {
objectToOut(a2, out)
errorToOut(inp, out)
if (!(message = format(a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16))) {
message = undefined
}
} else {
errorToOut(inp, out)
if (!(message = format(a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16))) {
message = undefined
}
}
} else {
if (!(message = format(a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16))) {
message = undefined
}
}
if (typeof inp === 'boolean') { message = String(inp) } else if (typeof inp === 'object' && !(inp instanceof Error)) {
if (inp.method && inp.url && inp.headers && inp.socket) { requestToOut(inp, out) } else { objectToOut(inp, out) }
}
}
if (l === 1 && !hasObjMode) { // fast, standard case
outputs[0].write(new Buffer(stringify(level, nameSt, message, out) + '\n'))
outputs[0].write(Buffer.from(stringify(level, nameSt, message, out) + '\n'))
return

@@ -153,8 +160,10 @@ }

if (objectMode(outputs[i])) {
if (objectOut === undefined) // lazy object completion
if (objectOut === undefined) { // lazy object completion
objectOut = extend(level, name, message, out)
}
outputs[i].write(objectOut)
} else {
if (stringified === undefined) // lazy stringify
stringified = new Buffer(stringify(level, nameSt, message, out) + '\n')
if (stringified === undefined) { // lazy stringify
stringified = Buffer.from(stringify(level, nameSt, message, out) + '\n')
}
outputs[i].write(stringified)

@@ -166,3 +175,2 @@ }

function bole (name) {

@@ -181,5 +189,4 @@ function boleLogger (subname) {

bole.output = function output (opt) {
var i = 0, b
let b = false

@@ -191,13 +198,16 @@ if (Array.isArray(opt)) {

if (typeof opt.level !== 'string')
if (typeof opt.level !== 'string') {
throw new TypeError('Must provide a "level" option')
}
for (; i < levels.length; i++) {
if (!b && levels[i] === opt.level)
for (const level of levels) {
if (!b && level === opt.level) {
b = true
}
if (b) {
if (opt.stream && objectMode(opt.stream))
if (opt.stream && objectMode(opt.stream)) {
hasObjMode = true
individual[levels[i]].push(opt.stream)
}
individual[level].push(opt.stream)
}

@@ -209,7 +219,6 @@ }

bole.reset = function reset () {
levels.forEach(function (level) {
for (const level of levels) {
individual[level].splice(0, individual[level].length)
})
}
individual.fastTime = false

@@ -219,12 +228,12 @@ return bole

bole.setFastTime = function setFastTime (b) {
if (!arguments.length)
if (!arguments.length) {
individual.fastTime = true
else
} else {
individual.fastTime = b
}
return bole
}
module.exports = bole

@@ -5,34 +5,48 @@ // consider this a warning about getting obsessive about optimization

function format (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) {
if (a16 !== undefined)
if (a16 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16)
if (a15 !== undefined)
}
if (a15 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)
if (a14 !== undefined)
}
if (a14 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)
if (a13 !== undefined)
}
if (a13 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13)
if (a12 !== undefined)
}
if (a12 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
if (a11 !== undefined)
}
if (a11 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)
if (a10 !== undefined)
}
if (a10 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
if (a9 !== undefined)
}
if (a9 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (a8 !== undefined)
}
if (a8 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7, a8)
if (a7 !== undefined)
}
if (a7 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6, a7)
if (a6 !== undefined)
}
if (a6 !== undefined) {
return utilformat(a1, a2, a3, a4, a5, a6)
if (a5 !== undefined)
}
if (a5 !== undefined) {
return utilformat(a1, a2, a3, a4, a5)
if (a4 !== undefined)
}
if (a4 !== undefined) {
return utilformat(a1, a2, a3, a4)
if (a3 !== undefined)
}
if (a3 !== undefined) {
return utilformat(a1, a2, a3)
if (a2 !== undefined)
}
if (a2 !== undefined) {
return utilformat(a1, a2)
}
return a1

@@ -39,0 +53,0 @@ }

{
"name": "bole",
"version": "3.0.2",
"version": "4.0.0",
"description": "A tiny JSON logger",
"main": "bole.js",
"scripts": {
"test": "node test.js"
"lint": "standard",
"test": "npm run lint && node test.js"
},

@@ -20,11 +21,12 @@ "keywords": [

"dependencies": {
"fast-safe-stringify": "~1.1.0",
"individual": "~3.0.0"
"fast-safe-stringify": "^2.0.7",
"individual": "^3.0.0"
},
"devDependencies": {
"bl": "~1.1.2",
"hyperquest": "~1.3.0",
"list-stream": "~1.0.1",
"tape": "~4.5.1"
"bl": "^4.0.0",
"hyperquest": "^2.1.3",
"list-stream": "^2.0.0",
"standard": "^14.3.1",
"tape": "^4.11.0"
}
}

@@ -5,4 +5,6 @@ # bole

[![NPM](https://nodei.co/npm/bole.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/bole/)
[![Travis Status](https://api.travis-ci.com/rvagg/bole.svg?branch=master)](https://travis-ci.com/rvagg/bole)
[![NPM](https://nodei.co/npm/bole.svg)](https://nodei.co/npm/bole/)
Log JSON from within Node.js applications. The log format is obviously inspired by the excellent [Bunyan](https://github.com/trentm/node-bunyan) and is likely to be output-compatible in most cases. The difference is that **bole** aims for even more simplicity, supporting only the common-case basics.

@@ -16,5 +18,5 @@

```js
var log = require('bole')('mymodule')
const log = require('bole')('mymodule')
module.exports.derp = function derp() {
module.exports.derp = () => {
log.debug('W00t!')

@@ -27,4 +29,4 @@ log.info('Starting mymodule#derp()')

```js
var bole = require('bole')
var mod = require('./mymodule')
const bole = require('bole')
const mod = require('./mymodule')

@@ -47,3 +49,3 @@ bole.output({

* Very fast, bole has been optimised for common cases and is designed to add minimal overhead to your applications, use the `fastTime` feature (below) to make it even faster
* Arbitrary log **names**, create a logger by calling `var log = bole('logname')` and `'logname'` will be attached to the output
* Arbitrary log **names**, create a logger by calling `const log = bole('logname')` and `'logname'` will be attached to the output
* Loggers have 4 levels / methods: `log.debug()`, `log.info()`, `log.warn()`, `log.error()`

@@ -87,5 +89,5 @@ * Log methods accept `console.log()` style strfmt output ( using`util.format()`): `log.warn('foo %s', 'bar')`

```js
var log = bole('server')
const log = bole('server')
http.createServer(function (req, res) {
http.createServer((req, res) => {
req.log = log(uuid.v4()) // make a new sub-logger

@@ -92,0 +94,0 @@ req.log.info(req)

@@ -1,24 +0,21 @@

var http = require('http')
, hreq = require('hyperquest')
, test = require('tape')
, bl = require('bl')
, listStream = require('list-stream')
, bole = require('./')
, pid = process.pid
, host = require('os').hostname()
const http = require('http')
const hreq = require('hyperquest')
const test = require('tape')
const bl = require('bl')
const listStream = require('list-stream')
const bole = require('./')
const pid = process.pid
const hostname = require('os').hostname()
function mklogobj (name, level, inp, fastTime) {
var out = {
time : fastTime ? Date.now() : new Date().toISOString()
, hostname : host
, pid : pid
, level : level
, name : name
}
, k
const out = {
time: fastTime ? Date.now() : new Date().toISOString(),
hostname: hostname,
pid: pid,
level: level,
name: name
}
for (k in inp) {
if (Object.prototype.hasOwnProperty.call(inp, k))
out[k] = inp[k]
for (const k in inp) {
if (Object.prototype.hasOwnProperty.call(inp, k)) { out[k] = inp[k] }
}

@@ -29,3 +26,2 @@

// take a log string and zero out the millisecond field

@@ -35,54 +31,59 @@ // to make comparison a little safer (not *entirely* safe)

return str.replace(/("time":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.)\d{3}Z"/g, '$1xxxZ')
.replace(/("remoteAddress":")(?:::ffff:)?(127.0.0.1")/g, '$1$2')
.replace(/("host":")(?:(?:localhost)|(?:::))(:\d+")/g, '$1$2')
.replace(/("remoteAddress":")(?:::ffff:)?(127.0.0.1")/g, '$1$2')
.replace(/("host":")(?:(?:localhost)|(?:::))(:\d+")/g, '$1$2')
}
// just the time value part of the above
function safeTime (time) {
return time.replace(/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.)\d{3}Z$/, '$1xxxZ')
}
test('test simple logging', function (t) {
function prepareExpected (expected) {
return (Array.isArray(expected) ? expected : [expected]).reduce((p, c) => {
return `${p}${JSON.stringify(c)}\n`
}, '')
}
test('test simple logging', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log = bole('simple')
, expected = []
const sink = bl()
const log = bole('simple')
const expected = []
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})
expected.push(mklogobj('simple', 'debug', { aDebug : 'object' }))
expected.push(mklogobj('simple', 'debug', { aDebug: 'object' }))
log.debug({ aDebug: 'object' })
expected.push(mklogobj('simple', 'info', { anInfo : 'object' }))
expected.push(mklogobj('simple', 'info', { anInfo: 'object' }))
log.info({ anInfo: 'object' })
expected.push(mklogobj('simple', 'warn', { aWarn : 'object' }))
expected.push(mklogobj('simple', 'warn', { aWarn: 'object' }))
log.warn({ aWarn: 'object' })
expected.push(mklogobj('simple', 'error', { anError : 'object' }))
expected.push(mklogobj('simple', 'error', { anError: 'object' }))
log.error({ anError: 'object' })
sink.end(function () {
var exp = expected.reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(sink.slice().toString()), safe(exp))
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})
})
test('test complex object logging', function (t) {
test('test complex object logging', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log = bole('simple')
, expected = []
, cplx = {
aDebug : 'object'
, deep : { deeper: { deeperStill: { tooDeep: 'whoa' }, arr: [ 1, 2, 3, { eh: 'wut?' } ] } }
}
const sink = bl()
const log = bole('simple')
const expected = []
const cplx = {
aDebug: 'object',
deep: { deeper: { deeperStill: { tooDeep: 'whoa' }, arr: [1, 2, 3, { eh: 'wut?' }] } }
}
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})

@@ -93,61 +94,50 @@

sink.end(function () {
var exp = expected.reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(sink.slice().toString()), safe(exp))
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})
})
test('test multiple logs', function (t) {
test('test multiple logs', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log1 = bole('simple1')
, log2 = bole('simple2')
, expected = []
const sink = bl()
const log1 = bole('simple1')
const log2 = bole('simple2')
const expected = []
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})
expected.push(mklogobj('simple1', 'debug', { aDebug : 'object' }))
expected.push(mklogobj('simple1', 'debug', { aDebug: 'object' }))
log1.debug({ aDebug: 'object' })
expected.push(mklogobj('simple1', 'info', { anInfo : 'object' }))
expected.push(mklogobj('simple1', 'info', { anInfo: 'object' }))
log1.info({ anInfo: 'object' })
expected.push(mklogobj('simple2', 'warn', { aWarn : 'object' }))
expected.push(mklogobj('simple2', 'warn', { aWarn: 'object' }))
log2.warn({ aWarn: 'object' })
expected.push(mklogobj('simple2', 'error', { anError : 'object' }))
expected.push(mklogobj('simple2', 'error', { anError: 'object' }))
log2.error({ anError: 'object' })
sink.end(function () {
var exp = expected.reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(sink.slice().toString()), safe(exp))
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})
})
test('test multiple outputs', function (t) {
test('test multiple outputs', (t) => {
t.plan(4)
t.on('end', bole.reset)
var debugSink = bl()
, infoSink = bl()
, warnSink = bl()
, errorSink = bl()
, log = bole('simple')
, expected = []
const debugSink = bl()
const infoSink = bl()
const warnSink = bl()
const errorSink = bl()
const log = bole('simple')
const expected = []
// add individual
bole.output({
level : 'debug'
, stream : debugSink
level: 'debug',
stream: debugSink
})

@@ -157,24 +147,24 @@

bole.output([
{
level : 'info'
, stream : infoSink
}
, {
level : 'warn'
, stream : warnSink
}
{
level: 'info',
stream: infoSink
},
{
level: 'warn',
stream: warnSink
}
])
bole.output({
level : 'error'
, stream : errorSink
level: 'error',
stream: errorSink
})
expected.push(mklogobj('simple', 'debug', { aDebug : 'object' }))
expected.push(mklogobj('simple', 'debug', { aDebug: 'object' }))
log.debug({ aDebug: 'object' })
expected.push(mklogobj('simple', 'info', { anInfo : 'object' }))
expected.push(mklogobj('simple', 'info', { anInfo: 'object' }))
log.info({ anInfo: 'object' })
expected.push(mklogobj('simple', 'warn', { aWarn : 'object' }))
expected.push(mklogobj('simple', 'warn', { aWarn: 'object' }))
log.warn({ aWarn: 'object' })
expected.push(mklogobj('simple', 'error', { anError : 'object' }))
expected.push(mklogobj('simple', 'error', { anError: 'object' }))
log.error({ anError: 'object' })

@@ -185,35 +175,15 @@

warnSink.end()
errorSink.end(function () {
errorSink.end(() => {
// debug
var exp = expected.reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(debugSink.slice().toString()), safe(exp))
t.equal(safe(debugSink.slice().toString()), safe(prepareExpected(expected)))
// info
exp = expected.slice(1).reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(infoSink.slice().toString()), safe(exp))
t.equal(safe(infoSink.slice().toString()), safe(prepareExpected(expected.slice(1))))
// warn
exp = expected.slice(2).reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(warnSink.slice().toString()), safe(exp))
t.equal(safe(warnSink.slice().toString()), safe(prepareExpected(expected.slice(2))))
// error
exp = expected.slice(3).reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(errorSink.slice().toString()), safe(exp))
t.equal(safe(errorSink.slice().toString()), safe(prepareExpected(expected.slice(3))))
})
})
test('test string formatting', function (t) {
test('test string formatting', (t) => {
t.plan(8)

@@ -223,17 +193,15 @@ t.on('end', bole.reset)

function testSingle (level, msg, args) {
var sink = bl()
, log = bole('strfmt')
, expected
const sink = bl()
const log = bole('strfmt')
bole.output({
level : level
, stream : sink
level: level,
stream: sink
})
expected = mklogobj('strfmt', level, msg)
const expected = mklogobj('strfmt', level, msg)
log[level].apply(log, args)
sink.end(function () {
var exp = JSON.stringify(expected) + '\n'
t.equal(safe(sink.slice().toString()), safe(exp))
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})

@@ -245,107 +213,96 @@

testSingle('debug', {}, [])
testSingle('debug', { message: 'test' }, [ 'test' ])
testSingle('info', { message: 'true' }, [ true ])
testSingle('info', { message: 'false' }, [ false ])
testSingle('warn', { message: 'a number [42]' }, [ 'a number [%d]', 42 ])
testSingle('error', { message: 'a string [str]' }, [ 'a string [%s]', 'str' ])
testSingle('debug', { message: 'test' }, ['test'])
testSingle('info', { message: 'true' }, [true])
testSingle('info', { message: 'false' }, [false])
testSingle('warn', { message: 'a number [42]' }, ['a number [%d]', 42])
testSingle('error', { message: 'a string [str]' }, ['a string [%s]', 'str'])
testSingle(
'error'
, { message: 'a string [str], a number [101], s, 1, 2 a b c' }
, [ 'a string [%s], a number [%d], %s, %s, %s', 'str', 101, 's', 1, 2, 'a', 'b', 'c' ]
'error'
, { message: 'a string [str], a number [101], s, 1, 2 a b c' }
, ['a string [%s], a number [%d], %s, %s, %s', 'str', 101, 's', 1, 2, 'a', 'b', 'c']
)
testSingle('error', { message: 'foo bar baz' }, [ 'foo', 'bar', 'baz' ])
testSingle('error', { message: 'foo bar baz' }, ['foo', 'bar', 'baz'])
})
test('test error formatting', function (t) {
test('test error formatting', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log = bole('errfmt')
, err = new Error('error msg in here')
, expected
const sink = bl()
const log = bole('errfmt')
const err = new Error('error msg in here')
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})
expected = mklogobj('errfmt', 'debug', { err: {
name : 'Error'
, message : 'error msg in here'
, stack : 'STACK'
}})
const expected = mklogobj('errfmt', 'debug', {
err: {
name: 'Error',
message: 'error msg in here',
stack: 'STACK'
}
})
log.debug(err)
sink.end(function () {
var exp = JSON.stringify(expected) + '\n'
, act = safe(sink.slice().toString())
act = act.replace(/("stack":")Error:[^"]+/, '$1STACK')
t.equal(act, safe(exp))
sink.end(() => {
const act = safe(sink.slice().toString()).replace(/("stack":")Error:[^"]+/, '$1STACK')
t.equal(act, safe(prepareExpected(expected)))
})
})
test('test error formatting with message', function (t) {
test('test error formatting with message', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log = bole('errfmt')
, err = new Error('error msg in here')
, expected
const sink = bl()
const log = bole('errfmt')
const err = new Error('error msg in here')
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})
expected = mklogobj('errfmt', 'debug', {
message : 'this is a message'
, err : {
name : 'Error'
, message : 'error msg in here'
, stack : 'STACK'
}
const expected = mklogobj('errfmt', 'debug', {
message: 'this is a message',
err: {
name: 'Error',
message: 'error msg in here',
stack: 'STACK'
}
})
log.debug(err, 'this is a %s', 'message')
sink.end(function () {
var exp = JSON.stringify(expected) + '\n'
, act = safe(sink.slice().toString())
act = act.replace(/("stack":")Error:[^"]+/, '$1STACK')
t.equal(act, safe(exp))
sink.end(() => {
const act = safe(sink.slice().toString()).replace(/("stack":")Error:[^"]+/, '$1STACK')
t.equal(act, safe(prepareExpected(expected)))
})
})
test('test request object', function (t) {
test('test request object', (t) => {
t.on('end', bole.reset)
var sink = bl()
, log = bole('reqfmt')
, expected
, server
, host
const sink = bl()
const log = bole('reqfmt')
let host
bole.output({
level : 'info'
, stream : sink
level: 'info',
stream: sink
})
server = http.createServer(function (req, res) {
expected = mklogobj('reqfmt', 'info', {
req: {
method : 'GET'
, url : '/foo?bar=baz'
, headers : {
host : host
, connection : 'close'
}
, remoteAddress : '127.0.0.1'
, remotePort : 'RPORT'
}
const server = http.createServer((req, res) => {
const expected = mklogobj('reqfmt', 'info', {
req: {
method: 'GET',
url: '/foo?bar=baz',
headers: {
host: host,
connection: 'close'
},
remoteAddress: '127.0.0.1',
remotePort: 'RPORT'
}
})

@@ -356,9 +313,5 @@ log.info(req)

sink.end(function () {
var exp = JSON.stringify(expected) + '\n'
, act = safe(sink.slice().toString())
act = act.replace(/("remotePort":)\d+/, '$1"RPORT"')
t.equal(act, safe(exp))
sink.end(() => {
const act = safe(sink.slice().toString()).replace(/("remotePort":)\d+/, '$1"RPORT"')
t.equal(act, safe(prepareExpected(expected)))
server.close(t.end.bind(t))

@@ -368,36 +321,33 @@ })

server.listen(0, '127.0.0.1', function () {
hreq.get('http://' + (host = this.address().address + ':' + this.address().port) + '/foo?bar=baz')
server.listen(0, '127.0.0.1', () => {
host = `${server.address().address}:${server.address().port}`
hreq.get(`http://${host}/foo?bar=baz`)
})
})
test('test request object with message', function (t) {
test('test request object with message', (t) => {
t.on('end', bole.reset)
var sink = bl()
, log = bole('reqfmt')
, expected
, server
, host
const sink = bl()
const log = bole('reqfmt')
let host
bole.output({
level : 'info'
, stream : sink
level: 'info',
stream: sink
})
server = http.createServer(function (req, res) {
expected = mklogobj('reqfmt', 'info', {
message : 'this is a message'
, req: {
method : 'GET'
, url : '/foo?bar=baz'
, headers : {
host : host
, connection : 'close'
}
, remoteAddress : '127.0.0.1'
, remotePort : 'RPORT'
}
const server = http.createServer((req, res) => {
const expected = mklogobj('reqfmt', 'info', {
message: 'this is a message',
req: {
method: 'GET',
url: '/foo?bar=baz',
headers: {
host: host,
connection: 'close'
},
remoteAddress: '127.0.0.1',
remotePort: 'RPORT'
}
})

@@ -408,9 +358,6 @@ log.info(req, 'this is a %s', 'message')

sink.end(function () {
var exp = JSON.stringify(expected) + '\n'
, act = safe(sink.slice().toString())
sink.end(() => {
const act = safe(sink.slice().toString()).replace(/("remotePort":)\d+/, '$1"RPORT"')
t.equal(act, safe(prepareExpected(expected)))
act = act.replace(/("remotePort":)\d+/, '$1"RPORT"')
t.equal(act, safe(exp))
server.close(t.end.bind(t))

@@ -420,76 +367,75 @@ })

server.listen(0, '127.0.0.1', function () {
hreq.get('http://' + (host = this.address().address + ':' + this.address().port) + '/foo?bar=baz')
server.listen(0, '127.0.0.1', () => {
host = `${server.address().address}:${server.address().port}`
hreq.get(`http://${host}/foo?bar=baz`)
})
})
test('test sub logger', function (t) {
test('test sub logger', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log = bole('parent')
, expected = []
, sub1
, sub2
const sink = bl()
const log = bole('parent')
const expected = []
let sub1
let sub2
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})
expected.push(mklogobj('parent', 'debug', { aDebug : 'object' }))
expected.push(mklogobj('parent', 'debug', { aDebug: 'object' }))
log.debug({ aDebug: 'object' })
expected.push(mklogobj('parent', 'info', { anInfo : 'object' }))
expected.push(mklogobj('parent', 'info', { anInfo: 'object' }))
log.info({ anInfo: 'object' })
expected.push(mklogobj('parent', 'warn', { aWarn : 'object' }))
expected.push(mklogobj('parent', 'warn', { aWarn: 'object' }))
log.warn({ aWarn: 'object' })
expected.push(mklogobj('parent', 'error', { anError : 'object' }))
expected.push(mklogobj('parent', 'error', { anError: 'object' }))
log.error({ anError: 'object' })
expected.push(mklogobj('parent:sub1', 'debug', { aDebug : 'object' }))
expected.push(mklogobj('parent:sub1', 'debug', { aDebug: 'object' }))
;(sub1 = log('sub1')).debug({ aDebug: 'object' })
expected.push(mklogobj('parent:sub1', 'info', { anInfo : 'object' }))
expected.push(mklogobj('parent:sub1', 'info', { anInfo: 'object' }))
sub1.info({ anInfo: 'object' })
expected.push(mklogobj('parent:sub2', 'warn', { aWarn : 'object' }))
expected.push(mklogobj('parent:sub2', 'warn', { aWarn: 'object' }))
;(sub2 = log('sub2')).warn({ aWarn: 'object' })
expected.push(mklogobj('parent:sub2:subsub', 'error', { anError : 'object' }))
expected.push(mklogobj('parent:sub2:subsub', 'error', { anError: 'object' }))
sub2('subsub').error({ anError: 'object' })
sink.end(function () {
var exp = expected.reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
t.equal(safe(sink.slice().toString()), safe(exp))
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})
})
test('test object logging', function (t) {
test('test object logging', (t) => {
t.on('end', bole.reset)
var sink = listStream.obj()
, log = bole('simple')
, expected = []
const sink = listStream.obj()
const log = bole('simple')
const expected = []
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})
expected.push(mklogobj('simple', 'debug', { aDebug : 'object' }))
expected.push(mklogobj('simple', 'debug', { aDebug: 'object' }))
log.debug({ aDebug: 'object' })
expected.push(mklogobj('simple', 'info', { anInfo : 'object' }))
expected.push(mklogobj('simple', 'info', { anInfo: 'object' }))
log.info({ anInfo: 'object' })
expected.push(mklogobj('simple', 'warn', { aWarn : 'object' }))
expected.push(mklogobj('simple', 'warn', { aWarn: 'object' }))
log.warn({ aWarn: 'object' })
expected.push(mklogobj('simple', 'error', { anError : 'object' }))
expected.push(mklogobj('simple', 'error', { anError: 'object' }))
log.error({ anError: 'object' })
sink.end(function () {
sink.end(() => {
t.equal(sink.length, expected.length, 'correct number of log entries')
for (var i = 0; i < expected.length; i++)
t.deepEqual(sink.get(i), expected[i], 'correct log entry #' + i)
for (let i = 0; i < expected.length; i++) {
const actual = sink.get(i)
actual.time = safeTime(actual.time)
expected[i].time = safeTime(actual.time)
t.deepEqual(actual, expected[i], `correct log entry #${i}`)
}
t.end()

@@ -499,14 +445,46 @@ })

test('test error and object logging', (t) => {
t.on('end', bole.reset)
test('test fast time', function (t) {
const sink = bl()
const log = bole('errobjfmt')
const err = new Error('anError')
bole.output({
level: 'debug',
stream: sink
})
log.debug(err, { aDebug: 'object' })
const expected = safe(prepareExpected(mklogobj('errobjfmt', 'debug', {
aDebug: 'object',
err: {
name: 'Error',
message: 'anError',
stack: 'STACK'
}
})))
sink.end(() => {
let act = safe(sink.slice().toString())
act = act.replace(/("stack":")Error:[^"]+/, '$1STACK')
t.equal(act, expected)
t.end()
})
})
test('test fast time', (t) => {
t.plan(1)
t.on('end', bole.reset)
var sink = bl()
, log = bole('simple')
, expected = []
const sink = bl()
const log = bole('simple')
const expected = []
bole.output({
level : 'debug'
, stream : sink
level: 'debug',
stream: sink
})

@@ -516,18 +494,35 @@

expected.push(mklogobj('simple', 'debug', { aDebug : 'object' }, true))
expected.push(mklogobj('simple', 'debug', { aDebug: 'object' }, true))
log.debug({ aDebug: 'object' })
expected.push(mklogobj('simple', 'info', { anInfo : 'object' }, true))
expected.push(mklogobj('simple', 'info', { anInfo: 'object' }, true))
log.info({ anInfo: 'object' })
expected.push(mklogobj('simple', 'warn', { aWarn : 'object' }, true))
expected.push(mklogobj('simple', 'warn', { aWarn: 'object' }, true))
log.warn({ aWarn: 'object' })
expected.push(mklogobj('simple', 'error', { anError : 'object' }, true))
expected.push(mklogobj('simple', 'error', { anError: 'object' }, true))
log.error({ anError: 'object' })
sink.end(function () {
var exp = expected.reduce(function (p, c) {
return p + JSON.stringify(c) + '\n'
}, '')
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})
})
t.equal(safe(sink.slice().toString()), safe(exp))
test('test undefined values', (t) => {
t.plan(1)
t.on('end', bole.reset)
const sink = bl()
const log = bole('simple')
const expected = []
bole.output({
level: 'debug',
stream: sink
})
})
expected.push(mklogobj('simple', 'debug', { message: 'testing', aDebug: undefined }))
log.debug({ aDebug: undefined }, 'testing')
sink.end(() => {
t.equal(safe(sink.slice().toString()), safe(prepareExpected(expected)))
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc