Skip to content

Commit

Permalink
Merge pull request #125 from pinojs/constructor-refactor
Browse files Browse the repository at this point in the history
Refactor internal Pino to use an options object
  • Loading branch information
mcollina committed Nov 13, 2016
2 parents 040437e + f400c3d commit 1ade750
Showing 1 changed file with 63 additions and 49 deletions.
112 changes: 63 additions & 49 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var stringifySafe = require('fast-safe-stringify')
var format = require('quick-format')
var EventEmitter = require('events').EventEmitter
var os = require('os')
var fs = require('fs')
var flatstr = require('flatstr')
var once = require('once')
var pid = process.pid
Expand All @@ -13,6 +14,17 @@ var extend = require('object.assign').getPolyfill()

var LOG_VERSION = 1

var defaultOptions = {
safe: true,
name: undefined,
serializers: {},
timestamp: true,
slowtime: false,
extreme: false,
level: 'info',
enabled: true
}

var levels = {
fatal: 60,
error: 50,
Expand Down Expand Up @@ -52,48 +64,48 @@ function streamIsBlockable (s) {
}

function pino (opts, stream) {
if (opts && (opts.writable || opts._writableState)) {
stream = opts
opts = null
var iopts = opts
var istream = stream
if (iopts && (iopts.writable || iopts._writableState)) {
istream = iopts
iopts = defaultOptions
}
stream = stream || process.stdout
opts = opts || {}
var timestamp = (opts.hasOwnProperty('timestamp')) ? opts.timestamp : true
var slowtime = opts.slowtime
var safe = opts.safe !== false
var stringify = safe ? stringifySafe : JSON.stringify
var formatOpts = safe ? null : {lowres: true}
var name = opts.name
var level = opts.level || 'info'
var serializers = opts.serializers || {}
var end = ',"v":' + LOG_VERSION + '}\n'
var cache = !opts.extreme ? null : {
istream = istream || process.stdout
iopts = extend({}, defaultOptions, iopts)

// internal options
iopts.stringify = iopts.safe ? stringifySafe : JSON.stringify
iopts.formatOpts = iopts.safe ? null : {lowres: true}
iopts.end = ',"v":' + LOG_VERSION + '}\n'
iopts.cache = !iopts.extreme ? null : {
size: 4096,
buf: ''
}
iopts.chindings = ''

if (opts.enabled === false) {
level = 'silent'
if (iopts.enabled === false) {
iopts.level = 'silent'
}

var logger = new Pino(level, stream, serializers, stringify, end, name, timestamp, slowtime, '', cache, formatOpts)
if (cache) {
var logger = new Pino(iopts, istream)
if (iopts.cache) {
// setImmediate is causing a very weird crash:
// Assertion failed: (cb_v->IsFunction()), function MakeCallback...
// but setTimeout isn't *shrug*
setTimeout(function () {
if (!streamIsBlockable(stream)) {
if (!streamIsBlockable(istream)) {
logger.emit('error', new Error('stream must have a file descriptor in extreme mode'))
}
}, 100)

onExit(function (code, evt) {
if (cache.buf) {
var buf = iopts.cache.buf
if (buf) {
// We need to block the process exit long enough to flush the buffer
// to the destination stream. We do that by forcing a synchronous
// write directly to the stream's file descriptor.
var fd = (stream.fd) ? stream.fd : stream._handle.fd
require('fs').writeSync(fd, cache.buf)
var fd = (istream.fd) ? istream.fd : istream._handle.fd
fs.writeSync(fd, buf)
}
if (!process._events[evt] || process._events[evt].length < 2 || !process._events[evt].filter(function (f) {
return f + '' !== onExit.passCode + '' && f + '' !== onExit.insertCode + ''
Expand All @@ -116,25 +128,25 @@ Object.defineProperty(pino, 'levels', {
enumerable: true
})

function Pino (level, stream, serializers, stringify, end, name, timestamp, slowtime, chindings, cache, formatOpts) {
function Pino (opts, stream) {
this.stream = stream
this.serializers = serializers
this.stringify = stringify
this.end = end
this.name = name
this.timestamp = timestamp
this.slowtime = slowtime
this.chindings = chindings
this.cache = cache
this.formatOpts = formatOpts
this._setLevel(level)
this.serializers = opts.serializers
this.stringify = opts.stringify
this.end = opts.end
this.name = opts.name
this.timestamp = opts.timestamp
this.slowtime = opts.slowtime
this.chindings = opts.chindings
this.cache = opts.cache
this.formatOpts = opts.formatOpts
this._setLevel(opts.level)

this._baseLog = flatstr(baseLog +
(this.name === undefined ? '' : '"name":' + stringify(this.name) + ','))
(this.name === undefined ? '' : '"name":' + this.stringify(this.name) + ','))

if (timestamp === false) {
if (opts.timestamp === false) {
this.time = getNoTime
} else if (slowtime) {
} else if (opts.slowtime) {
this.time = getSlowTime
} else {
this.time = getTime
Expand Down Expand Up @@ -283,18 +295,20 @@ Pino.prototype.child = function child (bindings) {
}
data = this.chindings + data.substr(0, data.length - 1)

return new Pino(
bindings.level || this.level,
this.stream,
bindings.hasOwnProperty('serializers') ? extend(this.serializers, bindings.serializers) : this.serializers,
this.stringify,
this.end,
this.name,
this.timestamp,
this.slowtime,
data,
this.cache,
this.formatOpts)
var opts = {
level: bindings.level || this.level,
serializers: bindings.hasOwnProperty('serializers') ? extend(this.serializers, bindings.serializers) : this.serializers,
stringify: this.stringify,
end: this.end,
name: this.name,
timestamp: this.timestamp,
slowtime: this.slowtime,
chindings: data,
cache: this.cache,
formatOpts: this.formatOpts
}

return new Pino(opts, this.stream)
}

Pino.prototype.write = function (obj, msg, num) {
Expand Down

0 comments on commit 1ade750

Please sign in to comment.