DEV Community

Cover image for Creating a Custom Logger for a Node.js Application using Errsole
Venki for errsole

Posted on

Creating a Custom Logger for a Node.js Application using Errsole

Every robust logging system consists of three key components: a Collector, a Storage, and a Visualizer. Popular Node.js logging modules like Winston and Pino function solely as log collectors. Errsole, however, offers a complete logging solution that includes:

  1. Log Storage: Save logs in a file or your application’s database.

  2. Built-in Dashboard: View, filter, and search logs using the built-in dashboard.

  3. Alerts: Receive real-time notifications for critical errors.

Installing Errsole and its dependencies

To use Errsole, you need to install the Errsole module along with a storage module based on your chosen database. Here are the installation steps for different storage options:

File Storage:

npm install errsole errsole-sqlite
Enter fullscreen mode Exit fullscreen mode

MongoDB:

npm install errsole errsole-mongodb
Enter fullscreen mode Exit fullscreen mode

MySQL:

npm install errsole errsole-mysql
Enter fullscreen mode Exit fullscreen mode

PostgreSQL:

npm install errsole errsole-postgres
Enter fullscreen mode Exit fullscreen mode

Creating a Custom Logger File

In a real-life Node.js project, you will have multiple files. To use the Errsole Logger in every file of your project, create a logger.js file and initialize Errsole in it:

const errsole = require('errsole');
const ErrsoleSQLite = require('errsole-sqlite');

errsole.initialize({
  storage: new ErrsoleSQLite('/tmp/logs.sqlite')
});

module.exports = errsole;
Enter fullscreen mode Exit fullscreen mode

Now, you can import the logger.js file in every file in your project and use it to log:

const logger = require('./logger');

// Example usage
logger.info('This is an informational message.');
logger.error('This is an error message.');
Enter fullscreen mode Exit fullscreen mode

Understanding Log Levels in Errsole

Errsole Logger functions take the same arguments as console.log. You can provide one or more strings, objects, or variables separated by a comma. Additionally, you can attach metadata to your log messages using the meta function. This metadata can be any contextual information, such as HTTP requests or database query results.

logger.meta({ reqBody: req.body, queryResults: results }).error(err);
logger.meta({ email: req.body.email }).log('User logged in');
Enter fullscreen mode Exit fullscreen mode

Errsole Logger provides functions for five log levels: alert, error, warn, info, and debug.

log / info: Use to log messages or information.

logger.log('Logging a message');
logger.log('Multiple', 'arguments', 'are supported');
logger.log('Logging with a variable:', var1);
logger.log(new Error('An error occurred'));
logger.log('Logging with an error object:', errorObject);
Enter fullscreen mode Exit fullscreen mode

alert: Logs a message and sends a notification to configured channels, such as Email or Slack.

logger.alert('Alert! Something critical happened');
Enter fullscreen mode Exit fullscreen mode

error: Specifically designed to log errors.

logger.error(new Error('An error occurred'));
Enter fullscreen mode Exit fullscreen mode

warn: Logs warning messages.

logger.warn('This is a warning message');
Enter fullscreen mode Exit fullscreen mode

debug: Logs debug information, typically used for troubleshooting during development.

logger.debug('Debugging information');
Enter fullscreen mode Exit fullscreen mode

When to use logger.alert in your code

Whenever your Node.js application crashes, Errsole sends a real-time notification to your development team. This notification includes the error message, the app name, the environment name, and the server name.

Image description

To add critical errors to this notification list, use logger.alert in your code. However, avoid overusing it to prevent flooding your developers with notifications. Use it for critical errors like payment failures or database query failures. Put the error object in the alert function and add all contextual information in the meta function. This allows developers to easily debug critical errors.

logger.meta({ reqBody: req.body, queryResults: results }).alert(err);
Enter fullscreen mode Exit fullscreen mode

Conclusion

Incorporating a complete logging solution into your Node.js application ensures robust log management and efficient debugging. Errsole provides log storage, a built-in dashboard, and real-time notifications, making it a complete logging tool.

To get started with Errsole, visit https://github.com/errsole/errsole.js.

Top comments (14)

Collapse
 
aakanksha_kumari_23 profile image
Info Comment hidden by post author - thread only accessible via permalink
Aakanksha Kumari

By integrating Errsole, your logger does more than just record error and performance data—it automatically captures detailed logs and sends them to a centralized dashboard. This streamlines the process of diagnosing and resolving issues, helping to improve the stability and maintainability of your applications in production.

Collapse
 
esha_lal profile image
Info Comment hidden by post author - thread only accessible via permalink
Esha Lal

Great article! It really explains Errsole as a full-fledged logging tool for Node.js. I really liked the detailed breaking down from real-time notifications on errors to the built-in dashboard. It's good to realize that Errsole can replace traditional log collectors and even enhance error management with its functionality. Will definitely let my colleagues know about this awesome library!

Collapse
 
dinesh_41916 profile image
Info Comment hidden by post author - thread only accessible via permalink
Kuddana Dinesh

This is a great and insightful article that really explains the benefits of using advanced logging tools in Node.js projects.

Errsole is a comprehensive Node.js logger and debugger that combines features like log storage, real-time alerts, dashboards, and error tracking in one tool. It’s ideal for high-traffic applications and simplifies error management. Compared to popular libraries like Winston and Pino, Errsole offers more all-in-one capabilities, making it a great choice for efficient debugging and logging in both large and small projects.

If you’re looking for a comprehensive tool, Errsole is definitely worth considering.
Thank you for this post..

Collapse
 
priyanshi_siwach profile image
Info Comment hidden by post author - thread only accessible via permalink
Priyanshi Siwach

Reading this blog, I got to know how errsole is advantageous over other logging libraries.
It is not just a log collector but provides the whole logging solution, is easy to integrate into our application, enables us to attach meta data to log messages and also has the log level "alert" which is very beneficial for critical errors but should not be overused.

Collapse
 
ankita_mahapatra profile image
Info Comment hidden by post author - thread only accessible via permalink
Ankita Mahapatra

"Thank you for the detailed explanation of Errsole's capabilities. The integrated dashboard, real-time alerts, and error tracking, which together make it a super-advanced logging solution for node.js applications with high traffic, have been highly valued. I’m curious about how Errsole handles performance overhead in large-scale applications with extensive logging, especially when using the alert function frequently." Although it is possible to introduce some minor latency, is there a preferred way to manage it effectively? I am anticipating trying it out in my projects!"

Collapse
 
abhinavraghav4it profile image
Info Comment hidden by post author - thread only accessible via permalink
Abhinav Raghav

This article offers a detailed guide on creating a custom logger for Node.js using Errsole. It highlights the importance of effective logging for real-time error monitoring and debugging, enhancing application reliability and maintainability in production environments.

Collapse
 
raunakkk profile image
Info Comment hidden by post author - thread only accessible via permalink
Raunak Agarwal

A concise guide on integrating Errsole for complete logging in Node.js. It covers setting up custom loggers, managing log storage, and using real-time alerts. Perfect for developers looking for a robust all-in-one logging solution!

Collapse
 
baishnavi_sharma profile image
Baishnavi Sharma

In the case of multiple errors, how does Errsole prioritize the most critical error that needs to be addressed first?

Collapse
 
dinesh_41916 profile image
Kuddana Dinesh • Edited

Great question....

I got to know that , Errsole prioritizes critical errors using the logger.alert function, which sends real-time notifications for high-priority issues like payment or database failures. Developers can include detailed contextual information using logger.meta to help quickly identify and resolve these critical problems. By focusing on key errors without flooding notifications, Errsole ensures that the most urgent issues get addressed first.

I hope this answer is clear and useful.
Thank you

Collapse
 
sampritym profile image
Info Comment hidden by post author - thread only accessible via permalink
Samprity Mukherjee

A complete solutions for logging in Node.js, also providing a log storage, a dashboard and real time notifications for continuous updates. This guide is very helpful to understand what Erssole comes with and how to install and use it in Node.js applications.

Collapse
 
shubham_poddar_6649a42d26 profile image
Info Comment hidden by post author - thread only accessible via permalink
Shubham Poddar

In this blog, we learned that creating a custom logger for a Node.js application helps you track important events, errors, and information in a way that fits your specific needs. By building your own logger, you can format logs, set different log levels (like info, warning, error), and decide where to save these logs (such as in files or a database). This approach makes debugging and monitoring your application easier and more effective.

Collapse
 
shruti_tagade profile image
Info Comment hidden by post author - thread only accessible via permalink
Shruti Tagade • Edited

Thank you for this detailed blog! It helped me understand how Errsole offers a complete logging solution for Node.js, including log storage, a built-in dashboard, and real-time alerts for critical errors. The explanation on using logger.alert and logger.meta to manage and debug critical issues like payment or database failures was particularly insightful.

Collapse
 
baishnavi_sharma profile image
Info Comment hidden by post author - thread only accessible via permalink
Baishnavi Sharma

As per my understanding Errsole is a great library for handling errors and logging. It tends to enhance the developer experience in understanding the potential errors in Node.js application.

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more