|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +import { isCI, isTestExecution } from '../common/constants'; |
| 6 | +import { CallInfo } from '../common/utils/decorators'; |
| 7 | +import { LogLevel } from './levels'; |
| 8 | +import { configureLogger, createLogger, ILogger, LoggerConfig, logToAll } from './logger'; |
| 9 | +import { createTracingDecorator, LogInfo, TraceOptions, tracing as _tracing } from './trace'; |
| 10 | +import { Arguments } from './util'; |
| 11 | + |
| 12 | +const globalLogger = createLogger(); |
| 13 | +initialize(); |
| 14 | + |
| 15 | +/** |
| 16 | + * Initialize the logger. |
| 17 | + * |
| 18 | + * For console we do two things here: |
| 19 | + * - Anything written to the logger will be displayed in the console |
| 20 | + * window as well This is the behavior of the extension when running |
| 21 | + * it. When running tests on CI, we might not want this behavior, as |
| 22 | + * it'll pollute the test output with logging (as mentioned this is |
| 23 | + * optional). Messages logged using our logger will be prefixed with |
| 24 | + * `Python Extension: ....` for console window. This way, its easy |
| 25 | + * to identify messages specific to the python extension. |
| 26 | + * - Monkey patch the console.log and similar methods to send messages |
| 27 | + * to the file logger. When running UI tests or similar, and we want |
| 28 | + * to see everything that was dumped into `console window`, then we |
| 29 | + * need to hijack the console logger. To do this we need to monkey |
| 30 | + * patch the console methods. This is optional (generally done when |
| 31 | + * running tests on CI). |
| 32 | + * |
| 33 | + * For the logfile: |
| 34 | + * - we send all logging output to a log file. We log to the file |
| 35 | + * only if a file has been specified as an env variable. Currently |
| 36 | + * this is setup on CI servers. |
| 37 | + */ |
| 38 | +function initialize() { |
| 39 | + const config: LoggerConfig = {}; |
| 40 | + let nonConsole = false; |
| 41 | + |
| 42 | + // Do not log to console if running tests and we're not |
| 43 | + // asked to do so. |
| 44 | + if (!isTestExecution() || process.env.VSC_PYTHON_FORCE_LOGGING) { |
| 45 | + config.console = {}; |
| 46 | + // In CI there's no need for the label. |
| 47 | + if (!isCI) { |
| 48 | + config.console.label = 'Python Extension:'; |
| 49 | + } |
| 50 | + } |
| 51 | + if (process.env.VSC_PYTHON_LOG_FILE) { |
| 52 | + config.file = { |
| 53 | + logfile: process.env.VSC_PYTHON_LOG_FILE |
| 54 | + }; |
| 55 | + nonConsole = true; |
| 56 | + } |
| 57 | + configureLogger(globalLogger, config); |
| 58 | + |
| 59 | + if (isCI && nonConsole) { |
| 60 | + delete config.console; |
| 61 | + // Send console.*() to the non-console loggers. |
| 62 | + monkeypatchConsole( |
| 63 | + // This is a separate logger that matches our config but |
| 64 | + // does not do any console logging. |
| 65 | + createLogger(config) |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Emit a log message derived from the args to all enabled transports. |
| 71 | +export function log(logLevel: LogLevel, ...args: Arguments) { |
| 72 | + logToAll([globalLogger], logLevel, args); |
| 73 | +} |
| 74 | + |
| 75 | +// tslint:disable-next-line:no-any |
| 76 | +export function logVerbose(...args: any[]) { |
| 77 | + log(LogLevel.Info, ...args); |
| 78 | +} |
| 79 | + |
| 80 | +// tslint:disable-next-line:no-any |
| 81 | +export function logError(...args: any[]) { |
| 82 | + log(LogLevel.Error, ...args); |
| 83 | +} |
| 84 | + |
| 85 | +// tslint:disable-next-line:no-any |
| 86 | +export function logInfo(...args: any[]) { |
| 87 | + log(LogLevel.Info, ...args); |
| 88 | +} |
| 89 | + |
| 90 | +// tslint:disable-next-line:no-any |
| 91 | +export function logWarning(...args: any[]) { |
| 92 | + log(LogLevel.Warn, ...args); |
| 93 | +} |
| 94 | + |
| 95 | +// This is like a "context manager" that logs tracing info. |
| 96 | +export function tracing<T>(info: LogInfo, run: () => T, call?: CallInfo): T { |
| 97 | + return _tracing([globalLogger], info, run, call); |
| 98 | +} |
| 99 | + |
| 100 | +export namespace traceDecorators { |
| 101 | + const DEFAULT_OPTS: TraceOptions = TraceOptions.Arguments | TraceOptions.ReturnValue; |
| 102 | + |
| 103 | + export function verbose(message: string, opts: TraceOptions = DEFAULT_OPTS) { |
| 104 | + return createTracingDecorator([globalLogger], { message, opts }); |
| 105 | + } |
| 106 | + export function error(message: string) { |
| 107 | + const opts = DEFAULT_OPTS; |
| 108 | + const level = LogLevel.Error; |
| 109 | + return createTracingDecorator([globalLogger], { message, opts, level }); |
| 110 | + } |
| 111 | + export function info(message: string) { |
| 112 | + const opts = TraceOptions.None; |
| 113 | + return createTracingDecorator([globalLogger], { message, opts }); |
| 114 | + } |
| 115 | + export function warn(message: string) { |
| 116 | + const opts = DEFAULT_OPTS; |
| 117 | + const level = LogLevel.Warn; |
| 118 | + return createTracingDecorator([globalLogger], { message, opts, level }); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Ensure that the console functions are bound before monkeypatching. |
| 123 | +import './transports'; |
| 124 | + |
| 125 | +/** |
| 126 | + * What we're doing here is monkey patching the console.log so we can |
| 127 | + * send everything sent to console window into our logs. This is only |
| 128 | + * required when we're directly writing to `console.log` or not using |
| 129 | + * our `winston logger`. This is something we'd generally turn on, only |
| 130 | + * on CI so we can see everything logged to the console window |
| 131 | + * (via the logs). |
| 132 | + */ |
| 133 | +function monkeypatchConsole(logger: ILogger) { |
| 134 | + // The logging "streams" (methods) of the node console. |
| 135 | + const streams = ['log', 'error', 'warn', 'info', 'debug', 'trace']; |
| 136 | + const levels: { [key: string]: LogLevel } = { |
| 137 | + error: LogLevel.Error, |
| 138 | + warn: LogLevel.Warn |
| 139 | + }; |
| 140 | + // tslint:disable-next-line:no-any |
| 141 | + const consoleAny: any = console; |
| 142 | + for (const stream of streams) { |
| 143 | + // Using symbols guarantee the properties will be unique & prevents |
| 144 | + // clashing with names other code/library may create or have created. |
| 145 | + // We could use a closure but it's a bit trickier. |
| 146 | + const sym = Symbol.for(stream); |
| 147 | + consoleAny[sym] = consoleAny[stream]; |
| 148 | + // tslint:disable-next-line: no-function-expression |
| 149 | + consoleAny[stream] = function () { |
| 150 | + const args = Array.prototype.slice.call(arguments); |
| 151 | + const fn = consoleAny[sym]; |
| 152 | + fn(...args); |
| 153 | + const level = levels[stream] || LogLevel.Info; |
| 154 | + logToAll([logger], level, args); |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments