9
5 Comments

Small logger function for javascript projects

Hello!
I am writing a node app. I needed to centralize my logging system so I can easily manage the logs from one environment to another, and add more features to it. I know there are packages for that, and they would provide more powerful features, but the following code is really all I need, and I keep a lot of control over what I display.

The code:

const { format } = require("date-fns");

/**
 * logger is a small tool to centralize the logging of the application
 * [@params](/params) {String} src - this will be prefixed before each message. It can be any string
 * that would be significant when you read them in the logs (often in this code base it's the name
 * of the file from where the log is sent)
 */
const logger = (src) => {
  return {
    /**simply displays the message in the console
     * [@param](/param) {String} msg - the message to be displayed
     * [@return](/return) - nothing
     */
    info: (msg) => {
      const now = format(new Date(), "dd/MM/yyyy HH:mm");
      console.log(`${now} info - ${msg} (${src})`);
    },
    /** displays the message in the console with a warning prefix
     * [@param](/param) {String} msg - the message to be displayed
     * [@return](/return) - nothing
     */
    warn: (msg) => {
      const now = format(new Date(), "dd/MM/yyyy HH:mm");
      console.warn(`${now} warning - ${msg} (${src})`);
    },
    /** displays the message in the console with an error prefix
     * [@todo](/todo) send the error to slack in production environment
     * [@param](/param) {String} msg - the message to be displayed
     * [@return](/return) - nothing
     */
    err: (error) => {
      const now = format(new Date(), "dd/MM/yyyy HH:mm");
      // in production, send to slack/email
      console.error(`${now} ERROR - ${error.message || error} (${src})`);
    },
  };
};

module.exports = logger;

You would then import it as const log = require("../logger.js")(__filename) in every file you want to use it, and there you go :)

Hope it can be useful to someone!

  1. 1

    I used Winston as a logger package for our microservices. It enables you to log every level and desired platforms such as files, CloudWatch, S3, Slack and more. Also I wrote an article about. You can check it out if you want https://medium.com/javascript-in-plain-english/log-like-a-boss-nodejs-a10f57a8e3fc

    1. 1

      I have read it this morning. It's interesting, thank you! I am glad I have this personal logger file because from there I can switch to winston at any time. No need to go trough all the code and change the logging functions everywhere since it's already centralized!

      1. 1

        I'm glad that you find it useful. I love Winston, it is easy to use module and very powerful.

  2. 1

    Thanks for sharing @nakurai

    I believe console.log is blocking in node.js, and should be used carefully for logging in production; unless that's the desired outcome.

    1. 1

      I just checked to be sure and you are right. From what I have seen the trick is to use a stream to write to a file in production.
      Also, I found it handy in the past to send the errors to a slack channel to be notified in real time. Anyway, thank you for your comment!

Trending on Indie Hackers
How I grew a side project to 100k Unique Visitors in 7 days with 0 audience 48 comments Competing with Product Hunt: a month later 33 comments Why do you hate marketing? 28 comments $15k revenues in <4 months as a solopreneur 14 comments My Top 20 Free Tools That I Use Everyday as an Indie Hacker 13 comments Use Your Product 13 comments