@imqueue/async-logger
Version:
Configurable async logger over winston for @imqueue services
116 lines • 3.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const winston_1 = require("winston");
const helpers_1 = require("./helpers");
/**
* Class Logger
*/
class Logger {
/**
* @description
* Configuring options of async-logger
*
* @returns {LoggerOptions}
*/
static getLoggerOptions(metadata) {
return {
exitOnError: false,
format: winston_1.format.json(),
defaultMeta: metadata || (0, helpers_1.defaultMetadata)(),
};
}
/**
* Logger Ctor
*
* @constructor
* @param {AsyncLoggerOptions} options
*/
constructor(options) {
const opts = Logger.getLoggerOptions((options || {}).metadata);
const config = (options || {}).transports || (0, helpers_1.transportsConfig)();
if (config && config.length) {
this.logger = (0, winston_1.createLogger)(opts);
this.setupLogger(config);
}
}
/**
* Logs given arguments to configured async-logger destinations and to stdout
* with log level = LOG
*
* @param {...*[]} args
*/
log(...args) {
Logger.console.log(...args);
if (this.logger) {
this.logger.info((0, helpers_1.buildMessage)(args));
}
}
/**
* Logs given arguments to configured async-logger destinations and to stdout
* with log level = INFO
*
* @param {...*[]} args
*/
info(...args) {
Logger.console.info(...args);
if (this.logger) {
this.logger.info((0, helpers_1.buildMessage)(args));
}
}
/**
* Logs given arguments to configured async-logger destinations and to stderr
* with log level = WARN
*
* @param {...*[]} args
*/
warn(...args) {
Logger.console.warn(...args);
if (this.logger) {
this.logger.warn((0, helpers_1.buildMessage)(args));
}
}
/**
* Logs given arguments to configured async-logger destinations and to stderr
* with log level = ERROR
*
* @param {...*[]} args
*/
error(...args) {
Logger.console.error(...args);
if (this.logger) {
this.logger.error((0, helpers_1.buildMessage)(args));
}
}
/**
* Configuring transports of async-logger
*
* @param {TransportOptions[]} config - configuration for transports from
* environment variables
*/
setupLogger(config) {
if (!config || !Array.isArray(config)) {
throw new TypeError('Logger: Invalid config provided!');
}
for (const options of config) {
if (!options.enabled) {
continue;
}
const transport = (0, helpers_1.getTransport)(options.type, options.options);
this.logger.add(transport);
}
}
}
exports.Logger = Logger;
/**
* Asynchronous console async-logger
*
* @type {ILogger}
*/
Logger.console = {
log: (...args) => setTimeout(() => console.log(...args)),
info: (...args) => setTimeout(() => console.info(...args)),
warn: (...args) => setTimeout(() => console.warn(...args)),
error: (...args) => setTimeout(() => console.error(...args)),
};
//# sourceMappingURL=Logger.js.map