forked from pastjean/pino-logdna-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·56 lines (47 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env node
'use strict'
var split = require('split2')
var pump = require('pump')
var through = require('through2')
var levels = {
10: "trace",
20: "debug",
30: "info",
40: "warn",
50: "error",
60: "fatal",
}
var logLevelTransformer = through.obj(function (chunk, enc, cb) {
if (typeof chunk === 'string') {
console.log(chunk);
cb();
return;
}
if (chunk.level) {
chunk = Object.assign({},chunk,{
level: levels[chunk.level] || chunk.level,
// Report full stack trace if available
message: chunk.stack || chunk.msg,
timestamp: new Date(chunk.time)
})
delete chunk['msg']
delete chunk['stack']
delete chunk['time']
}
console.log(JSON.stringify(chunk))
cb()
})
/**
* Parse as JSON if it is JSON, otherwise just return it.
*/
function tryParseJSON(s) {
try {
return JSON.parse(s);
} catch (e) {
if (e.name === 'SyntaxError') {
return s;
}
throw e;
}
}
pump(process.stdin, split(tryParseJSON), logLevelTransformer)