Skip to content

Instantly share code, notes, and snippets.

@bitsapien
Last active January 30, 2021 02:26
Show Gist options
  • Save bitsapien/5f301181947a4a1ec00bcf550862d2e7 to your computer and use it in GitHub Desktop.
Save bitsapien/5f301181947a4a1ec00bcf550862d2e7 to your computer and use it in GitHub Desktop.
node-logger : May serve as an alternative to winstonjs
/*
* ======================================================
* node-logger: A simple logger for nodejs
* ======================================================
*
* Motivation: I noticed a logging library like winstonjs
* could add almost 200KBs to my bundle size (un-gzipped)
* for doing something as simple as this 👇🏽
*/
const errorLevels = {
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
}
const logger = (level, message) => {
const envLogLevel = errorLevels[process.env.LOG_LEVEL] || 100;
if(errorLevels[level] <= envLogLevel)
console.log(JSON.stringify({timestamp: (new Date()).toISOString(),level, message}));
}
// Usage:
logger('info', 'Design is separating into things that can be composed. - Rich Hickey')
/* Output:
* {"timestamp":"2021-01-29T14:22:34.688Z","level":"info","message":"Design is separating into things that can be composed. - Rich Hickey"}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment