LogSense Blog

See everything, even before it happens.

Node.js logging libraries

Mar 12, 2019 7:25:57 AM |     Krystian Kruk

No matter what your Node.js application does, runs HTTP server, is a part of bigger microservices architecture or runs on serverless platform, logging will become an important part of your job.

Proper logs will help you:

  • easier find potential issues and errors

  • better understand your application

  • find bottlenecks and improve performance

Node.js logging libraries

Choosing logging library depends on your node.js application’s needs.

If it’s a library, you should check debug. It’s a popular library which gives a possibility to show debug logs only from libraries you want to. It works by passing DEBUG environment variable. For instance, if you run your application by DEBUG=express* node index.js, you will see debug logs from popular Express HTTP server.

For applications you can use standard built-in console methods or use some more advanced libraries like winston.

Using console methods in Node.js is as simple as logging in a browser.

By calling e.g. console.error('Cannot find user', userId, 'because of', error) you will find well-formatted (all objects will be stringified) log message in your console.

Winston, on the other hand is helpful when you want to stream your logs into a file (text or json), restrict logging level, format all logs at once (e.g. by adding timestamp to each log) and more. Winston can be called like this winston.log('info', 'Server started listening', { port: 3000 }).

Structured vs unstructured logs

If you’re not familiar with a concept of structured logs, it says that each log should provide metadata (extra informations potentially helpful in logs analyzing) in a standardized way (mostly JSON, but also could be XML or other format).

When normally you would write:

console.log('Send email to a client')

To keep your logs more structured, you should write:

console.log('Send email to a client', { email: { topic, address }, clientId })

Not so scary, right? Developers often provide such informations without knowing what structured logging is. It’s clear that such informations would be very helpful in further investigations. Furthermore such metadata are frequently written inside the log message e.g. console.log('Send', topic, 'email to a client', clientId). Such logs provide all needed informations as well.

Besides of a way how you log things, remember to never ever log password, or credentials as well as other sensitive data.

Understand your logs

LogSense is a cloud-based service where you can deeper understand and filter all of your structured and unstructured logs thanks to auto matching patterns technology.

To start using LogSense with your Node.js application you need just a few minutes.

Firstly you need to create a free account on https://www.logsense.com/ and copy your private CUSTOMER_TOKEN.

Then, inside your Node.js project, type npm install logsense.

The final step is to modify your main js file and add:

const logsense = require('logsense')('CUSTOMER_TOKEN')

Or, for Winston:

const logsense = require('logsense')('CUSTOMER_TOKEN')
logger.add(new logsense.WinstonTransport())

That’s it! Now you can open your logs console and discover logs sent from your application.

Let’s see, one by one, how our sample logs would see in LogSense:

console.log('Send email to a client', { email: { topic, address }, clientId })

console.log('Send', topic, 'email to a client', clientId)

console.error('Cannot find user', userId, 'because of', error)

winston.log('info', 'Server started listening', { port: 3000 })

As you can see, all fields from structured logs as well as auto-discovered parameters from text messages are easy to access and filter.

Log parameters into charts

All this data we provide in logs could be very helpful for statistics or performance analyze.

Using our previously sent logs console.log('Send email to a client', { email: { topic, address }, clientId }) let’s check how many emails we sent in a period of time.

Charts can be built by clicking or drag and dropping the Custom Chart view from the views list.

Period of time from what your logs needs to be aggregated or listed can be managed using the time bar at the bottom of page.

Topics: Node.js

Want more of the LogSense Blog? You got it.
Subscribe to our newsletter.