@mcma/core
Version:
Node module with type definitions and helper utils for the EBU MCMA framework
88 lines (87 loc) • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.LogType = exports.LogLevel = void 0;
const util = require("util");
const uuid_1 = require("uuid");
const log_event_1 = require("./log-event");
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["Fatal"] = 100] = "Fatal";
LogLevel[LogLevel["Error"] = 200] = "Error";
LogLevel[LogLevel["Warn"] = 300] = "Warn";
LogLevel[LogLevel["Info"] = 400] = "Info";
LogLevel[LogLevel["Debug"] = 500] = "Debug";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
var LogType;
(function (LogType) {
LogType["Fatal"] = "FATAL";
LogType["Error"] = "ERROR";
LogType["Warn"] = "WARN";
LogType["Info"] = "INFO";
LogType["Debug"] = "DEBUG";
})(LogType || (exports.LogType = LogType = {}));
class Logger {
_source;
_requestId;
_tracker;
constructor(source, requestId, tracker) {
this._source = source;
this._requestId = requestId ?? (0, uuid_1.v4)();
this._tracker = tracker;
}
get source() {
return this._source;
}
get requestId() {
return this._requestId;
}
get tracker() {
return this._tracker;
}
static System;
buildLogEvent(level, type, message, ...optionalParams) {
if (optionalParams.length) {
if (typeof message === "string") {
message = util.format(message, ...optionalParams);
}
else {
message = [message, ...optionalParams];
}
}
return new log_event_1.LogEvent(type, level, this.source, this.requestId, new Date(), message, this.tracker);
}
fatal(message, ...optionalParams) {
this.log(LogLevel.Fatal, LogType.Fatal, message, ...optionalParams);
}
error(message, ...optionalParams) {
this.log(LogLevel.Error, LogType.Error, message, ...optionalParams);
}
warn(message, ...optionalParams) {
this.log(LogLevel.Warn, LogType.Warn, message, ...optionalParams);
}
info(message, ...optionalParams) {
this.log(LogLevel.Info, LogType.Info, message, ...optionalParams);
}
debug(message, ...optionalParams) {
this.log(LogLevel.Debug, LogType.Debug, message, ...optionalParams);
}
functionStart(message, ...optionalParams) {
this.log(450, "FUNCTION_START", message, ...optionalParams);
}
functionEnd(message, ...optionalParams) {
this.log(450, "FUNCTION_END", message, ...optionalParams);
}
jobStart(message, ...optionalParams) {
this.log(LogLevel.Info, "JOB_START", message, ...optionalParams);
}
jobUpdate(message, ...optionalParams) {
this.log(LogLevel.Info, "JOB_UPDATE", message, ...optionalParams);
}
jobEnd(message, ...optionalParams) {
this.log(LogLevel.Info, "JOB_END", message, ...optionalParams);
}
log(level, type, message, ...optionalParams) {
this.writeLogEvent(this.buildLogEvent(level, type, message, ...optionalParams));
}
}
exports.Logger = Logger;