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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
import is from '@sindresorhus/is';
import * as bunyan from 'bunyan';
import { nanoid } from 'nanoid';
import cmdSerializer from './cmd-serializer';
import configSerializer from './config-serializer';
import errSerializer from './err-serializer';
import { RenovateStream } from './pretty-stdout';
import type { BunyanRecord, Logger } from './types';
import { ProblemStream, validateLogLevel, withSanitizer } from './utils';
let logContext: string = process.env.LOG_CONTEXT ?? nanoid();
let curMeta: Record<string, unknown> = {};
const problems = new ProblemStream();
// istanbul ignore if: not easily testable
if (is.string(process.env.LOG_LEVEL)) {
process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase().trim();
}
validateLogLevel(process.env.LOG_LEVEL);
const stdout: bunyan.Stream = {
name: 'stdout',
level:
(process.env.LOG_LEVEL as bunyan.LogLevel) ||
/* istanbul ignore next: not testable */ 'info',
stream: process.stdout,
};
// istanbul ignore else: not testable
if (process.env.LOG_FORMAT !== 'json') {
// TODO: typings (#9615)
const prettyStdOut = new RenovateStream() as any;
prettyStdOut.pipe(process.stdout);
stdout.stream = prettyStdOut;
stdout.type = 'raw';
}
const bunyanLogger = bunyan.createLogger({
name: 'renovate',
serializers: {
body: configSerializer,
cmd: cmdSerializer,
config: configSerializer,
migratedConfig: configSerializer,
originalConfig: configSerializer,
presetConfig: configSerializer,
oldConfig: configSerializer,
newConfig: configSerializer,
err: errSerializer,
},
streams: [
stdout,
{
name: 'problems',
level: 'warn' as bunyan.LogLevel,
stream: problems as any,
type: 'raw',
},
].map(withSanitizer),
});
const logFactory =
(level: bunyan.LogLevelString): any =>
(p1: any, p2: any): void => {
if (p2) {
// meta and msg provided
bunyanLogger[level]({ logContext, ...curMeta, ...p1 }, p2);
} else if (is.string(p1)) {
// only message provided
bunyanLogger[level]({ logContext, ...curMeta }, p1);
} else {
// only meta provided
bunyanLogger[level]({ logContext, ...curMeta, ...p1 });
}
};
const loggerLevels: bunyan.LogLevelString[] = [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
];
export const logger: Logger = {} as any;
loggerLevels.forEach((loggerLevel) => {
logger[loggerLevel] = logFactory(loggerLevel);
});
export function setContext(value: string): void {
logContext = value;
}
export function getContext(): any {
return logContext;
}
// setMeta overrides existing meta, may remove fields if no longer existing
export function setMeta(obj: Record<string, unknown>): void {
curMeta = { ...obj };
}
// addMeta overrides or adds fields but does not remove any
export function addMeta(obj: Record<string, unknown>): void {
curMeta = { ...curMeta, ...obj };
}
// removeMeta removes the provided fields from meta
export function removeMeta(fields: string[]): void {
Object.keys(curMeta).forEach((key) => {
if (fields.includes(key)) {
delete curMeta[key];
}
});
}
export /* istanbul ignore next */ function addStream(
stream: bunyan.Stream
): void {
bunyanLogger.addStream(withSanitizer(stream));
}
export function levels(name: string, level: bunyan.LogLevel): void {
bunyanLogger.levels(name, level);
}
export function getProblems(): BunyanRecord[] {
return problems.getProblems();
}
export function clearProblems(): void {
return problems.clearProblems();
}
|