Skip to content

Commit

Permalink
Merge pull request #237 from sandyplace/master
Browse files Browse the repository at this point in the history
add forceColor option to pretty
  • Loading branch information
David Mark Clements authored Apr 27, 2017
2 parents 74bc0fd + bb66fe5 commit c204595
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (arg('-h') || arg('--help')) {
process.stdin.pipe(pretty({
timeTransOnly: arg('-t'),
levelFirst: arg('-l'),
forceColor: arg('-c'),
messageKey: messageKeyArg()
})).pipe(process.stdout)
}
Expand Down
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Returns a new [logger](#logger) instance.
level as the first field in the log line. Default: `false`.
* `messageKey` (string): the key in the JSON object to use as the highlighted
message. Default: `msg`.
* `forceColor` (boolean): if set to `true`, will add color information to the formatted output
message. Default: `false`.

### Example:
```js
Expand Down
12 changes: 12 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ Into this:
```sh
INFO [2016-03-09T15:27:09.339Z] (14139 on MacBook-Pro-3.home): hello world
```
If you would like to enforce the output to be color encoded you can specify the `-c` flag
`cat log | pino -c` will transform this:

```js
{"pid":14139,"hostname":"MacBook-Pro-3.home","level":30,"fooMessage":"hello world","time":1457537229339,"v":1}
```

Into this:

```sh
[2017-04-25T17:32:09.662Z] INFO (24280 on SP2): hello world
```
3 changes: 2 additions & 1 deletion pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function pretty (opts) {
var formatter = opts && opts.formatter
var levelFirst = opts && opts.levelFirst
var messageKey = opts && opts.messageKey
var forceColor = opts && opts.forceColor
messageKey = messageKey || 'msg'

var stream = split(mapLine)
Expand All @@ -67,7 +68,7 @@ function pretty (opts) {

stream.pipe = function (dest, opts) {
ctx = new chalk.constructor({
enabled: !!(chalk.supportsColor && dest.isTTY)
enabled: !!((chalk.supportsColor && dest.isTTY) || forceColor)
})

levelColors = {
Expand Down
11 changes: 11 additions & 0 deletions test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ test('pino pretty moves level to start on flag', function (t) {
instance.info('hello world')
})

test('pino pretty force color on flag', function (t) {
t.plan(1)
var prettier = pretty({ forceColor: true })
prettier.pipe(split(function (line) {
t.ok(line.match(/.*\u001b\[32mINFO\u001b\[39m.*\u001b\[36mhello world\u001b\[39m$/), 'color coding information is encoded in the line')
return line
}))
var instance = pino(prettier)

instance.info('hello world')
})
test('pino transform can just parse the dates', function (t) {
t.plan(1)
var prettier = pretty({ timeTransOnly: true })
Expand Down

0 comments on commit c204595

Please sign in to comment.