Introduction to Pino Pretty
Pino Pretty is a powerful, flexible, and high-performance logging library for Node.js that provides pretty-printing capabilities for the pino
logger. It is designed to format and colorize the output logs to make them more readable.
Installation
npm install pino-pretty
Basic Usage
To use Pino Pretty, you can pipe the output of pino
to pino-pretty
:
const pino = require('pino');
const logger = pino({
prettyPrint: { colorize: true }
});
logger.info('Hello, this is an informational message');
logger.error('This is an error message');
API Examples
Custom Timestamp Formatting
You can customize the timestamp format using translateTime
:
const logger = pino({
prettyPrint: { translateTime: 'SYS:standard' }
});
logger.info('Custom timestamp formatting');
Level First Output
To display the log level as the first element in the log message:
const logger = pino({
prettyPrint: { levelFirst: true }
});
logger.warn('The log level appears first');
Custom Message Format
Format the log message in a custom way:
const logger = pino({
prettyPrint: {
messageFormat: '{msg} - pid:{pid} - hostname:{hostname}'
}
});
logger.info('Custom message format example');
Pino Pretty with Express.js
Here’s a comprehensive example of using Pino Pretty in an Express.js application:
const express = require('express');
const pino = require('pino');
const pinoHttp = require('pino-http');
const logger = pino({
prettyPrint: { colorize: true, translateTime: true }
});
const app = express();
app.use(pinoHttp({ logger }));
app.get('/', (req, res) => {
req.log.info('Hello world request');
res.send('Hello, world!');
});
app.listen(3000, () => {
logger.info('Server started on http://localhost:3000');
});
With Pino Pretty and Express.js, you can ensure that your server logs are colorful, readable, and well-formatted, making debugging and monitoring a breeze.
Hash: 73b92c81bbe390c361ad8f5ee16af2ad5b308431c2673620cac5ffec8e762fc3