Logflake, a NodeJS Console Logger with superpowers

I just finished this lib that i’ve been working on for the few past weeks. LogFlake is a NodeJS console logger with superpowers. It has the same API as the usual Console but with beautified output, a message header with timestamp and useful information…


This content originally appeared on DEV Community and was authored by Felippe Regazio

I just finished this lib that i've been working on for the few past weeks. LogFlake is a NodeJS console logger with superpowers. It has the same API as the usual Console but with beautified output, a message header with timestamp and useful information, trackability and a toolset for a better control of your log messages. You can check out the lib and the docs on this link: https://www.npmjs.com/package/logflake.

I decided to write this lib because i like the Console simplicity, but i miss some features. I was searching for a very simple, out of the box tool just to have better output and control of the console message logging. Then i wrote "logflake", which is pretty neat and, despite lots of options, requires zero configuration to use its basic features.

The lib was written with TypeScript, and tested with Jest. It has a test coverage (unity and integration) near 90%, and its available on NPMJS. You can download, or install it using npm/yarn.

Getting started

I'll show some of the basic features. If you like it, please consider to leave a star on GitHub. PR's are very welcome!

Hands on, you can install it using NPM or Yarn:

npm install logflake

Then you must create your log function (you can give the name you prefer). In CJS

const logger = require('logflake');
const log = logger();

or EJS

import logger from 'logflake';
const log = logger();

Now you can log things. As said, it has the same API as Console, with some advantages.

log('Hello world');

Will output:

image

The console header shows a namespace CONSOLE LOG , followed by the O.S indentifier, O.S username, current mainfile, date and time. You can configure the header and decide which information you want to show.

You can log anything you want, or how many things you want. For example, this is the log function logged by itself:

log('Hello %s', 'world', log);

Will output:

image

Log levels

The first log function argument can be used to change the log level. You can use the following log levels:

  • log (blue)
  • info (cyan)
  • warn (yellow)
  • error (red)
  • trace (magenta)
  • quiet (no console output)

An error, for example, would be:

log('error', 'Unexpected error', 500);

And would produce:

image

Namespaces

Now lets imagine that you have lots of logs in a huge and distributed application. You can add a namespace for each log function to make them easier to find:

const logger = require('logflake');
const log = logger('Example'); // Example is the namespace

log('error', 'Unexpected error', 500);

Note the [ EXAMPLE ERROR ] prefix on the log header:

image

Options

Logflake accepts lots of options passed directly to the "logger". To illustrate some of them, lets say you want to count how many times a log was triggered, and save its output on a local file. You could do:

const logger = require('logflake');

const log = logger({
  prefix: 'Example', // Now we pass Namespace as an option
  logDir: './',      // Directory to save the logs
  callCount: true    // Count how many times a log happened
});

/**
 * Now lets pretend this error happened 1000 times
 */
for (let i = 0; i < 1000; i++) {
  log('error', 'Unexpected error', 500).save();
}

This will output:

(...)

image

Note that the function now has a count (x1000, for example). Since we passed the option "callCount", it indicates how many times the log has been triggered on the current runtime. The save() method tells the logger to save each log output (of this specific call) to a file on the directory passed on the logDir option. The logger will automatically organize the different log files by date.

Methods

Now lets say you dont want to pass the save() method to specific log calls, instead you want to save all of them. Also you dont want to polute your log file with 1000 duplicated log registers, just one is enough to alarm the team.

You can ask LogFlake to save all logs for you, and to save some of them only once, like that:

const logger = require('logflake');

const log = logger({
  prefix: 'Example', // Now we pass Namespace as an option
  logDir: './',      // Directory to save the logs
  alwaysSave: true,  // Save all log outputs to a log file
  callCount: true    // Count how many times a log happened
});

log('I'll be saved also :3');

for (let i = 0; i < 1000; i++) {
  log('error', 'Unexpected error', 500).once();
}

The code above will save the first log, then will trigger and save the error log only once, despite being inside a 1000x for loop, because of the .once() method. All logs will be automatically saved due the alwaysSave option. Since the once has been used on the error, it will be saved only once.

We can also imagine that this is a very important log for you, and you want to send an alarm with its content to slack when and if it fires. LogFlake STILL dont do that (the slack thing), but you can get the log output + information and sent to whatever you want:

log('error', 'Unexpected error', 500)
  .once();
  .get((output, info) => {
     /* do whatever you want with output and info */
   });

As showed above, we are getting the log output using the get method. The output param will contain the string representing the log exactly as showed on the console. The info` param is a useful object containing information about the log as level, call count, trace, etc. You can also automatically trap all log outputs, allowing you to send them to slack, a database or whatever you want.

Conclusion

There are lots of options and usages to LogFlake and would be a huge post to show all of them, those were only some cool examples. If you liked, you can checkout the complete documentation and sources at GitHub: https://github.com/felippe-regazio/logflake.

As already mentioned, this lib is intended to be VERY simple and useful. Its a very handy way to track and save some runtime information while running your code.

image

Cover image by Jason Richard at Unsplash


This content originally appeared on DEV Community and was authored by Felippe Regazio


Print Share Comment Cite Upload Translate Updates
APA

Felippe Regazio | Sciencx (2021-08-04T15:38:54+00:00) Logflake, a NodeJS Console Logger with superpowers. Retrieved from https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/

MLA
" » Logflake, a NodeJS Console Logger with superpowers." Felippe Regazio | Sciencx - Wednesday August 4, 2021, https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/
HARVARD
Felippe Regazio | Sciencx Wednesday August 4, 2021 » Logflake, a NodeJS Console Logger with superpowers., viewed ,<https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/>
VANCOUVER
Felippe Regazio | Sciencx - » Logflake, a NodeJS Console Logger with superpowers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/
CHICAGO
" » Logflake, a NodeJS Console Logger with superpowers." Felippe Regazio | Sciencx - Accessed . https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/
IEEE
" » Logflake, a NodeJS Console Logger with superpowers." Felippe Regazio | Sciencx [Online]. Available: https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/. [Accessed: ]
rf:citation
» Logflake, a NodeJS Console Logger with superpowers | Felippe Regazio | Sciencx | https://www.scien.cx/2021/08/04/logflake-a-nodejs-console-logger-with-superpowers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.