pino logger

오픈소스·2023년 6월 6일
0
post-thumbnail

https://getpino.io

$ npm i pino
$ npm i pino-http-send

logger webhook

  • webhook.mjs

    import express from 'express';
    import bodyParser from 'body-parser';
    
    const app = express();
    
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    
    app.post('/', function (req, res) {
      console.log(req.body);
      res.send('hello world');
    });
    
    app.listen(8080);

$ node webhook.mjs

pino application

  • app.mjs

    import pino from 'pino';
    
    const transport = pino.transport({
      targets: [
        {
          target: './stream.mjs',
          level: 'error',
          options: { destination: 'http://localhost:8080' },
        },
        {
          target: 'pino-pretty',
          level: 'debug',
          options: { destination: 1 },
        },
      ],
    });
    
    const logger = pino({ level: 'debug' }, transport);
    
    logger.debug('debug log!');
    logger.info('info log!');
    logger.error('error log!');
  • stream.mjs

    import { createWriteStream } from 'pino-http-send';
    
    export default (options) => createWriteStream({
      url: options.destination,
    })

$ node app.mjs


실행 결과

webhook

$ node webhook.mjs 
{
  logs: [
    {
      level: 50,
      time: 1686037588315,
      pid: 54307,
      hostname: 'lyuyeong-giui-MacBookPro.local',
      msg: 'error log!'
    }
  ]
}

application

$ node app.mjs  
[16:46:28.315] DEBUG (54307): debug log!
[16:46:28.315] INFO (54307): info log!
[16:46:28.315] ERROR (54307): error log!

0개의 댓글