/
logger.ts
78 lines (70 loc) · 1.82 KB
/
logger.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as bunyan from 'bunyan';
import * as _ from 'lodash';
import { Writable } from 'stream';
import { CommitHash, getImageName } from './api';
import { getLogPath } from './builder';
import { config } from './config';
export const ringbuffer = new bunyan.RingBuffer( { limit: 1000 } );
const dserveLogger = bunyan.createLogger( {
name: 'dserve',
streams: [
{
type: 'rotating-file',
path: './logs/log.txt',
period: '1d',
count: 7,
},
{
level: bunyan.DEBUG,
type: 'raw',
stream: ringbuffer,
},
{
stream: process.stdout,
level: bunyan.DEBUG,
},
],
serializers: bunyan.stdSerializers, // allows one to use err, req, and res as special keys
src: true,
} );
/* super convenient name */
export const l = {
log: dserveLogger.info.bind( dserveLogger ),
error: dserveLogger.error.bind( dserveLogger ),
};
/**
* Creates a child logger that outputs to the build directory and
* outputs errors to the console.
*
* @param commitHash - hash to make logger for
*/
export function getLoggerForBuild( commitHash: CommitHash ) {
const path = getLogPath( commitHash );
const logger = dserveLogger.child( {
streams: [
{
type: 'file',
path,
},
{
stream: process.stdout,
level: bunyan.INFO,
},
],
commitHash,
imageName: getImageName( commitHash ),
} );
// we want it to be a child so that
// it inherits al the same properties as the parent
// except we don't want any of the parents streams
// so this line removes them all
// @ts-ignore this needs to be fixed with proper typing
( ( logger as any ) as Logger ).streams = _.filter( ( logger as any ).streams, { path } );
return logger;
}
type Logger = { streams: Array< { stream: Writable; type: string } > };
export function closeLogger( logger: Logger ) {
logger.streams.forEach( stream => {
stream.stream.end();
} );
}