📚 Tutorial🕸️ Express JsAdvance - Logging

Logging

The following guide will using pino for logging.

1. Install pino

npm install pino

2. Config

Adding the highlight section in the express.

server.ts
import pino from "pino";
import express, { Request, Response } from "express";
 
const app = express();
 
// highlight-start
export const logger = pino({
    level: "info",
    transport: {
        targets: [
            {
                target: 'pino/file',
                level: 'error',
                options: { destination: 'error.log' },
            },
            {
                target: 'pino/file',
                level: 'info',
                options: { destination: 'info.log' },
            },
        ]
    }
});
// highlight-end
 
app.get('/', (req: Request, res: Response) => {
    // highlight-start
    logger.info("Hello mate");
    // highlight-end
    res.send('Hello Express!');
});
 
const PORT = 8080;
app.listen(PORT, () => {
  console.log(`Listening at http://localhost:${PORT}/`);
});