microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/log/loggers.ts
85lines · 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 | /** |
| 5 | * Formatter for the Output channel. |
| 6 | */ |
| 7 | import {LogHelper, LogLevel} from "./logHelper"; |
| 8 | |
| 9 | export interface ILogger { |
| 10 | logMessage: (message: string, formatMessage?: boolean) => void; |
| 11 | logError: (errorMessage: string, error?: any, logStack?: boolean) => void; |
| 12 | logInternalMessage: (logLevel: LogLevel, message: string) => void; |
| 13 | setFocusOnLogChannel: () => void; |
| 14 | } |
| 15 | |
| 16 | export class ConsoleLogger implements ILogger { |
| 17 | public logMessage(message: string, formatMessage: boolean = true ) { |
| 18 | console.log(formatMessage ? |
| 19 | this.getFormattedMessage(message) : |
| 20 | message); |
| 21 | } |
| 22 | |
| 23 | public logError(errorMessage: string, error?: any, logStack: boolean = true) { |
| 24 | console.error(this.getFormattedMessage(errorMessage)); |
| 25 | |
| 26 | // Print the error stack if necessary |
| 27 | if (logStack && error && (<Error>error).stack) { |
| 28 | console.error(`Stack: ${(<Error>error).stack}`); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public logInternalMessage(logLevel: LogLevel, message: string) { |
| 33 | this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false); |
| 34 | } |
| 35 | |
| 36 | public setFocusOnLogChannel() { |
| 37 | // Do nothing - console takes focus automatically upon logging |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | private getFormattedMessage(message: string) { |
| 42 | return `${LogHelper.MESSAGE_TAG} ${message}\n`; |
| 43 | } |
| 44 | |
| 45 | private getFormattedInternalMessage(logLevel: LogLevel, message: string) { |
| 46 | return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}\n`); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | export class StreamLogger implements ILogger { |
| 51 | private stream: NodeJS.WritableStream; |
| 52 | constructor(stream: NodeJS.WritableStream) { |
| 53 | this.stream = stream; |
| 54 | } |
| 55 | public logMessage(message: string, formatMessage: boolean = true ) { |
| 56 | this.stream.write(formatMessage ? |
| 57 | this.getFormattedMessage(message) : |
| 58 | message); |
| 59 | } |
| 60 | |
| 61 | public logError(errorMessage: string, error?: any, logStack: boolean = true) { |
| 62 | this.logMessage(errorMessage); |
| 63 | |
| 64 | if (logStack && error && (<Error>error).stack) { |
| 65 | this.logMessage(`Stack: ${(<Error>error).stack}`, /* formatMessage */ false); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public logInternalMessage(logLevel: LogLevel, message: string) { |
| 70 | this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false); |
| 71 | } |
| 72 | |
| 73 | public getFormattedMessage(message: string) { |
| 74 | return `${LogHelper.MESSAGE_TAG} ${message}\n`; |
| 75 | } |
| 76 | |
| 77 | public getFormattedInternalMessage(logLevel: LogLevel, message: string) { |
| 78 | return (`${LogHelper.INTERNAL_TAG} [${logLevel}] ${message}\n`); |
| 79 | } |
| 80 | |
| 81 | public setFocusOnLogChannel() { |
| 82 | // Do nothing |
| 83 | return; |
| 84 | } |
| 85 | } |