Welcome to this comprehensive guide on Structured Logging using JSON in Node.js! In this lesson, we will explore the importance of structured logging, understand how to implement it using JSON, and see practical examples that will help you in real-world projects.
Structured logging provides a consistent format for logs, making it easier to analyze, search, and understand the generated logs. Unlike unstructured logs, which are often text-based, structured logs contain well-defined fields that help in extracting valuable insights from the data.
Before we dive into the implementation, let's ensure you have Node.js installed on your system. If not, follow this guide to install it.
Node.js provides several libraries for structured logging. We will use the winston library in this tutorial.
Install the library by running:
npm install winstonNow, let's create a new file named app.js and implement structured logging using JSON.
const winston = require('winston');
// Create a custom logger instance
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
// - Write all logs with level `info` and above to `combined.log`
new winston.transports.File({ filename: 'combined.log', level: 'info' }),
// - Write only `error` and `critical` logs to `error.log`
new winston.transports.File({ filename: 'error.log', level: 'error' })
]
});
// Log an info message
logger.info('Welcome to structured logging with JSON!');
// Log an error message
logger.error('An error occurred while processing the data.');In the above example, we created a custom logger instance using the winston.createLogger() method. We defined the log format as JSON using winston.format.json() and specified the transports to store the logs in files.
š Note: You can change the log level (level) of your logger to control which logs are written to the files.
In this practical example, we will create a simple web server that logs requests using structured logging.
First, let's create a new file named server.js:
const express = require('express');
const winston = require('winston');
// Create a custom logger instance
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console(), // Log to the console
]
});
const app = express();
// Log every request using the custom logger
app.use((req, res, next) => {
const { method, url } = req;
logger.info({ method, url });
// Continue with the request
next();
});
// Sample endpoint to test the logger
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// Start the server
app.listen(3000, () => {
logger.info('Server started on port 3000.');
});This example creates a simple web server using Express.js that logs every request using the custom logger we created earlier. Save this file and run it using:
node server.jsNow, if you access the server at http://localhost:3000, the request will be logged in the console.
Which library are we using for structured logging in this tutorial?
We hope you found this lesson helpful! In the next lesson, we will delve deeper into structured logging, discuss formatting options, and explore how to send logs to external services like Splunk and Loggly.
Stay tuned and happy learning! š