UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

251 lines • 8.57 kB
// ***************************************************************************** // Copyright (C) 2017 Ericsson and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { __decorate } from "tslib"; import { inject, injectable, optional, postConstruct } from 'inversify'; import { LoggerWatcher } from './logger-watcher.js'; import { ILoggerServer, LogLevel, ConsoleLogger, rootLoggerName } from './logger-protocol.js'; import { LoggerSanitizer } from './logger-sanitizer.js'; /* eslint-disable @typescript-eslint/no-explicit-any */ export { LogLevel, rootLoggerName }; /* This is to be initialized from container composition root. It can be used outside of the inversify context. */ export let logger; /** * Counterpart of the `#setRootLogger(ILogger)`. Restores the `console.xxx` bindings to the original one. * Invoking has no side-effect if `setRootLogger` was not called before. Multiple function invocation has * no side-effect either. */ export function unsetRootLogger() { if (logger !== undefined) { ConsoleLogger.reset(); logger = undefined; } } export function setRootLogger(aLogger) { logger = aLogger; const log = (logLevel, message, ...optionalParams) => logger.log(logLevel, message, ...optionalParams); console.error = log.bind(undefined, LogLevel.ERROR); console.warn = log.bind(undefined, LogLevel.WARN); console.info = log.bind(undefined, LogLevel.INFO); console.debug = log.bind(undefined, LogLevel.DEBUG); console.trace = log.bind(undefined, LogLevel.TRACE); console.log = log.bind(undefined, LogLevel.INFO); } /** Shared no-op {@link Log} returned for a disabled level so that callers neither block nor emit. */ const nullLogger = () => { }; export const LoggerFactory = Symbol('LoggerFactory'); export const LoggerName = Symbol('LoggerName'); export const ILogger = Symbol('ILogger'); let Logger = class Logger { constructor() { this.cache = new Map(); } init() { if (this.name !== rootLoggerName) { /* Creating a child logger. */ this.created = this.server.child(this.name); } else { /* Creating the root logger (it already exists at startup). */ this.created = Promise.resolve(); } /* Fetch the log level so it's cached in the frontend. */ this._logLevel = this.created.then(_ => this.server.getLogLevel(this.name)); /* Update the log level if it changes in the backend. */ this.loggerWatcher.onLogLevelChanged(event => { this.created.then(() => { if (event.loggerName === this.name) { this._logLevel = Promise.resolve(event.newLogLevel); } }); }); /* Refetch log level if overall config in backend changed. */ this.loggerWatcher.onLogConfigChanged(() => { this._logLevel = this.created.then(_ => this.server.getLogLevel(this.name)); }); } setLogLevel(logLevel) { return new Promise(resolve => { this.created.then(() => { this._logLevel.then(oldLevel => { this.server.setLogLevel(this.name, logLevel).then(() => { this._logLevel = Promise.resolve(logLevel); resolve(); }); }); }); }); } getLogLevel() { return this._logLevel; } isEnabled(logLevel) { return this._logLevel.then(level => logLevel >= level); } ifEnabled(logLevel) { return new Promise(resolve => this.isEnabled(logLevel).then(enabled => { if (enabled) { resolve(); } })); } log(logLevel, arg2, ...params) { return this.getLog(logLevel).then(log => { if (log === nullLogger) { return; } if (typeof arg2 === 'function') { const loggable = arg2; loggable(log); } else if (arg2) { log(arg2, ...params); } }); } getLog(logLevel) { return this.isEnabled(logLevel).then(enabled => { if (!enabled) { return nullLogger; } return this.created.then(() => (message, ...params) => this.server.log(this.name, logLevel, this.format(message), params.map(p => this.format(p)))); }); } format(value) { switch (typeof value) { case 'object': if (value instanceof Error) { return this.sanitize(value.stack || value.toString()); } else if (!value) { return value; } else if (Array.isArray(value)) { return value.map(item => this.format(item)); } else { try { const stringified = JSON.stringify(value); const sanitized = this.sanitize(stringified); try { return JSON.parse(sanitized); } catch { return sanitized; } } catch { return value; } } case 'string': return this.sanitize(value); default: return value; } } sanitize(message) { return this.sanitizer?.sanitize(message) ?? message; } isTrace() { return this.isEnabled(LogLevel.TRACE); } ifTrace() { return this.ifEnabled(LogLevel.TRACE); } trace(arg, ...params) { return this.log(LogLevel.TRACE, arg, ...params); } isDebug() { return this.isEnabled(LogLevel.DEBUG); } ifDebug() { return this.ifEnabled(LogLevel.DEBUG); } debug(arg, ...params) { return this.log(LogLevel.DEBUG, arg, ...params); } isInfo() { return this.isEnabled(LogLevel.INFO); } ifInfo() { return this.ifEnabled(LogLevel.INFO); } info(arg, ...params) { return this.log(LogLevel.INFO, arg, ...params); } isWarn() { return this.isEnabled(LogLevel.WARN); } ifWarn() { return this.ifEnabled(LogLevel.WARN); } warn(arg, ...params) { return this.log(LogLevel.WARN, arg, ...params); } isError() { return this.isEnabled(LogLevel.ERROR); } ifError() { return this.ifEnabled(LogLevel.ERROR); } error(arg, ...params) { return this.log(LogLevel.ERROR, arg, ...params); } isFatal() { return this.isEnabled(LogLevel.FATAL); } ifFatal() { return this.ifEnabled(LogLevel.FATAL); } fatal(arg, ...params) { return this.log(LogLevel.FATAL, arg, ...params); } child(name) { const existing = this.cache.get(name); if (existing) { return existing; } else { const child = this.factory(name); this.cache.set(name, child); return child; } } }; __decorate([ inject(ILoggerServer) ], Logger.prototype, "server", void 0); __decorate([ inject(LoggerWatcher) ], Logger.prototype, "loggerWatcher", void 0); __decorate([ inject(LoggerFactory) ], Logger.prototype, "factory", void 0); __decorate([ inject(LoggerName) ], Logger.prototype, "name", void 0); __decorate([ inject(LoggerSanitizer), optional() ], Logger.prototype, "sanitizer", void 0); __decorate([ postConstruct() ], Logger.prototype, "init", null); Logger = __decorate([ injectable() ], Logger); export { Logger }; //# sourceMappingURL=logger.js.map