UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

220 lines 8.18 kB
import { LoggerRingBuffer } from './logger-ring-buffer.js'; import { LoggerLevelName } from './logger-level-name.js'; import { LoggerUtil } from './logger-util.js'; import { LogMessageFormatType } from './log-message-format-type.js'; import { ClassicSingleLineLogMessageFormatter } from './classic-single-line-log-message-formatter.js'; import { NoneLogMessageFormatter } from './none-log-message-formatter.js'; import { StructuredJsonLogMessageFormatter } from './structured-json-log-message-formatter.js'; import { StringRatchet } from '../lang/string-ratchet.js'; import { SingleLineNoLevelLogMessageFormatter } from './single-line-no-level-log-message-formatter.js'; export class LoggerInstance { loggerInstanceName; _guid = Math.floor(Math.random() * 1_000_000); _loggerMeta; _ringBuffer; _formatter; _level; _handlerFunctionMap; _options; constructor(loggerInstanceName = 'default', inOptions) { this.loggerInstanceName = loggerInstanceName; this.options = inOptions; } levelIsEnabled(level) { return LoggerUtil.levelIsEnabled(level, this._level); } addPreProcessor(proc) { if (proc) { this._options.preProcessors = this._options.preProcessors || []; this._options.preProcessors.push(proc); } return Object.assign([], this._options.preProcessors); } get ringBuffer() { return this._ringBuffer; } dumpConfigurationIntoLog() { this.error('ERROR enabled'); this.warn('WARN enabled'); this.info('INFO enabled'); this.verbose('VERBOSE enabled'); this.debug('DEBUG enabled'); this.silly('SILLY enabled'); } dumpOptionsIntoLog() { this.info('Guid: %s Options: %j', this._guid, this.options); if (this?.options?.preProcessors?.length) { const labels = this.options.preProcessors.map((p) => StringRatchet.trimToNull(p.label()) || 'Unlabelled'); this.info('Preprocessors: %j', labels); } } get guid() { return this._guid; } changeRingBufferSize(newSize) { this._ringBuffer = null; if (newSize) { this._ringBuffer = new LoggerRingBuffer(newSize); this._options.ringBufferSize = newSize; } } updateTracePrefix(newValue) { this._options.trace = newValue; } get options() { return Object.assign({}, this._options); } set options(newOpts) { this._options = Object.assign({}, newOpts); if (this._options.ringBufferSize) { this._ringBuffer = new LoggerRingBuffer(this._options.ringBufferSize); } switch (this._options.formatType) { case LogMessageFormatType.None: this._formatter = new NoneLogMessageFormatter(); break; case LogMessageFormatType.StructuredJson: this._formatter = new StructuredJsonLogMessageFormatter(); break; case LogMessageFormatType.SingleLineNoLevel: this._formatter = new SingleLineNoLevelLogMessageFormatter(); break; default: this._formatter = new ClassicSingleLineLogMessageFormatter(); break; } this._level = this._options.initialLevel; this._handlerFunctionMap = LoggerUtil.handlerFunctionMap(this._options.outputFunction); const oldId = this._loggerMeta ? this._loggerMeta.loggerInstanceId : null; this._loggerMeta = { options: this._options, loggerInstanceName: this.loggerInstanceName, loggerInstanceId: oldId || StringRatchet.createRandomHexString(8), }; } get level() { return this._level; } set level(newLevel) { if (!newLevel) { throw new Error('Cannot set level to null/undefined'); } this._level = newLevel; } consoleLogPassThru(level, ...input) { if (LoggerUtil.levelIsEnabled(level, this._level)) { let passThruPrefix = this._options.trace || ''; passThruPrefix += '[' + level + '] '; input.unshift(passThruPrefix); const fn = this._handlerFunctionMap.get(level) || LoggerUtil.defaultHandlerFunction; fn(...input); } } createLogMessage(level, params, format, ...input) { const rval = { lvl: level, timestamp: Date.now(), messageSource: format, subsVars: input, params: params, }; return rval; } formatMessage(msg) { const rval = msg ? this._formatter.formatMessage(msg, this._loggerMeta) : null; return rval; } formatMessages(msgs) { const rval = (msgs || []).map((m) => this.formatMessage(m)).filter((m) => !!m); return rval; } recordMessageBuilder(msgBuild) { return this.recordMessage(msgBuild.toMessage()); } recordMessage(inMsg) { let rval = null; if (LoggerUtil.levelIsEnabled(inMsg.lvl, this._level)) { let msg = Object.assign({}, inMsg); if (this._options.preProcessors?.length) { for (const proc of this._options.preProcessors) { msg = proc.process(msg); } } rval = this.formatMessage(msg); if (rval) { const fn = this._handlerFunctionMap.get(msg.lvl) || LoggerUtil.defaultHandlerFunction; fn(rval); if (this._ringBuffer) { this._ringBuffer.addToRingBuffer(msg); } } } else { } return rval; } error(format, ...input) { const msg = this.createLogMessage(LoggerLevelName.error, {}, format, ...input); return this.recordMessage(msg); } errorP(...input) { this.consoleLogPassThru(LoggerLevelName.error, ...input); } warn(format, ...input) { const msg = this.createLogMessage(LoggerLevelName.warn, {}, format, ...input); return this.recordMessage(msg); } warnP(...input) { this.consoleLogPassThru(LoggerLevelName.warn, ...input); } info(format, ...input) { const msg = this.createLogMessage(LoggerLevelName.info, {}, format, ...input); return this.recordMessage(msg); } infoP(...input) { this.consoleLogPassThru(LoggerLevelName.info, ...input); } verbose(format, ...input) { const msg = this.createLogMessage(LoggerLevelName.verbose, {}, format, ...input); return this.recordMessage(msg); } verboseP(...input) { this.consoleLogPassThru(LoggerLevelName.verbose, ...input); } debug(format, ...input) { const msg = this.createLogMessage(LoggerLevelName.debug, {}, format, ...input); return this.recordMessage(msg); } debugP(...input) { this.consoleLogPassThru(LoggerLevelName.debug, ...input); } silly(format, ...input) { const msg = this.createLogMessage(LoggerLevelName.silly, {}, format, ...input); return this.recordMessage(msg); } sillyP(...input) { this.consoleLogPassThru(LoggerLevelName.silly, ...input); } logByLevel(level, format, ...input) { const msg = this.createLogMessage(level, {}, format, ...input); this.recordMessage(msg); } importMessages(msgs, prefixIn = '', addTimestamp = true) { const prefix = prefixIn || ''; if (msgs && msgs.length > 0) { this.silly('Received import data : %d msgs', msgs.length); msgs.forEach((m) => { if (m.messageSource) { let mOut = prefix; if (addTimestamp) { const ts = String(new Date()).substring(15, 24); mOut += ' (' + ts + ') : '; mOut += m.messageSource; } this.logByLevel(m.lvl, mOut); } }); } } } //# sourceMappingURL=logger-instance.js.map