Timestamp: to know when things happened
The computer/server name: if running a distributed system
The process ID: When running multiple Node processes using handlers such aspm2
The message: a message in itself, with content
A call trace: in case of logging an error
Potentially some additional variables/information
stdout Additionally, now that we know that everything is going to and anyway stderr, we're probably going to want different logging levels and have the ability to configure and filter our logs based on those.
We can achieve all of this by accessing different parts of process philippines mobile number example and writing a lot of JavaScript, but what's really cool about Node.js is that we have the ecosystem npm and there are already a lot of libraries that we can use. Here are some examples:
bunyan (Notez toutefois que celle-ci n'a pas été mise à jour depuis 2 ans)
Personally, I like it pino because it is fast and has a good ecosystem. Let's see how using pino can help us with logging. The good thing is that there is already a package express-pino-logger that we can use to log requests.
app.listen(PORT, () => {
logger.info('Server running on port %d', PORT);
});
In this snippet, we created an instance logger of pino and passed it to express-pino-logger to create a new logging middleware to call with app.use. Additionally, we replaced console.log on server startup with logger.info and added an logger.debug additional to our route to display different logging levels.
If you restart your server by running again node index.js, you will see quite a different output printing a JSON for each line. Go to again and you will notice another line of JSON added.
Screenshot Examples of pino logs from an HTTP request
If you inspect this JSON, you will see that it contains all of the above information, such as the timestamp. You may also notice that our entry logger.debug is not printed, which is because we need to change our default logging level to make it visible. When we created our instance logger, we set the value to process.env.LOG_LEVEL, which means we can change the value through that, or accept the default of info. By running LOG_LEVEL=debug node index.js, we can adjust the logging level.
Timestamp: to know when things happened
-
- Posts: 10
- Joined: Sun Dec 22, 2024 5:31 am