Skip to content

Commit

Permalink
reduce code and further optimize LOG
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmarkclements committed Jun 12, 2018
1 parent 2641ef3 commit 8f87bf5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 59 deletions.
8 changes: 0 additions & 8 deletions README.md
Expand Up @@ -382,14 +382,6 @@ var pino = require('pino')({
<a name="caveats"></a>
## Caveats

There's some fine points to be aware of, which are a result of worthwhile trade-offs:

### 11 Arguments

The logger functions (e.g. `pino.info`) can take a maximum of 11 arguments.

If you need more than that to write a log entry, you're probably doing it wrong.

### Duplicate Keys

It's possible for naming conflicts to arise between child loggers and
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/multiArg.bench.js
Expand Up @@ -10,11 +10,11 @@ var dest = fs.createWriteStream('/dev/null')
var loglevel = require('./loglevelMock')(dest)
var plog = pino(dest)
delete require.cache[require.resolve('../')]
var plogExtreme = require('../')({extreme: true}, dest)
var plogExtreme = require('../')(pino.extreme('/dev/null'))
delete require.cache[require.resolve('../')]
var plogUnsafe = require('../')({safe: false}, dest)
delete require.cache[require.resolve('../')]
var plogUnsafeExtreme = require('../')({extreme: true, safe: false}, dest)
var plogUnsafeExtreme = require('../')({safe: false}, pino.extreme('/dev/null'))

process.env.DEBUG = 'dlog'
var debug = require('debug')
Expand Down
4 changes: 2 additions & 2 deletions browser.js
Expand Up @@ -209,8 +209,8 @@ function asObject (logger, level, args, ts) {
while (lvl-- && typeof args[0] === 'object') {
Object.assign(o, args.shift())
}
msg = args.length ? format(args) : undefined
} else if (typeof msg === 'string') msg = format(args)
msg = args.length ? format(args.shift(), args) : undefined
} else if (typeof msg === 'string') msg = format(args.shift(), args)
if (msg !== undefined) o.msg = msg
return o
}
Expand Down
55 changes: 9 additions & 46 deletions lib/tools.js
@@ -1,8 +1,7 @@
'use strict'

const format = require('quick-format-unescaped')
const util = require('util')
const serializers = require('pino-std-serializers')
const { mapHttpRequest, mapHttpResponse } = require('pino-std-serializers')
const SonicBoom = require('sonic-boom')
const {
lsCacheSym,
Expand Down Expand Up @@ -55,52 +54,16 @@ function asMetaWrapper (pretty, dest) {
}
}

function countInterp (s, i) {
var n = 0
var pos = 0
while (true) {
pos = s.indexOf(i, pos)
if (pos >= 0) {
++n
pos += 2
} else break
}
return n
}

function genLog (z) {
return function LOG (a, b, c, d, e, f, g, h, i, j, k) {
var l = 0
var m = null
var n = null
var o
var p
if (typeof a === 'object' && a !== null) {
m = a
n = [b, c, d, e, f, g, h, i, j, k]
l = 1

if (m.method && m.headers && m.socket) {
m = serializers.mapHttpRequest(m)
} else if (typeof m.setHeader === 'function') {
m = serializers.mapHttpResponse(m)
return function LOG (o, ...n) {
if (typeof o === 'object' && o !== null) {
if (o.method && o.headers && o.socket) {
o = mapHttpRequest(o)
} else if (typeof o.setHeader === 'function') {
o = mapHttpResponse(o)
}
} else {
n = [a, b, c, d, e, f, g, h, i, j, k]
}
p = n.length = arguments.length - l
if (p > 1) {
l = typeof a === 'string' ? countInterp(a, '%j') : 0
if (l) {
n.length = l + countInterp(a, '%d') + countInterp(a, '%s') + 1
o = `${util.format.apply(null, n)}`
} else {
o = format(n, this[formatOptsSym])
}
} else if (p === 1) {
o = n[0]
}
this[writeSym](m, o, z)
this[writeSym](o, format(null, n, this[formatOptsSym]), z)
} else this[writeSym](null, format(o, n, this[formatOptsSym]), z)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -84,7 +84,7 @@
"flatstr": "^1.0.5",
"pino-std-serializers": "^2.0.0",
"pump": "^3.0.0",
"quick-format-unescaped": "^2.0.1",
"quick-format-unescaped": "^3.0.0",
"sonic-boom": "^0.5.0"
}
}

0 comments on commit 8f87bf5

Please sign in to comment.