UNPKG

@livy/console-handler

Version:
52 lines (51 loc) 1.75 kB
import { ConsoleFormatter } from '@livy/console-formatter'; import { SeverityMap } from '@livy/contracts/lib/log-level.mjs'; import * as environment from '@livy/util/lib/environment.mjs'; import { AbstractSyncFormattingProcessingHandler } from '@livy/util/lib/handlers/abstract-formatting-processing-handler.mjs'; /** * Writes log records to the terminal */ export class ConsoleHandler extends AbstractSyncFormattingProcessingHandler { constructor({ console, formatter, ...options } = {}) { super(options); // istanbul ignore next: Environment is hard to test if (typeof console === 'undefined') { if (environment.isNodeJs) { console = global.console; } else if (environment.isBrowser) { console = self.console; } else if (typeof globalThis === 'object' && typeof globalThis.console === 'object') { console = globalThis.console; } else { // eslint-disable-next-line unicorn/prefer-type-error throw new Error('Could not find a global console object'); } } this.console = console; this.explicitFormatter = formatter; } /** * @inheritdoc */ writeSync(record, formatted) { if (record.severity <= SeverityMap.error) { this.console.error('%s', formatted); } else if (record.severity === SeverityMap.warning) { this.console.warn('%s', formatted); } else { this.console.log('%s', formatted); } } /** * @inheritdoc */ getDefaultFormatter() { return new ConsoleFormatter(); } }