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

45 lines
1008 B
JavaScript

"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) {
let prefix = (new Date()).toISOString();
if(this.settings.logging.date_display_mode == "relative") {
prefix = (new Date() - this.start_time).toFixed(5);
}
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;