Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3f2ca10

Browse files
author
Michal Vlasák
committed
Add stackdriver format to all streams
1 parent f8578a0 commit 3f2ca10

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const multistream = require('pino-multi-stream').multistream;
44
const serializers = require('./serializers');
55
const { levels } = require('./levels');
66
const { expressMiddleware, expressErrorMiddleware } = require('./express');
7+
const { StackDriverFormatStream } = require('./stackdriver');
78

89
// This is a custom slightly edited version of pino-multistream's wirte method, whch adds support for maximum log level
910
// The original version was pino-multistream 3.1.2 (commit 71d98ae) - https://github.com/pinojs/pino-multi-stream/blob/71d98ae191e02c56e39e849d2c30d59c8c6db1b9/multistream.js#L43
@@ -68,6 +69,15 @@ const defaultLogger = (options = {}) => {
6869
{ level: levels.warn, stream: process.stderr },
6970
];
7071
}
72+
streams = streams.map(stream => {
73+
const newStream = new StackDriverFormatStream();
74+
newStream.pipe(stream.stream);
75+
return {
76+
level: stream.level,
77+
maxLevel: stream.maxLevel,
78+
stream: newStream,
79+
};
80+
});
7181

7282
if (!options.ignoredHttpMethods) {
7383
options.ignoredHttpMethods = ['OPTIONS'];

stackdriver.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { Transform } = require('stream');
2+
3+
const PINO_TO_STACKDRIVER = {
4+
10: 'DEBUG',
5+
20: 'DEBUG',
6+
30: 'INFO',
7+
40: 'WARNING',
8+
50: 'ERROR',
9+
60: 'CRITICAL',
10+
};
11+
12+
class StackDriverFormatStream extends Transform {
13+
_transform(chunk, encoding, callback) {
14+
const obj = JSON.parse(chunk);
15+
obj.severity = PINO_TO_STACKDRIVER[obj.level] || 'UNKNOWN';
16+
17+
this.push(`${JSON.stringify(obj)}\n`);
18+
callback();
19+
}
20+
}
21+
22+
module.exports = { StackDriverFormatStream };

0 commit comments

Comments
 (0)