DEV Community

Cover image for Logflake, a NodeJS Console Logger with superpowers
Felippe Regazio
Felippe Regazio

Posted on

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, 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
Enter fullscreen mode Exit fullscreen mode

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

const logger = require('logflake');
const log = logger();
Enter fullscreen mode Exit fullscreen mode

or EJS

import logger from 'logflake';
const log = logger();
Enter fullscreen mode Exit fullscreen mode

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

log('Hello world');
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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 */
   });
Enter fullscreen mode Exit fullscreen mode

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

Discussion (2)

Collapse
rakz profile image
rakz

Man this is awesome. A few days back I was looking for something like this. Great work..

Collapse
felipperegazio profile image
Felippe Regazio Author

Thanks :3