Skip to content

Commit

Permalink
Resolve issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Apr 7, 2018
1 parent 2183729 commit ff556b2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ feed, to the formatted log line.
to human readable format (see: `--translateTime`). The default format string
is `'yyyy-mm-dd HH:MM:ss.l o'`. For a list of available patter letters
see the [`dateformat` documentation](https://www.npmjs.com/package/dateformat).
When the value is anything other than the default value, `--translateTime` is
implied.
+ `--errorProps` (`-e`): When formatting an error object, display this list
of properties. The list should be a comma separated list of properties Default: `''`.
+ `--levelFirst` (`-l`): Display the log level name before the logged date and time.
Expand Down
5 changes: 3 additions & 2 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ const pump = require('pump')
const split = require('split2')
const through = require('through2')
const prettyFactory = require('./')
const CONSTANTS = require('./lib/constants')

args
.option(['c', 'colorize'], 'Force adding color sequences to the output')
.option(['f', 'crlf'], 'Append CRLF instead of LF to formatted lines')
.option(['d', 'dateFormat'], 'A format string to govern display of dates', 'yyyy-MM-dd HH:mm:ss.SSS Z')
.option(['d', 'dateFormat'], 'A format string to govern display of dates. When not set to the default value, `--translateTime` is implied', CONSTANTS.DATE_FORMAT)
.option(['e', 'errorProps'], 'Comma separated list of properties on error objects to show (`*` for all properties)', '')
.option(['l', 'levelFirst'], 'Display the log level as the first output field')
.option(['k', 'errorLikeObjectKeys'], 'Define which keys contain error objects (`-k err,error`)', 'err,error')
.option(['m', 'messageKey'], 'Highlight the message under the specified key', 'msg')
.option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY)
.option(['n', 'localTime'], 'Display timestamps according to system timezone')
.option(['t', 'translateTime'], 'Convert Epoch timestamps to ISO format')

Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const chalk = require('chalk')
const dateformat = require('dateformat')
const jsonParser = require('fast-json-parse')

const CONSTANTS = require('./lib/constants')

const levels = {
default: 'USERLVL',
60: 'FATAL',
Expand All @@ -17,12 +19,12 @@ const levels = {
const defaultOptions = {
colorize: false,
crlf: false,
dateFormat: 'yyyy-mm-dd HH:MM:ss.l o',
dateFormat: CONSTANTS.DATE_FORMAT,
errorLikeObjectKeys: ['err', 'error'],
errorProps: '',
levelFirst: false,
localTime: false,
messageKey: 'msg',
messageKey: CONSTANTS.MESSAGE_KEY,
translateTime: false,
useMetadata: false,
outputStream: process.stdout
Expand Down Expand Up @@ -54,6 +56,10 @@ module.exports = function prettyFactory (options) {
const errorLikeObjectKeys = opts.errorLikeObjectKeys
const errorProps = opts.errorProps.split(',')

if (opts.dateFormat.length > 0 && opts.dateFormat !== CONSTANTS.DATE_FORMAT) {
opts.translateTime = true
}

const color = {
default: nocolor,
60: nocolor,
Expand Down
6 changes: 6 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = {
DATE_FORMAT: 'yyyy-mm-dd HH:MM:ss.l o',
MESSAGE_KEY: 'msg'
}
5 changes: 1 addition & 4 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ test('basic prettifier tests', (t) => {

t.test('will format date with a custom format string', (t) => {
t.plan(1)
const pretty = prettyFactory({
translateTime: true,
dateFormat: 'yyyy-mm-dd HH:MM'
})
const pretty = prettyFactory({dateFormat: 'yyyy-mm-dd HH:MM'})
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
Expand Down
11 changes: 11 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,16 @@ test('cli', (t) => {
t.tearDown(() => child.kill())
})

t.test('translates time to default format', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-t'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[2018-03-30 17:35:28.992 +0000] INFO (42 on foo): hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})

t.end()
})

0 comments on commit ff556b2

Please sign in to comment.