import { debugLevel, dropConsoleOnProduction } from "../config"; export enum DebugLevel { Trace = "trace", Debug = "debug", Log = "log", Warning = "warn", Error = "error", } export class Logger { logLevel: boolean | "all" | DebugLevel[]; constructor(logLevel: any, options: { dropConsoleOnProduction: boolean }) { this.logLevel = logLevel; if (typeof console.log == "undefined") { console.log = function () {}; } this.trace = this.noop; this.debug = this.noop; this.log = this.noop; this.warn = this.noop; this.error = this.noop; if (process.env.NODE_ENV === "production" && options.dropConsoleOnProduction === true) { // drop_console by webpack terserOptions } else { if (this.logLevel === true || this.logLevel === "all") { this.trace = console.trace.bind(console); this.debug = console.debug.bind(console); this.log = console.log.bind(console); this.warn = console.warn.bind(console); this.error = console.error.bind(console); } else if (Array.isArray(this.logLevel)) { for (let d of this.logLevel) { switch (d) { case "trace": this.trace = console.trace.bind(console); break; case "debug": this.debug = console.debug.bind(console); break; case "log": this.log = console.log.bind(console); break; case "warn": this.warn = console.warn.bind(console); break; case "error": this.error = console.error.bind(console); break; default: console.error( "Unknown debugging option '" + d + "' (supported: 'trace', 'debug', 'vdebug', 'log', warn', 'error')" ); break; } } } } } noop(...args: any[]) { // } trace(...args: any[]) { // } debug(...args: any[]) { // } log(...args: any[]) { // } warn(...args: any[]) { // } error(...args: any[]) { // } } export const logger = new Logger(debugLevel, { dropConsoleOnProduction });