@bitblit/ratchet-common
Version:
Common tools for general use
157 lines • 6.1 kB
JavaScript
import { LoggerInstance } from './logger-instance.js';
import { LogMessageFormatType } from './log-message-format-type.js';
import { LoggerLevelName } from './logger-level-name.js';
import { LoggerOutputFunction } from './logger-output-function.js';
import { GlobalRatchet } from '../lang/global-ratchet.js';
export class Logger {
static LOGGER_INSTANCE_MAP_GLOBAL_KEY = 'RATCHET_GLOBAL_LOGGER_MAP_V01';
static DEFAULT_OPTIONS = {
initialLevel: LoggerLevelName.info,
formatType: LogMessageFormatType.ClassicSingleLine,
trace: null,
globalVars: {},
outputFunction: LoggerOutputFunction.Console,
ringBufferSize: 0,
preProcessors: [],
};
static applyDefaultsToOptions(input) {
const rval = input || {};
rval.initialLevel = rval.initialLevel ?? Logger.DEFAULT_OPTIONS.initialLevel;
rval.formatType = rval.formatType ?? Logger.DEFAULT_OPTIONS.formatType;
rval.trace = rval.trace ?? Logger.DEFAULT_OPTIONS.trace;
rval.globalVars = rval.globalVars ?? Logger.DEFAULT_OPTIONS.globalVars;
rval.outputFunction = rval.outputFunction ?? Logger.DEFAULT_OPTIONS.outputFunction;
rval.ringBufferSize = rval.ringBufferSize ?? Logger.DEFAULT_OPTIONS.ringBufferSize;
return rval;
}
static loggerInstances() {
GlobalRatchet.fetchGlobalVarsRecord(false);
let rval = GlobalRatchet.fetchGlobalVar(Logger.LOGGER_INSTANCE_MAP_GLOBAL_KEY);
if (!rval) {
rval = new Map();
GlobalRatchet.setGlobalVar(Logger.LOGGER_INSTANCE_MAP_GLOBAL_KEY, rval);
}
return rval;
}
static changeDefaultOptions(input, retroactive) {
if (!input || !input.initialLevel || !input.formatType) {
throw new Error('Default options must be non-null, and provide initial level and format type');
}
Logger.DEFAULT_OPTIONS = Object.assign({}, input);
if (retroactive) {
Array.from(Logger.loggerInstances().values()).forEach((li) => (li.options = input));
}
}
static getLogger(loggerName = 'default', inOptions) {
let inst = Logger.loggerInstances().get(loggerName);
if (!inst) {
const options = Logger.applyDefaultsToOptions(inOptions);
inst = new LoggerInstance(loggerName, options);
Logger.loggerInstances().set(loggerName, inst);
}
return inst;
}
static recordMessageBuilder(msgBuild) {
return Logger.getLogger().recordMessageBuilder(msgBuild);
}
static levelIsEnabled(level) {
return Logger.getLogger().levelIsEnabled(level);
}
static recordMessage(msg) {
return Logger.getLogger().recordMessage(msg);
}
static formatMessages(msgs) {
return Logger.getLogger().formatMessages(msgs);
}
static updateTracePrefix(newValue) {
return Logger.getLogger().updateTracePrefix(newValue);
}
static changeRingBufferSize(newValue) {
return Logger.getLogger().changeRingBufferSize(newValue);
}
static getRingBuffer() {
return Logger.getLogger().ringBuffer;
}
static dumpConfigurationIntoLog() {
return Logger.getLogger().dumpConfigurationIntoLog();
}
static dumpOptionsIntoLog() {
return Logger.getLogger().dumpOptionsIntoLog();
}
static getLevel() {
return Logger.getLogger().level;
}
static setLevel(newLevel) {
Logger.getLogger().level = newLevel;
}
static ringBufferOnlyMode(ringBufferSize = 1000, initialLevel = LoggerLevelName.silly) {
const newOptions = Logger.applyDefaultsToOptions({
ringBufferSize: ringBufferSize,
outputFunction: LoggerOutputFunction.Disabled,
formatType: LogMessageFormatType.SingleLineNoLevel,
initialLevel: initialLevel,
});
Logger.changeDefaultOptions(newOptions, true);
}
static getOptions() {
return Logger.getLogger().options;
}
static getMessages(inStartFrom = null, clear = false, reverseSort = false) {
const buf = Logger.getLogger().ringBuffer;
return buf ? buf.getMessages(inStartFrom, clear, reverseSort) : null;
}
static consoleLogPassThru(level, ...input) {
return Logger.getLogger().consoleLogPassThru(level, ...input);
}
static error(format, ...input) {
return Logger.getLogger().error(format, ...input);
}
static errorP(...input) {
return Logger.getLogger().errorP(...input);
}
static warn(format, ...input) {
return Logger.getLogger().warn(format, ...input);
}
static warnP(...input) {
return Logger.getLogger().warnP(...input);
}
static info(format, ...input) {
return Logger.getLogger().info(format, ...input);
}
static infoP(...input) {
return Logger.getLogger().infoP(...input);
}
static verbose(format, ...input) {
return Logger.getLogger().verbose(format, ...input);
}
static verboseP(...input) {
return Logger.getLogger().verboseP(...input);
}
static debug(format, ...input) {
return Logger.getLogger().debug(format, ...input);
}
static debugP(...input) {
return Logger.getLogger().debugP(...input);
}
static silly(format, ...input) {
return Logger.getLogger().silly(format, ...input);
}
static sillyP(...input) {
return Logger.getLogger().sillyP(...input);
}
static takeSnapshot() {
const buf = Logger.getLogger().ringBuffer;
return buf ? buf.takeSnapshot() : null;
}
static logByLevel(level, format, ...input) {
return Logger.getLogger().logByLevel(level, format, ...input);
}
static importMessages(msgs, prefixIn = '', addTimestamp = true) {
return Logger.getLogger().importMessages(msgs, prefixIn, addTimestamp);
}
static getLastLogMessage() {
const buf = Logger.getLogger().ringBuffer;
return buf ? buf.getLastLogMessage() : null;
}
}
//# sourceMappingURL=logger.js.map