Module to standardize logging output.
I don't like using
console.log
everywhere in my code. So this module allows me to standardize log statements with certain format.
- Log levels
- Output by log levels
- Standardized log structure that includes (but not limited to) app name, module name, function name, and other information
- High-order functions to wrap module name and function name
export interface LogEntry {
logType: LOGTYPES; // Log type
appName: string; // Application name
modName: string; // Module name
fnName: string; // Function name
entryTS: Date; // Log entry timestamp in UTC
friendlyMsg: string; // Log friendly/brief message
detailMsg?: string; // Log detail message (ex: StackTrace from Error)
task?: string; // Step/task being executed
durationIsMS?: number; // Duration taken in millisecond
}
export interface LogEntryWithDuration extends LogEntry {
endTS: Date; // Log entry end timestamp in UTC
durationIsMS?: number; // Duration taken in millisecond
}
Logger that takes all parameters
High-order logger on top of main logger to pin the module name
High-order logger on top of module logger to pin the function name
This logger helper module does not actually output the log entries. It expects a logger output to be assigned via setLoggerOutput()
function available from the main logger interface.
export type LoggerOutput = (entry: Readonly<LogEntry> | Readonly<LogEntryWithDuration>) => void;
You will need to run your application with
--es-module-specifier-resolution=node
option.Ex:
"exec": "node --es-module-specifier-resolution=node ./dist/index.js"
for your NPM scriptnpm run exec
.
Set type to module
"type": "module"
{
"name": "my-awesome-app",
"version": "1.0.0",
"description": "My Awesome App",
"main": "index.js",
"type": "module",
"scripts": {
}
}
Set module to one of ECMA script
"module": "esnext"
incompilerOptions
section
{
"compilerOptions": {
"module": "esnext",
}
}
Set module resolution to node
"moduleResolution": "node"
incompilerOptions
section
{
"compilerOptions": {
"moduleResolution": "node",
}
}
Brought to you by www.simplyappdevs.com (2021)