Technical Resources
Educational Resources
APM Integrated Experience
Connect with Us
Many developers start with Node.js by building a small pet project. However, things often change, and small projects become increasingly bigger.
On top of this, developers sometimes aren’t the best writers by logging inaccurate, meaningless, or just dubious log messages.
What do you end up with? An application that’s hard to debug.
When a user encounters an issue, you have no relevant data on what went wrong. You can only hope to get in touch with the user who experienced the issue, so they can tell you what happened.
As you can tell, this isn’t a good approach. This article focuses on four main points related to logging with Node.js:
– The importance of logging meaningful messages
– Best logging practices for Node.js applications
– Different logger libraries and how you can integrate them with log aggregation tools, such as Loggly
– The power of trend analysis applied to logs
Logging was previously considered a low-priority operation. People spent little effort on writing meaningful logs. However, due to technical improvements in machine learning and artificial intelligence, logs have become a valuable piece of information.
You can use logs to:
– Analyze user behavior
– Analyze the overall health of your application
– Improve application performance by analyzing the key components in your application
– Measure important metrics, such as the number of errors or warnings being logged or the overall availability of your application
Moreover, logs help you understand what’s happening inside your application. Your logs tell the story of what’s happened. This data is important when something goes wrong and you have to debug an issue. The first thing you should do as a developer is consult the logs to see the story leading up to the issue. By doing this, you can easily understand what actions have led to the problem.
Logs are valuable when debugging your application. Furthermore, technological advancements such as trend analysis make it possible to detect anomalies by analyzing your logs, so your organization can shift from reactive to proactive monitoring. You can reduce the number of errors because you can fix them before the user experiences them.
To sum up the benefits of logging:
– It lets you solve issues faster
– You can shift to proactive monitoring and improving the user experience
– It allows you to measure important metrics through the data logged
When starting with Node.js logging, keep these in mind.
As John Papa describes in his article on environment variables, they’re a fundamental part of developing with Node.js. They allow your app to behave differently based on the environment you want them to run in. It’s an elegant and simple solution to change the behavior of your application.
You can use environment variables to tailor the log level for your application. For a testing environment, it makes sense to display DEBUG- and INFO-level log messages. However, when deploying your application to a production environment, you won’t want to deal with the noise DEBUG- and INFO-level log messages create. Here, you can use environment variables to configure your application to print only the log level messages that are WARNING or higher.
In addition, the size of your log files won’t grow as fast when only printing WARNING or higher log messages.
It’s important to agree as a team on when to use which log level. For example, you might define a failed login attempt as a WARNING level. But a failed login attempt might mean something’s wrong or someone’s trying to breach an account.
When your team uses different log levels for similar events, you might miss out on important information in your production environment.
In addition, agree on a specific style format for your logs. You might want to include the user ID in every log message. Define these standards before you start writing your code. A consistent log format makes it easier to analyze logs. It also makes it easier for log aggregation tools such as SolarWinds® Loggly® to apply trend analysis. Consistency is crucial!
A logging library often provides much more functionality than a standard console.log statement. Well-known logging libraries include Winston, Bunyan, and Log4js.
Bunyan provides functionality to send log data to various locations, such as Loggly or another log aggregation tool. It also allows you to send Slack notifications when a FATAL error occurs. Even more interesting, Bunyan can automatically take care of file rotation to save you storage space.
In this section, we’ll showcase the most important features for three logging libraries: Winston, Bunyan, and Log4js.
Winston is a universal logging library that supports the storage of logs in remote locations. It allows you to store certain log levels in a remote location, such as Loggly. The remaining logs can be written to another location, like a database. This useful feature allows you to write higher-level logs to a log aggregation tool (such as Loggly) and store less important logs in a database or in file storage.
Loggly provides an NPM package to transport logs by the transport option to Loggly: winston-loggly-bulk. Want more information on how to use this package? View this Node.js logging tutorial.
Loggly also has a tutorial on how to add Winston to your application.
Bunyan is also a feature-rich logging library. Like Winston, Bunyan provides the ability to configure different transport options. For example, you can use the bunyan-loggly package to configure Bunyan to transport logs to Loggly.
Log4js stems from the popular Java logger Log4j. The package is less popular than Winston or Bunyan but still holds some interesting features. For example, Log4js allows developers to log to the console, log to a file, or transmit logs through a TCP connection to a log aggregator, such as the Loggly log4js-loggly package.
Log4js is easy to configure through a config file holding a JSON object. The snippet below shows how you can quickly change the log format using patterns. You can find the full list of patterns in Log4js documentation.
{
"appenders": {
"consoleAppender": {
"type": "console", "layout": {
"type": "pattern", "pattern": "%d - %c:[%p]: %m"
}
}
}
}
Trend analysis allows you to analyze logs and detect anomalies by looking for patterns in your logs indicative of something wrong. For example, an increased number of failed login attempts might indicate some malicious user is trying to breach an account.
Let’s say the number of non-existing pages being requested suddenly increases. This could mean there’s something wrong with the application or an attacker is trying to gain access to your application through insecure ways.
These examples of log analysis are hard for humans to detect because we just can’t process thousands or even millions of logs. You can learn more about trend analysis in Loggly here. Also, you can follow the Loggly simple tutorial to learn how you can use its logging tool for Node.js.
This post was written by Michiel Mulders. Michiel is a passionate blockchain developer who loves writing technical content. Besides that, he loves learning about marketing, UX psychology, and entrepreneurship. When he’s not writing, he’s probably enjoying a Belgian beer!