LoRaWAN-Signal-Mapping/server/Helpers/Log.mjs

49 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
import Ansi from './Ansi.mjs';
class Log {
// Destructure the awilix container
constructor({ settings, ansi }) {
this.settings = settings;
/** @type {Ansi} */
this.a = ansi;
this.start_time = new Date();
// FUTURE: We could add a log target (e.g. a file etc.) here if required
}
log_raw(func, ...items) {
if(this.settings.logging.date_display_mode == "none") {
func(...items);
return;
}
let prefix = (new Date()).toISOString();
if(this.settings.logging.date_display_mode == "relative") {
prefix = ((new Date() - this.start_time) / 1000).toFixed(4);
}
func(`${this.a.locol}[${prefix}]${this.a.reset}`, ...items);
}
log(...items) { this.log_raw(console.log, ...items); }
info(...items) { this.log_raw(console.info, ...items); }
debug(...items) { this.log_raw(console.debug, ...items); }
warn(...items) {
this.log_raw(console.warn,
this.a.hicol + this.a.fyellow,
...items,
this.a.reset
);
}
error(...items) {
this.log_raw(console.error,
this.a.hicol + this.a.fred,
...items,
this.a.reset
);
}
}
export default Log;