microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/log/ConsoleLogger.ts
46lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {ILogger, LogLevel, LogHelper} from "./LogHelper"; |
| 5 | |
| 6 | export class ConsoleLogger implements ILogger { |
| 7 | public log(message: string, level: LogLevel): void { |
| 8 | if (LogHelper.LOG_LEVEL === LogLevel.None) { |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | if (level >= LogHelper.LOG_LEVEL) { |
| 13 | message = ConsoleLogger.getFormattedMessage(message, level); |
| 14 | console.log(message); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public info(message: string): void { |
| 19 | this.log(message, LogLevel.Info); |
| 20 | } |
| 21 | |
| 22 | public warning(message: string, logStack = false): void { |
| 23 | this.log(message, LogLevel.Warning); |
| 24 | } |
| 25 | |
| 26 | public error(errorMessage: string, error?: Error, logStack: boolean = true) { |
| 27 | console.error(ConsoleLogger.getFormattedMessage(errorMessage, LogLevel.Error)); |
| 28 | |
| 29 | // Print the error stack if necessary |
| 30 | if (logStack && error && (<Error>error).stack) { |
| 31 | console.error(`Stack: ${(<Error>error).stack}`); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public debug(message: string): void { |
| 36 | this.log(message, LogLevel.Debug); |
| 37 | } |
| 38 | |
| 39 | public logStream(data: Buffer, stream: NodeJS.WritableStream) { |
| 40 | stream.write(data.toString()); |
| 41 | } |
| 42 | |
| 43 | protected static getFormattedMessage(message: string, level: LogLevel): string { |
| 44 | return `[${LogLevel[level]}] ${message}\n`; |
| 45 | } |
| 46 | } |
| 47 | |