forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
339 lines (319 loc) · 13.3 KB
/
logger.ts
File metadata and controls
339 lines (319 loc) · 13.3 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// tslint:disable:no-console no-any
import * as path from 'path';
import * as util from 'util';
import { createLogger, format, transports } from 'winston';
import { EXTENSION_ROOT_DIR } from '../constants';
import { sendTelemetryEvent } from '../telemetry';
import { isTestExecution } from './constants';
import { LogLevel } from './types';
import { StopWatch } from './utils/stopWatch';
// tslint:disable-next-line: no-var-requires no-require-imports
const TransportStream = require('winston-transport');
// Initialize the loggers as soon as this module is imported.
const consoleLogger = createLogger();
const fileLogger = createLogger();
initializeConsoleLogger();
initializeFileLogger();
const logLevelMap = {
[LogLevel.Error]: 'error',
[LogLevel.Information]: 'info',
[LogLevel.Warning]: 'warn'
};
function log(logLevel: LogLevel, ...args: any[]) {
if (consoleLogger.transports.length > 0) {
const message = args.length === 0 ? '' : util.format(args[0], ...args.slice(1));
consoleLogger.log(logLevelMap[logLevel], message);
}
logToFile(logLevel, ...args);
}
function logToFile(logLevel: LogLevel, ...args: any[]) {
if (fileLogger.transports.length === 0) {
return;
}
const message = args.length === 0 ? '' : util.format(args[0], ...args.slice(1));
fileLogger.log(logLevelMap[logLevel], message);
}
/**
* Initialize the logger for console.
* We do two things here:
* - Anything written to the logger will be displayed in the console window as well
* This is the behavior of the extension when running it.
* When running tests on CI, we might not want this behavior, as it'll pollute the
* test output with logging (as mentioned this is optional).
* Messages logged using our logger will be prefixed with `Python Extension: ....` for console window.
* This way, its easy to identify messages specific to the python extension.
* - Monkey patch the console.log and similar methods to send messages to the file logger.
* When running UI tests or similar, and we want to see everything that was dumped into `console window`,
* then we need to hijack the console logger.
* To do this we need to monkey patch the console methods.
* This is optional (generally done when running tests on CI).
*/
// tslint:disable-next-line: max-func-body-length
function initializeConsoleLogger() {
const logMethods = {
log: Symbol.for('log'),
info: Symbol.for('info'),
error: Symbol.for('error'),
debug: Symbol.for('debug'),
warn: Symbol.for('warn')
};
function logToConsole(stream: 'info' | 'error' | 'warn' | 'log' | 'debug', ...args: any[]) {
if (['info', 'error', 'warn', 'log', 'debug'].indexOf(stream) === -1) {
stream = 'log';
}
// Further below we monkeypatch the console.log, etc methods.
const fn = (console as any)[logMethods[stream]] || console[stream] || console.log;
fn(...args);
}
// Hijack `console.log` when running tests on CI.
if (process.env.VSC_PYTHON_LOG_FILE && process.env.TF_BUILD) {
/*
What we're doing here is monkey patching the console.log so we can send everything sent to console window into our logs.
This is only required when we're directly writing to `console.log` or not using our `winston logger`.
This is something we'd generally turn on, only on CI so we can see everything logged to the console window (via the logs).
*/
// Keep track of the original functions before we monkey patch them.
// Using symbols guarantee the properties will be unique & prevents clashing with names other code/library may create or have created.
(console as any)[logMethods.log] = console.log;
(console as any)[logMethods.info] = console.info;
(console as any)[logMethods.error] = console.error;
(console as any)[logMethods.debug] = console.debug;
(console as any)[logMethods.warn] = console.warn;
// tslint:disable-next-line: no-function-expression
console.log = function() {
const args = Array.prototype.slice.call(arguments);
logToConsole('log', ...args);
logToFile(LogLevel.Information, ...args);
};
// tslint:disable-next-line: no-function-expression
console.info = function() {
const args = Array.prototype.slice.call(arguments);
logToConsole('info', ...args);
logToFile(LogLevel.Information, ...args);
};
// tslint:disable-next-line: no-function-expression
console.warn = function() {
const args = Array.prototype.slice.call(arguments);
logToConsole('warn', ...args);
logToFile(LogLevel.Warning, ...args);
};
// tslint:disable-next-line: no-function-expression
console.error = function() {
const args = Array.prototype.slice.call(arguments);
logToConsole('error', ...args);
logToFile(LogLevel.Error, ...args);
};
// tslint:disable-next-line: no-function-expression
console.debug = function() {
const args = Array.prototype.slice.call(arguments);
logToConsole('debug', ...args);
logToFile(LogLevel.Information, ...args);
};
}
if (isTestExecution() && !process.env.VSC_PYTHON_FORCE_LOGGING) {
// Do not log to console if running tests on CI and we're not asked to do so.
return;
}
// Rest of this stuff is just to instantiate the console logger.
// I.e. when we use our logger, ensure we also log to the console (for end users).
const formattedMessage = Symbol.for('message');
class ConsoleTransport extends TransportStream {
constructor(options?: any) {
super(options);
}
public log?(info: { level: string; message: string; [formattedMessage]: string }, next: () => void): any {
setImmediate(() => this.emit('logged', info));
logToConsole(info.level as any, info[formattedMessage] || info.message);
if (next) {
next();
}
}
}
const consoleFormatter = format.printf(({ level, message, label, timestamp }) => {
// If we're on CI server, no need for the label (prefix)
// Pascal casing og log level, so log files get highlighted when viewing in VSC and other editors.
const prefix = `${level.substring(0, 1).toUpperCase()}${level.substring(1)} ${
process.env.TF_BUILD ? '' : label
}`;
return `${prefix.trim()} ${timestamp}: ${message}`;
});
const consoleFormat = format.combine(
format.label({ label: 'Python Extension:' }),
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
consoleFormatter
);
consoleLogger.add(new ConsoleTransport({ format: consoleFormat }) as any);
}
/**
* Send all logging output to a log file.
* We log to the file only if a file has been specified as an env variable.
* Currently this is setup on CI servers.
*/
function initializeFileLogger() {
if (!process.env.VSC_PYTHON_LOG_FILE) {
return;
}
const fileFormatter = format.printf(({ level, message, timestamp }) => {
// Pascal casing og log level, so log files get highlighted when viewing in VSC and other editors.
return `${level.substring(0, 1).toUpperCase()}${level.substring(1)} ${timestamp}: ${message}`;
});
const fileFormat = format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
fileFormatter
);
const logFilePath = path.isAbsolute(process.env.VSC_PYTHON_LOG_FILE)
? process.env.VSC_PYTHON_LOG_FILE
: path.join(EXTENSION_ROOT_DIR, process.env.VSC_PYTHON_LOG_FILE);
const logFileSink = new transports.File({
format: fileFormat,
filename: logFilePath,
handleExceptions: true
});
fileLogger.add(logFileSink);
}
/**
* What do we want to log.
* @export
* @enum {number}
*/
export enum LogOptions {
None = 0,
Arguments = 1,
ReturnValue = 2
}
// tslint:disable-next-line:no-any
function argsToLogString(args: any[]): string {
try {
return (args || [])
.map((item, index) => {
if (item === undefined) {
return `Arg ${index + 1}: undefined`;
}
if (item === null) {
return `Arg ${index + 1}: null`;
}
try {
if (item && item.fsPath) {
return `Arg ${index + 1}: <Uri:${item.fsPath}>`;
}
return `Arg ${index + 1}: ${JSON.stringify(item)}`;
} catch {
return `Arg ${index + 1}: <argument cannot be serialized for logging>`;
}
})
.join(', ');
} catch {
return '';
}
}
// tslint:disable-next-line:no-any
function returnValueToLogString(returnValue: any): string {
const returnValueMessage = 'Return Value: ';
if (returnValue === undefined) {
return `${returnValueMessage}undefined`;
}
if (returnValue === null) {
return `${returnValueMessage}null`;
}
try {
return `${returnValueMessage}${JSON.stringify(returnValue)}`;
} catch {
return `${returnValueMessage}<Return value cannot be serialized for logging>`;
}
}
export function traceVerbose(...args: any[]) {
log(LogLevel.Information, ...args);
}
export function traceError(...args: any[]) {
log(LogLevel.Error, ...args);
}
export function traceInfo(...args: any[]) {
log(LogLevel.Information, ...args);
}
export function traceWarning(...args: any[]) {
log(LogLevel.Warning, ...args);
}
export namespace traceDecorators {
export function verbose(message: string, options: LogOptions = LogOptions.Arguments | LogOptions.ReturnValue) {
return trace(message, options);
}
export function error(message: string) {
return trace(message, LogOptions.Arguments | LogOptions.ReturnValue, LogLevel.Error);
}
export function info(message: string) {
return trace(message);
}
export function warn(message: string) {
return trace(message, LogOptions.Arguments | LogOptions.ReturnValue, LogLevel.Warning);
}
}
function trace(message: string, options: LogOptions = LogOptions.None, logLevel?: LogLevel) {
// tslint:disable-next-line:no-function-expression no-any
return function(_: Object, __: string, descriptor: TypedPropertyDescriptor<any>) {
const originalMethod = descriptor.value;
// tslint:disable-next-line:no-function-expression no-any
descriptor.value = function(...args: any[]) {
const className = _ && _.constructor ? _.constructor.name : '';
// tslint:disable-next-line:no-any
function writeSuccess(elapsedTime: number, returnValue: any) {
if (logLevel === LogLevel.Error) {
return;
}
writeToLog(elapsedTime, returnValue);
}
function writeError(elapsedTime: number, ex: Error) {
writeToLog(elapsedTime, undefined, ex);
}
// tslint:disable-next-line:no-any
function writeToLog(elapsedTime: number, returnValue?: any, ex?: Error) {
const messagesToLog = [message];
messagesToLog.push(
`Class name = ${className}, completed in ${elapsedTime}ms, has a ${
returnValue ? 'truthy' : 'falsy'
} return value`
);
if ((options && LogOptions.Arguments) === LogOptions.Arguments) {
messagesToLog.push(argsToLogString(args));
}
if ((options & LogOptions.ReturnValue) === LogOptions.ReturnValue) {
messagesToLog.push(returnValueToLogString(returnValue));
}
if (ex) {
log(LogLevel.Error, messagesToLog.join(', '), ex);
sendTelemetryEvent('ERROR' as any, undefined, undefined, ex);
} else {
log(LogLevel.Information, messagesToLog.join(', '));
}
}
const timer = new StopWatch();
try {
// tslint:disable-next-line:no-invalid-this no-use-before-declare no-unsafe-any
const result = originalMethod.apply(this, args);
// If method being wrapped returns a promise then wait for it.
// tslint:disable-next-line:no-unsafe-any
if (result && typeof result.then === 'function' && typeof result.catch === 'function') {
// tslint:disable-next-line:prefer-type-cast
(result as Promise<void>)
.then(data => {
writeSuccess(timer.elapsedTime, data);
return data;
})
.catch(ex => {
writeError(timer.elapsedTime, ex);
});
} else {
writeSuccess(timer.elapsedTime, result);
}
return result;
} catch (ex) {
writeError(timer.elapsedTime, ex);
throw ex;
}
};
return descriptor;
};
}