Setup basic logging framework.

In the future, we might be able to use a proxy object here or some other 
wizadry? I'm not sure.
This commit is contained in:
Starbeamrainbowlabs 2019-05-29 11:59:40 +01:00
parent 31a75b5589
commit 009d6335a0
5 changed files with 62 additions and 5 deletions

44
server/Helpers/Log.mjs Normal file
View file

@ -0,0 +1,44 @@
"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;

View file

@ -3,6 +3,7 @@
import a from 'awilix'; import a from 'awilix';
import Ansi from '../Helpers/Ansi.mjs'; import Ansi from '../Helpers/Ansi.mjs';
import Log from '../Helpers/Log.mjs';
import TTNAppServer from '../ttn-app-server/TTNAppServer.mjs'; import TTNAppServer from '../ttn-app-server/TTNAppServer.mjs';
import settings from './settings.mjs'; import settings from './settings.mjs';
@ -14,10 +15,12 @@ const c = a.createContainer({
c.register({ c.register({
settings: a.asValue(settings), settings: a.asValue(settings),
ansi: a.asFunction(() => new Ansi()).singleton(), ansi: a.asClass(Ansi).singleton(),
log: a.Class(Log).singleton(),
database: a.asFunction(database_init).singleton(), database: a.asFunction(database_init).singleton(),
TTNAppServer: a.asClass(TTNAppServer), TTNAppServer: a.asClass(TTNAppServer),
}); });
c.loadModules("../Repos.SQLite/*.mjs", { c.loadModules("../Repos.SQLite/*.mjs", {
register: a.asClass, register: a.asClass,
lifetime: a.Lifetime.SINGLETON lifetime: a.Lifetime.SINGLETON

View file

@ -8,6 +8,7 @@ import show_help from './help.mjs';
const settings = c.resolve("settings"); const settings = c.resolve("settings");
const a = c.resolve("ansi"); const a = c.resolve("ansi");
const l = c.resolve("log");
// 2: CLI Argument Parsing // 2: CLI Argument Parsing
let args = process.argv.slice(2); // Slice out the node binary name and the filename let args = process.argv.slice(2); // Slice out the node binary name and the filename
@ -45,8 +46,12 @@ if(extras.length < 1) {
process.exit(); process.exit();
} }
l.log(`${a.fgreen}${a.hicol}*** LoRaWAN Signal Mapper ***`);
switch(extras[0]) { switch(extras[0]) {
case "ttn-app-server": case "ttn-app-server":
l.log(`${a.fgreen}${a.hicol}Starting The Things Network application server${a.reset}`);
let app_server = c.resolve("TTNAppServer"); let app_server = c.resolve("TTNAppServer");
app_server.run(); app_server.run();
break; break;

View file

@ -14,3 +14,9 @@ filename = "lorawan.sqlite"
# The options to pass to better-sqlite3. You probably don't need to change this. # The options to pass to better-sqlite3. You probably don't need to change this.
[database.options] [database.options]
[logging]
# The format the date displayed when logging things should take.
# Allowed values: relative (e.g like when a Linux machine boots), absolute (e.g. like Nginx server logs)
date_display_mode = "relative"

View file

@ -1,15 +1,14 @@
"use strict"; "use strict";
import ansi from '../Helpers/Ansi.mjs';
class TTNAppServer { class TTNAppServer {
// Destructure the awilix container // Destructure the awilix container
constructor({ settings }) { constructor({ settings, ansi }) {
this.settings = settings; this.settings = settings;
/** @type {Ansi} */
this.a = ansi;
} }
run() { run() {
} }
} }