UNPKG

@theia/core

Version:

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

90 lines • 3.59 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"; // This is the console logger itself, so it cannot log through an ILogger. /* eslint-disable @theia/named-logger-check */ import { inject, injectable, postConstruct } from 'inversify'; import { LoggerWatcher } from '../common/logger-watcher.js'; import { LogLevelCliContribution } from './logger-cli-contribution.js'; import { ConsoleLogger, rootLoggerName } from '../common/logger-protocol.js'; import { format } from 'util'; import { EOL } from 'os'; import * as fs from 'fs'; let ConsoleLoggerServer = class ConsoleLoggerServer { init() { this.setLogLevel(rootLoggerName, this.cli.defaultLogLevel); for (const name of Object.keys(this.cli.logLevels)) { this.setLogLevel(name, this.cli.logLevels[name]); } this.cli.onLogConfigChanged(() => { this.client?.onLogConfigChanged(); }); } async setLogLevel(name, newLogLevel) { const event = { loggerName: name, newLogLevel }; if (this.client !== undefined) { this.client.onLogLevelChanged(event); } } async getLogLevel(name) { return this.cli.logLevelFor(name); } /* eslint-disable @typescript-eslint/no-explicit-any */ async log(name, logLevel, message, params) { const configuredLogLevel = await this.getLogLevel(name); if (logLevel >= configuredLogLevel) { const fullMessage = ConsoleLogger.log(name, logLevel, message, params); this.logToFile(fullMessage, params); } } logToFile(message, params) { if (this.cli.logFile && !this.logFileStream) { this.logFileStream = fs.createWriteStream(this.cli.logFile, { flags: 'a' }); // Only log errors once to avoid spamming the console this.logFileStream.once('error', error => { console.error(`Error writing to log file ${this.cli.logFile}`, error); }); } if (this.logFileStream) { const formatted = format(message, ...params) + EOL; this.logFileStream.write(formatted); } } async child(name) { this.setLogLevel(name, this.cli.logLevelFor(name)); } dispose() { } setClient(client) { this.client = client; } }; __decorate([ inject(LoggerWatcher) ], ConsoleLoggerServer.prototype, "watcher", void 0); __decorate([ inject(LogLevelCliContribution) ], ConsoleLoggerServer.prototype, "cli", void 0); __decorate([ postConstruct() ], ConsoleLoggerServer.prototype, "init", null); ConsoleLoggerServer = __decorate([ injectable() ], ConsoleLoggerServer); export { ConsoleLoggerServer }; //# sourceMappingURL=console-logger-server.js.map