Fastify is a fast and low-overhead web framework for Node.js, designed for building efficient and scalable server-side applications. It is written in JavaScript and aims to provide the best developer experience with the least overhead. Fastify comes with a powerful plugin architecture and a focus on performance. One important part of developing applications with Fastify, or with any other back-end framework, is logging.
Why Logging Is Important for Developers
Debugging and Troubleshooting the Detection of Errors: Logging helps in the identification and understanding of errors and exceptions that occur during the application's runtime. By reading these log entries, developers can identify the source of a problem and make a decision on the best course of action to take.
Context Information: Logs allow for context about the application state when the error occurred. Context information could be a set of request parameters, user sessions, or system states that can be used to diagnose complex issues.
Choosing the Right Logging Library for Fastify
When building applications with Fastify, effective logging is essential for debugging, monitoring, and maintaining your application. There are several open-source logging libraries available, each offering unique features. Some popular choices include Winston, Pino, Morgan, Bunyan, Log4js, and Errsole. The other ones will give you custom logging, but Errsole has a few key benefits that you might want to think about.
Errsole comes with a built-in dashboard, allowing for easy visualization and management of logs without requiring additional integrations. Errsole also automatically captures standard logs, ensuring comprehensive coverage with minimal effort. Furthermore, it includes a notification system that sends real-time alerts via Slack and email, complete with error context, enabling quick responses to issues. These features make Errsole an excellent choice for integrating effective logging into your Fastify application, ensuring a more reliable and maintainable system.
Step-by-Step Guide
- Install Errsole and Dependencies First, you need to install Errsole and your choice of storage adapter. In this guide, we will use SQLite to store logs locally as a file. However, Errsole supports many popular databases, so you can select the one that best meets your requirements.
npm install errsole errsole-sequelize sqlite3
2. Initialize Errsole
Insert the Errsole initialization code at the beginning of your Fastify app's main file (app.js / app.ts).
// ESM or TS
import errsole from 'errsole';
import ErrsoleSequelize from 'errsole-sequelize';
errsole.initialize({
storage: new ErrsoleSequelize({
dialect: 'sqlite',
storage: '/tmp/logs.sqlite'
})
});
Example (app.js)
import errsole from 'errsole';
import ErrsoleSequelize from 'errsole-sequelize';
import Fastify from 'fastify';
import expressPlugin from '@fastify/express';
// Insert the Errsole code snippet at the beginning of your app's main file
errsole.initialize({
storage: new ErrsoleSequelize({
dialect: 'sqlite',
storage: '/tmp/logs.sqlite'
})
});
const fastify = Fastify();
await fastify.register(expressPlugin);
// Start the server
try {
await fastify.listen({ port: 3000 });
} catch (err) {
console.error(err);
process.exit(1);
}
Accessing the Web Dashboard
After setting up Errsole, errsole will start capturing the logs automatically and you can access the Errsole Web Dashboard:
Local Environment: Open your web browser and visit http://localhost:8001/.
Remote Server: If your remote app is hosted at https://api.example.com, visit https://api.example.com:8001.
Errsole provides full flexibility, allowing you to modify the port, path, and domain to keep your dashboard secure and hidden from others.
For more details about advanced configurations and other features, please refer to the official documentation. README
Top comments (2)
What other logging libraries have you tried with Fastify, and how do they compare to Errsole in terms of performance and features?
I have tried many including winston, morgan, and pino and no one has built-in dashboard.