-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log controller, start work on log levels #13, add ability to swit…
- Loading branch information
Brandyn
committed
Feb 18, 2024
1 parent
2893602
commit ac500c5
Showing
7 changed files
with
164 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const config = require("config.json"); | ||
const fs = require("fs"); | ||
const _ = require("lodash"); | ||
|
||
class log { | ||
LOG_LEVEL = { | ||
SYSTEM: 0, | ||
ERROR: 1, | ||
WARN: 3, | ||
INFO: 7, | ||
DEBUG: 15, | ||
VERBOSE: 31, | ||
}; | ||
|
||
LOG_STR = _.invert(this.LOG_LEVEL); | ||
|
||
constructor() { | ||
this.log = console.log; | ||
} | ||
|
||
addLog(level, message, data) { | ||
if (config.logLevel & level) return; | ||
this.logConsole(level, message, data); | ||
} | ||
|
||
getTimeStamp() { | ||
return new Date().toISOString(); | ||
} | ||
|
||
compileMessage(message, data) { | ||
if (data) { | ||
_.keysIn(data).forEach((key) => { | ||
let data = | ||
data[key] instanceof Object ? JSON.stringify(data[key]) : data[key]; | ||
message = message.replace(`\$${key}`, `${key}: ${data}`); | ||
}); | ||
} | ||
return message; | ||
} | ||
|
||
createLogString(level, message, data) { | ||
return `${this.getTimeStamp()} - [${ | ||
this.LOG_STR[level] | ||
}] - ${this.compileMessage(message, data)}`; | ||
} | ||
|
||
logConsole(level, message, data) { | ||
console.log(this.createLogString(level, message, data)); | ||
} | ||
|
||
logFile(level, message, data) { | ||
if (config.logging.file === undefined) return; | ||
fs.appendFileSync( | ||
config.logging.file, | ||
this.createLogString(level, message, data), | ||
data | ||
); | ||
} | ||
|
||
system(message, data) { | ||
this.addLog(this.LOG_LEVEL.SYSTEM, message, data); | ||
} | ||
|
||
error(message, data) { | ||
this.addLog(this.LOG_LEVEL.ERROR, message, data); | ||
} | ||
|
||
info(message, data) { | ||
this.addLog(this.LOG_LEVEL.INFO, message, data); | ||
} | ||
|
||
debug(message, data) { | ||
this.addLog(this.LOG_LEVEL.DEBUG, message, data); | ||
} | ||
|
||
verbose(message, data) { | ||
this.addLog(this.LOG_LEVEL.VERBOSE, message, data); | ||
} | ||
} | ||
|
||
module.exports = Object.freeze(new log()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters