Skip to main content

Quick Logger Function for Nodejs Projects


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:


You would then import it as: 

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

/**
* logger is a small tool to centralize the logging of the application
* @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 {String} msg - the message to be displayed
* @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 {String} msg - the message to be displayed
* @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 send the error to slack in production environment
* @param {String} msg - the message to be displayed
* @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;

`const log = require("../logger.js")(__filename)`

in every file you want to use it, and there you go :)

Be careful, in a production environment it is unwise to use the console the object. If you use this code, be sure to create some conditional statements for changing the behavior in production.

Hope it can be useful to someone!

Comments

Popular posts from this blog

How to generate a self-signed certificate with Openssl (using git windows bash)

I recently needed to generate a self-signed certificate to test a website locally. I wasn't familiar with the process, and I wanted to share here what I learned. First, we need a private key. A private key is a long series of characters that must be kept secret. In my context, it will be used to encrypt messages before the client and the server, in a way secure enough to prevent anybody to spy on them. Once the private key is created, we need to generate another file that will be the "signature" of our certificate. Among other data, this file will contains some information specific to the server's context: country, organization's name, email address of the organization's technical contact, etc. Once this signature is established, there are two paths: - Path A: If we want our server to be publicly accessible, every browser in the world must able to trust the certificate. In order for that to happen, we need to send our signature file to one of the official SSL ...

How to Compile a GTK4 program on Windows (using MSYS)

  I wanted to create a GUI program using C and GTK. I am using a windows operating system, so I had followed the instructions from the GTK Windows installation page . However, when trying to compile my program using: gcc main.c -o main.exe `pkg-config --cflags --libs gtk4` I had the following errors: gcc.exe: error: pkg-config: No such file or directory gcc.exe: error: gtk4`: No such file or directory gcc.exe: error: unrecognized command-line option '--cflags' gcc.exe: error: unrecognized command-line option '--libs' I noticed that the command  pkg-config --cflags --libs gtk4 was actually returning a list of required includes. It seemed that the gcc command simply was not able to recognize them. To solve the problem, I then created a Makefile, in which a variable called GTK_4_INCLUDES contains the list of includes.  The compilation command becomes:   gcc main.c -o main.exe $(GTK_4_INCLUDES) To compile the program in the console, I just had to type mingw32-make , a...

Fixing x-invalid-end-tag lint error on Vuejs

  My situation is: Coding with vue in a Vue file. Using the Vetur plugin in VS code It then generates an error of type x-invalid-end-tag in a quite random fashion. To solve it, I have found one simple trick in this github issue: // .eslintrc.js module.exports = {  //... rules:  { 'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }]  }  }