forked from microsoft/vscode-python
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monkeypatch console.* calls on CI if we are asked to (microsoft#11897)
* Move console.* monkeypatching to src/test/testLogging.ts * Use monkeypatching in all tests launched using testBootstrap.ts * Use monkeypatching in single workspace and multiroot tests * News entry * Don't do monkeypatching for smoketests * Modify gulp task to not delete 'out/client/logging' directory * Undo smoke test ccheck * Import only from ./out/client/logging in test logger * Added comment * More corrections * Add comments * Correct smoke tests * Oops
- Loading branch information
1 parent
9bedf2d
commit fe1a91a
Showing
12 changed files
with
125 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Monkeypatch `console.*` calls to the logger only in CI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
// IMPORTANT: This file should only be importing from the '../client/logging' directory, as we | ||
// delete everything in '../client' except for '../client/logging' before running smoke tests. | ||
|
||
import { LogLevel } from '../client/logging/levels'; | ||
import { configureLogger, createLogger, getPreDefinedConfiguration, logToAll } from '../client/logging/logger'; | ||
|
||
const isCI = process.env.TRAVIS === 'true' || process.env.TF_BUILD !== undefined; | ||
const monkeyPatchLogger = createLogger(); | ||
|
||
export function initializeLogger() { | ||
const config = getPreDefinedConfiguration(); | ||
if (isCI && process.env.VSC_PYTHON_LOG_FILE) { | ||
delete config.console; | ||
// This is a separate logger that matches our config but | ||
// does not do any console logging. | ||
configureLogger(monkeyPatchLogger, config); | ||
// Send console.*() to the non-console loggers. | ||
monkeypatchConsole(); | ||
} | ||
} | ||
|
||
/** | ||
* 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). | ||
*/ | ||
function monkeypatchConsole() { | ||
// The logging "streams" (methods) of the node console. | ||
const streams = ['log', 'error', 'warn', 'info', 'debug', 'trace']; | ||
const levels: { [key: string]: LogLevel } = { | ||
error: LogLevel.Error, | ||
warn: LogLevel.Warn | ||
}; | ||
// tslint:disable-next-line:no-any | ||
const consoleAny: any = console; | ||
for (const stream of streams) { | ||
// Using symbols guarantee the properties will be unique & prevents | ||
// clashing with names other code/library may create or have created. | ||
// We could use a closure but it's a bit trickier. | ||
const sym = Symbol.for(stream); | ||
consoleAny[sym] = consoleAny[stream]; | ||
// tslint:disable-next-line: no-function-expression | ||
consoleAny[stream] = function () { | ||
const args = Array.prototype.slice.call(arguments); | ||
const fn = consoleAny[sym]; | ||
fn(...args); | ||
const level = levels[stream] || LogLevel.Info; | ||
logToAll([monkeyPatchLogger], level, args); | ||
}; | ||
} | ||
} |