This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.ts
159 lines (129 loc) · 3.97 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as log4js from "log4js";
import * as util from "util";
import * as stream from "stream";
import * as marked from "marked";
const TerminalRenderer = require("marked-terminal");
const chalk = require("chalk");
export class Logger implements ILogger {
private log4jsLogger: log4js.ILogger = null;
private passwordRegex = /(password=).*?(['&,]|$)|(password["']?\s*:\s*["']).*?(["'])/i;
private passwordReplacement = "$1$3*******$2$4";
private static LABEL = "[WARNING]:";
constructor($config: Config.IConfig,
private $options: ICommonOptions) {
const appenders: log4js.IAppender[] = [];
if (!$config.CI_LOGGER) {
appenders.push({
type: "console",
layout: {
type: "messagePassThrough"
}
});
}
log4js.configure({ appenders: appenders });
this.log4jsLogger = log4js.getLogger();
if (this.$options.log) {
this.log4jsLogger.setLevel(this.$options.log);
} else {
this.log4jsLogger.setLevel($config.DEBUG ? "TRACE" : "INFO");
}
}
setLevel(level: string): void {
this.log4jsLogger.setLevel(level);
}
getLevel(): string {
return this.log4jsLogger.level.toString();
}
fatal(...args: string[]): void {
this.log4jsLogger.fatal.apply(this.log4jsLogger, args);
}
error(...args: string[]): void {
const message = util.format.apply(null, args);
const colorizedMessage = message.red;
this.log4jsLogger.error.apply(this.log4jsLogger, [colorizedMessage]);
}
warn(...args: string[]): void {
const message = util.format.apply(null, args);
const colorizedMessage = message.yellow;
this.log4jsLogger.warn.apply(this.log4jsLogger, [colorizedMessage]);
}
warnWithLabel(...args: string[]): void {
const message = util.format.apply(null, args);
this.warn(`${Logger.LABEL} ${message}`);
}
info(...args: string[]): void {
this.log4jsLogger.info.apply(this.log4jsLogger, args);
}
debug(...args: string[]): void {
const encodedArgs: string[] = this.getPasswordEncodedArguments(args);
this.log4jsLogger.debug.apply(this.log4jsLogger, encodedArgs);
}
trace(...args: string[]): void {
const encodedArgs: string[] = this.getPasswordEncodedArguments(args);
this.log4jsLogger.trace.apply(this.log4jsLogger, encodedArgs);
}
out(...args: string[]): void {
console.log(util.format.apply(null, args));
}
write(...args: string[]): void {
process.stdout.write(util.format.apply(null, args));
}
prepare(item: any): string {
if (typeof item === "undefined" || item === null) {
return "[no content]";
}
if (typeof item === "string") {
return item;
}
// do not try to read streams, because they may not be rewindable
if (item instanceof stream.Readable) {
return "[ReadableStream]";
}
// There's no point in printing buffers
if (item instanceof Buffer) {
return "[Buffer]";
}
return JSON.stringify(item);
}
public printInfoMessageOnSameLine(message: string): void {
if (!this.$options.log || this.$options.log === "info") {
this.write(message);
}
}
public printMsgWithTimeout(message: string, timeout: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
setTimeout(() => {
this.printInfoMessageOnSameLine(message);
resolve();
}, timeout);
});
}
public printMarkdown(...args: string[]): void {
const opts = {
unescape: true,
link: chalk.red,
tableOptions: {
chars: { 'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' },
style: {
'padding-left': 1,
'padding-right': 1,
head: ['green', 'bold'],
border: ['grey'],
compact: false
}
}
};
marked.setOptions({ renderer: new TerminalRenderer(opts) });
const formattedMessage = marked(util.format.apply(null, args));
this.write(formattedMessage);
}
private getPasswordEncodedArguments(args: string[]): string[] {
return _.map(args, argument => {
if (typeof argument === 'string' && !!argument.match(/password/i)) {
argument = argument.replace(this.passwordRegex, this.passwordReplacement);
}
return argument;
});
}
}
$injector.register("logger", Logger);