Repository: pinojs/pino
Pull request #186 (2017-02-06 23:35:33)

Add prettyPrint to pino constructor options
See the discussion in issue #184 for the motivation to this PR.


Patch:
diff --git a/docs/API.md b/docs/API.md
index 7de614f..336e1d4 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -53,6 +53,8 @@
     Default: 'info'.
   * `levelVal` (integer): when defining a custom log level via `level`, set to an
     integer value to define the new level. Default: `undefined`.
+  * `prettyPrint` (boolean|object): enables [pino.pretty](#pretty). This is intended for non-production
+    configurations. This may be set to a configuration object as outlined in [pino.pretty](#pretty). Default: `false`.
   * `enabled` (boolean): enables logging. Default: `true`
 + `stream` (Writable): a writable stream where the logs will be written.
   Deafult: `process.stdout`
@@ -83,6 +85,8 @@ Returns a new [logger](#logger) instance.
   timestamp to ISO 8601 date format, and reserialize the JSON (equivalent to `pino -t`).
   * `formatter` (function): a custom function to format the line, is passed the
   JSON object as an argument and should return a string value.
+  * `levelFirst` (boolean): if set to `true`, it will print the name of the log
+  level as the first field in the log line. Default: `false`.
 
 ### Example:
 ```js
@@ -103,6 +107,10 @@ log.child({ widget: 'bar' }).warn('hello 2')
 ### Discussion:
 Provides access to the [CLI](cli.md) log prettifier as an API.
 
+This can also be enabled via the [constructor](#constructor) by setting the
+`prettyPrint` option to either `true` or a configuration object described
+in this section.
+
 <a id="logger"></a>
 # Logger
 
diff --git a/package.json b/package.json
index e1a87d1..32fcdf4 100644
--- a/package.json
+++ b/package.json
@@ -79,6 +79,7 @@
     "fast-safe-stringify": "^1.1.3",
     "flatstr": "^1.0.4",
     "once": "^1.3.3",
+    "pump": "^1.0.2",
     "quick-format-unescaped": "^1.0.0",
     "split2": "^2.0.1"
   }
diff --git a/pino.js b/pino.js
index 04c0edc..ffcdfaf 100644
--- a/pino.js
+++ b/pino.js
@@ -3,7 +3,9 @@
 var stringifySafe = require('fast-safe-stringify')
 var EventEmitter = require('events').EventEmitter
 var fs = require('fs')
+var pump = require('pump')
 var flatstr = require('flatstr')
+var pretty = require('./pretty')
 var events = require('./lib/events')
 var levels = require('./lib/levels')
 var tools = require('./lib/tools')
@@ -20,6 +22,7 @@ var defaultOptions = {
   extreme: false,
   level: 'info',
   levelVal: undefined,
+  prettyPrint: false,
   enabled: true
 }
 
@@ -30,8 +33,17 @@ function pino (opts, stream) {
     istream = iopts
     iopts = defaultOptions
   }
-  istream = istream || process.stdout
   iopts = Object.assign({}, defaultOptions, iopts)
+  if (iopts.extreme && iopts.prettyPrint) throw Error('cannot enable pretty print in extreme mode')
+  istream = istream || process.stdout
+  if (iopts.prettyPrint) {
+    var pstream = pretty(iopts.prettyPrint)
+    var origStream = istream
+    pump(pstream, origStream, function (err) {
+      if (err) logger.emit('error', err)
+    })
+    istream = pstream
+  }
 
   // internal options
   iopts.stringify = iopts.safe ? stringifySafe : JSON.stringify
@@ -259,7 +271,7 @@ module.exports.stdSerializers = {
   res: serializers.asResValue,
   err: serializers.asErrValue
 }
-module.exports.pretty = require('./pretty')
+module.exports.pretty = pretty
 Object.defineProperty(
   module.exports,
   'LOG_VERSION',


Test patch:
diff --git a/test/fixtures/pretty/basic.js b/test/fixtures/pretty/basic.js
new file mode 100644
index 0000000..e26b18a
--- /dev/null
+++ b/test/fixtures/pretty/basic.js
@@ -0,0 +1,6 @@
+global.process = { __proto__: process, pid: 123456 }
+Date.now = function () { return 1459875739796 }
+require('os').hostname = function () { return 'abcdefghijklmnopqr' }
+var pino = require(require.resolve('./../../../'))
+var log = pino({prettyPrint: true})
+log.info('h')
diff --git a/test/fixtures/pretty/levelFirst.js b/test/fixtures/pretty/levelFirst.js
new file mode 100644
index 0000000..76305aa
--- /dev/null
+++ b/test/fixtures/pretty/levelFirst.js
@@ -0,0 +1,6 @@
+global.process = { __proto__: process, pid: 123456 }
+Date.now = function () { return 1459875739796 }
+require('os').hostname = function () { return 'abcdefghijklmnopqr' }
+var pino = require(require.resolve('./../../../'))
+var log = pino({prettyPrint: {levelFirst: true}})
+log.info('h')
diff --git a/test/pretty.test.js b/test/pretty.test.js
index 73e49de..63741be 100644
--- a/test/pretty.test.js
+++ b/test/pretty.test.js
@@ -3,6 +3,9 @@
 var test = require('tap').test
 var pino = require('../')
 var os = require('os')
+var path = require('path')
+var writeStream = require('flush-write-stream')
+var fork = require('child_process').fork
 var split = require('split2')
 var hostname = os.hostname()
 
@@ -186,3 +189,33 @@ test('accept customLogLevvel', function (t) {
 
   instance.testCustom('test message')
 })
+
+test('can be enabled via constructor', function (t) {
+  t.plan(1)
+  var actual = ''
+  var child = fork(path.join(__dirname, 'fixtures', 'pretty', 'basic.js'), {silent: true})
+
+  child.stdout.pipe(writeStream(function (s, enc, cb) {
+    actual += s
+    cb()
+  }))
+
+  child.on('close', function () {
+    t.notEqual(actual.match(/\(123456 on abcdefghijklmnopqr\): h/), null)
+  })
+})
+
+test('can be enabled via constructor with pretty configuration', function (t) {
+  t.plan(1)
+  var actual = ''
+  var child = fork(path.join(__dirname, 'fixtures', 'pretty', 'levelFirst.js'), {silent: true})
+
+  child.stdout.pipe(writeStream(function (s, enc, cb) {
+    actual += s
+    cb()
+  }))
+
+  child.on('close', function () {
+    t.notEqual(actual.match(/^INFO.*h/), null)
+  })
+})