Skip to content

Commit

Permalink
Merge pull request #82 from colm-mcqs/add-level-first-flag
Browse files Browse the repository at this point in the history
Add level first flag to pretty options.
  • Loading branch information
mcollina authored Sep 5, 2016
2 parents 4e052cc + afbceca commit 2361ca8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,20 @@ Then we simply pipe a log file through `pino`:
cat log | pino
```

There's also a transformer flag that converts Epoch timestamps to ISO timestamps.
There are also two transformer flags..

`-t` that converts Epoch timestamps to ISO timestamps.

```sh
cat log | pino -t
```
and `-l` that flips the time and level on the standard output.

```sh
cat log | pino -l
```

For instance, `pino -t` will transform this:
`pino -t` will transform this:

```js
{"pid":14139,"hostname":"MacBook-Pro-3.home","level":30,"msg":"hello world","time":1457537229339,"v":0}
Expand All @@ -167,6 +174,19 @@ Into this:
{"pid":14139,"hostname":"MacBook-Pro-3.home","level":30,"msg":"hello world","time":"2016-03-09T15:27:09.339Z","v":0}
```


`pino -l` will transform this:

```sh
[2016-03-09T15:27:09.339Z] INFO (14139 on MacBook-Pro-3.home): hello world
```

Into this:

```sh
INFO [2016-03-09T15:27:09.339Z] (14139 on MacBook-Pro-3.home): hello world
```

<a name="api"></a>
## API

Expand Down
15 changes: 12 additions & 3 deletions pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function filter (value) {

function pretty (opts) {
var timeTransOnly = opts && opts.timeTransOnly
var levelFirst = opts && opts.levelFirst

var stream = split(mapLine)
var ctx
Expand Down Expand Up @@ -84,11 +85,14 @@ function pretty (opts) {
}

if (timeTransOnly) {
value.time = new Date(value.time).toISOString()
value.time = asISODate(value.time)
return JSON.stringify(value) + '\n'
}

line = '[' + new Date(value.time).toISOString() + '] ' + asColoredLevel(value)
line = (levelFirst)
? asColoredLevel(value) + ' [' + asISODate(value.time) + ']'
: '[' + asISODate(value.time) + '] ' + asColoredLevel(value)

line += ' ('
if (value.name) {
line += value.name + '/'
Expand All @@ -107,6 +111,10 @@ function pretty (opts) {
return line
}

function asISODate (time) {
return new Date(time).toISOString()
}

function asColoredLevel (value) {
return levelColors[value.level](levels[value.level])
}
Expand All @@ -121,7 +129,8 @@ if (require.main === module) {
console.log(require('./package.json').version)
} else {
process.stdin.pipe(pretty({
timeTransOnly: arg('-t')
timeTransOnly: arg('-t'),
levelFirst: arg('-l')
})).pipe(process.stdout)
}
}
Expand Down
17 changes: 16 additions & 1 deletion test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ test('pino transform prettifies', function (t) {
var pretty = pino.pretty()
pretty.pipe(split(function (line) {
t.ok(line.match(/.*hello world$/), 'end of line matches')
t.ok(line.match(/.*INFO.*/), 'includes level')
t.ok(line.match(/(?!^)INFO.*/), 'includes level')
t.ok(line.indexOf('' + process.pid) > 0, 'includes pid')
t.ok(line.indexOf('' + hostname) > 0, 'includes hostname')
return line
}))
var instance = pino(pretty)

instance.info('hello world')
})

test('pino pretty moves level to start on flag', function (t) {
t.plan(4)
var pretty = pino.pretty({ levelFirst: true })
pretty.pipe(split(function (line) {
t.ok(line.match(/.*hello world$/), 'end of line matches')
t.ok(line.match(/^INFO.*/), 'level is at start of line')
t.ok(line.indexOf('' + process.pid) > 0, 'includes pid')
t.ok(line.indexOf('' + hostname) > 0, 'includes hostname')
return line
Expand Down
5 changes: 5 additions & 0 deletions usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

cat log | pino -t

To flip level and time/date in standard output use the -l flag

cat log | pino -l

Flags
-h | --help Display Help
-v | --version Display Version
-t Convert Epoch timestamps to ISO
-l Flip level and date

0 comments on commit 2361ca8

Please sign in to comment.