@egodigital/egoose
Version:
Helper classes and functions for Node.js 10 or later.
218 lines • 5.19 kB
JavaScript
;
/**
* This file is part of the @egodigital/egoose distribution.
* Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
*
* @egodigital/egoose is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* @egodigital/egoose is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLogger = exports.Logger = exports.LogType = void 0;
const moment = require("moment");
/**
* List of log types.
*/
var LogType;
(function (LogType) {
/**
* Emergency
*/
LogType[LogType["Emerg"] = 0] = "Emerg";
/**
* Alert
*/
LogType[LogType["Alert"] = 1] = "Alert";
/**
* Critical
*/
LogType[LogType["Crit"] = 2] = "Crit";
/**
* Error
*/
LogType[LogType["Err"] = 3] = "Err";
/**
* Warning
*/
LogType[LogType["Warn"] = 4] = "Warn";
/**
* Notice
*/
LogType[LogType["Notice"] = 5] = "Notice";
/**
* Informational
*/
LogType[LogType["Info"] = 6] = "Info";
/**
* Debug
*/
LogType[LogType["Debug"] = 7] = "Debug";
/**
* Trace
*/
LogType[LogType["Trace"] = 8] = "Trace";
})(LogType = exports.LogType || (exports.LogType = {}));
/**
* A logger.
*/
class Logger {
constructor() {
this._ACTIONS = [];
}
/**
* Adds a log action.
*
* @param {LogAction} action The action.
*
* @return this
*/
addAction(action) {
this._ACTIONS.push(action);
return this;
}
/**
* Logs an alert message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
alert(msg, tag) {
return this.log(LogType.Alert, msg, tag);
}
/**
* Logs a critical message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
crit(msg, tag) {
return this.log(LogType.Crit, msg, tag);
}
/**
* Logs a debug message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
dbg(msg, tag) {
return this.log(LogType.Debug, msg, tag);
}
/**
* Logs an emergency message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
emerg(msg, tag) {
return this.log(LogType.Emerg, msg, tag);
}
/**
* Logs an error message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
err(msg, tag) {
return this.log(LogType.Err, msg, tag);
}
/**
* Logs an info message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
info(msg, tag) {
return this.log(LogType.Info, msg, tag);
}
/**
* Logs a message.
*
* @param {LogType} type The type.
* @param {any} msg The message to log.
* @param {string} tag The optional tag.
*
* @return this
*/
log(type, msg, tag) {
const CONTEXT = {
message: msg,
tag: tag,
time: moment.utc(),
type: type,
};
this._ACTIONS.forEach(action => {
action(CONTEXT);
});
return this;
}
/**
* Logs a notice.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
note(msg, tag) {
return this.log(LogType.Notice, msg, tag);
}
/**
* Logs a trace message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
trace(msg, tag) {
return this.log(LogType.Trace, msg, tag);
}
/**
* Logs a warning message.
*
* @param {any} msg The message to log.
* @param {string} [tag] Optional tag to log.
*
* @return this
*/
warn(msg, tag) {
return this.log(LogType.Warn, msg, tag);
}
}
exports.Logger = Logger;
/**
* Creates a new logger instance.
*
* @param {LogAction[]} [actions] One or more initial actions to add.
*
* @return {Logger} The new instance.
*/
function createLogger(...actions) {
const NEW_LOGGER = new Logger();
actions.forEach(a => {
NEW_LOGGER.addAction(a);
});
return NEW_LOGGER;
}
exports.createLogger = createLogger;
//# sourceMappingURL=logger.js.map