$ npm i pino
$ npm i pino-http-send
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
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
$ node webhook.mjs
{
logs: [
{
level: 50,
time: 1686037588315,
pid: 54307,
hostname: 'lyuyeong-giui-MacBookPro.local',
msg: 'error log!'
}
]
}
$ 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!