1/* 2 * logger.ts 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 16import { getenv } from './environment'; 17import { Logger, LogLevel, logLevel, showDiagnosticsOutput } from './logger'; 18 19/** 20 * A Logger using console.log() 21 */ 22export class ConsoleLogger implements Logger { 23 24 logError(err: Error): void { 25 if (logLevel() >= LogLevel.ERR) { 26 console.log(err); 27 } 28 } 29 30 logErrorMessage(message: string): void { 31 if (logLevel() >= LogLevel.ERR) { 32 console.log(message); 33 } 34 } 35 36 logWarning(warning: string): void { 37 if (logLevel() >= LogLevel.WARN) { 38 console.log(warning); 39 } 40 } 41 42 logInfo(message: string): void { 43 if (logLevel() >= LogLevel.INFO) { 44 console.log(message); 45 } 46 } 47 48 logDebug(message: string): void { 49 if (logLevel() >= LogLevel.DEBUG) { 50 console.log(message); 51 } 52 } 53 54 logDiagnostic(message: string): void { 55 if (showDiagnosticsOutput()) { 56 console.log(message); 57 } 58 } 59 60 logDiagnosticEnvVar(name: string): void { 61 if (showDiagnosticsOutput()) { 62 const value = getenv(name); 63 if (value) { 64 this.logDiagnostic(` . ${name} = ${value}`); 65 } 66 } 67 } 68}