2023-03-08 21:31:19 -05:00
|
|
|
// ===========================================================================
|
2023-03-02 11:39:37 -08:00
|
|
|
// to fix serialization of regexes for logging purposes
|
|
|
|
RegExp.prototype.toJSON = RegExp.prototype.toString;
|
|
|
|
|
|
|
|
|
2023-03-08 21:31:19 -05:00
|
|
|
// ===========================================================================
|
|
|
|
export function errJSON(e) {
|
|
|
|
return {"type": "exception", "message": e.message, "stack": e.stack};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-24 18:31:08 -08:00
|
|
|
// ===========================================================================
|
2023-03-17 14:24:44 -07:00
|
|
|
class Logger
|
2022-12-15 12:38:41 -05:00
|
|
|
{
|
2023-03-17 14:24:44 -07:00
|
|
|
constructor() {
|
|
|
|
this.logStream = null;
|
|
|
|
this.debugLogging = null;
|
2023-04-01 13:07:59 -04:00
|
|
|
this.logLevels = [];
|
|
|
|
this.contexts = [];
|
2023-03-17 14:24:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
setExternalLogStream(logFH) {
|
|
|
|
this.logStream = logFH;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDebugLogging(debugLog) {
|
|
|
|
this.debugLogging = debugLog;
|
|
|
|
}
|
|
|
|
|
2023-04-01 13:07:59 -04:00
|
|
|
setLogLevel(logLevels) {
|
|
|
|
this.logLevels = logLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
setContext(contexts) {
|
|
|
|
this.contexts = contexts;
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:38:41 -05:00
|
|
|
logAsJSON(message, data, context, logLevel="info") {
|
2023-01-23 10:43:12 -08:00
|
|
|
if (data instanceof Error) {
|
2023-03-08 21:31:19 -05:00
|
|
|
data = errJSON(data);
|
2023-01-23 10:43:12 -08:00
|
|
|
} else if (typeof data !== "object") {
|
2022-12-15 12:38:41 -05:00
|
|
|
data = {"message": data.toString()};
|
|
|
|
}
|
2023-04-01 13:07:59 -04:00
|
|
|
|
|
|
|
if (this.logLevels.length) {
|
|
|
|
if (this.logLevels.indexOf(logLevel) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.contexts.length) {
|
|
|
|
if (this.contexts.indexOf(context) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:38:41 -05:00
|
|
|
let dataToLog = {
|
|
|
|
"logLevel": logLevel,
|
|
|
|
"timestamp": new Date().toISOString(),
|
|
|
|
"context": context,
|
|
|
|
"message": message,
|
|
|
|
"details": data ? data : {}
|
|
|
|
};
|
2023-02-24 18:31:08 -08:00
|
|
|
const string = JSON.stringify(dataToLog);
|
|
|
|
console.log(string);
|
2023-03-17 14:24:44 -07:00
|
|
|
if (this.logStream) {
|
|
|
|
this.logStream.write(string + "\n");
|
2023-02-24 18:31:08 -08:00
|
|
|
}
|
2022-12-15 12:38:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
info(message, data={}, context="general") {
|
|
|
|
this.logAsJSON(message, data, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
error(message, data={}, context="general") {
|
|
|
|
this.logAsJSON(message, data, context, "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(message, data={}, context="general") {
|
|
|
|
this.logAsJSON(message, data, context, "warn");
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(message, data={}, context="general") {
|
2023-03-17 14:24:44 -07:00
|
|
|
if (this.debugLogging) {
|
2022-12-15 12:38:41 -05:00
|
|
|
this.logAsJSON(message, data, context, "debug");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal(message, data={}, context="general", exitCode=1) {
|
|
|
|
this.logAsJSON(`${message}. Quitting`, data, context, "fatal");
|
|
|
|
process.exit(exitCode);
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 14:24:44 -07:00
|
|
|
|
|
|
|
export const logger = new Logger();
|