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