@sidequest/core
Version:
@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.
55 lines (52 loc) • 2.18 kB
JavaScript
import winston from 'winston';
let _logger;
/**
* Configures and creates a Winston logger for Sidequest.
* @param options Logger configuration options.
* @returns The configured Winston logger instance.
*/
function configureLogger(options) {
const colors = {
error: "red",
warn: "yellow",
info: "green",
verbose: "cyan",
debug: "blue",
silly: "magenta",
};
winston.addColors(colors);
const newLogger = winston.createLogger({
level: options.level,
format: buildFormat(),
transports: [new winston.transports.Console()],
});
/**
* Builds the log format based on options.
* @returns The Winston log format.
*/
function buildFormat() {
if (options.json) {
return winston.format.combine(winston.format.timestamp(), winston.format.errors({ stack: true }), winston.format.label({ label: "Sidequest" }), winston.format.json());
}
return winston.format.combine(winston.format.colorize(), winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), winston.format.errors({ stack: true }), winston.format.label({ label: "Sidequest" }), winston.format.printf(({ timestamp, level, message, label, stack, scope, ...metadata }) => {
const metaStr = Object.keys(metadata).length ? `\n${JSON.stringify(metadata, null, 2)}` : "";
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const base = `[${level}] [${timestamp}] [${label}] ${scope ? `[${scope}] ` : ""}: ${message}${metaStr}`;
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string
return stack ? `${base}\n${stack}` : base;
}));
}
_logger = newLogger;
return newLogger;
}
// Default logger instance for Sidequest.
_logger = configureLogger({ level: "info", json: false });
/**
* Returns the default logger instance.
* @returns The Winston logger instance.
*/
function logger(scope) {
return scope ? _logger.child({ scope }) : _logger;
}
export { configureLogger, logger };
//# sourceMappingURL=logger.js.map