Search code examples
node.jswebhooks

Webhook logging query


So I have this test webhook setup to capture a few events, how do I add logging so it saves all output to a file and have it save a daily log?

const express = require('express');
const bodyParser = require('body-parser');
 
const app = express();
app.use(bodyParser.json());
 
app.get('/', (req, res) => {
    res.send('Webhook server is running');
});
 
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Webhook server is listening on port ${PORT}`);
});
 
app.post('/webhook', (req, res) => {
    console.log('Webhook event received:', req.body);
    res.status(200).send('OK');
});
app.post('/event', function (req, res) {
  var events = req.body;
  events.forEach(function (event) {
    
      processEvent(event);
  });
});

Trying to get logging enabled?


Solution

  • If you are going with morgan it will be something like

    app.use(morgan('common', {stream: fs.createWriteStream('./webhook.log', {flags: 'a'})}));