0
votes

I am trying to save all the incoming request and their response into the database.

I am trying to achieve this using express-winston npm package and it is logging response body and request in console. but I wanna save it in database. how can I do this????

var winston = require("winston");
var expressWinston = require("express-winston");

expressWinston.requestWhitelist.push("body");
// expressWinston.responseWhitelist.push("body");
app.use(
  expressWinston.logger({
    transports: [
      new winston.transports.Console({
        json: true,
        colorize: true
      })
    ]
  })
);
expressWinston.responseWhitelist.push("body");

I am getting this log in console.

{"level":"info","message":"HTTP GET /ideas/category/hello","meta":{"res":{"statusCode":200,"body":{"hiiii":"djdjjdjdjdd"}},"req":{"url":"/ideas/category/hello","headers":{"host":"localhost:5000","connection":"keep-alive","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3","accept-encoding":"gzip, deflate, br","accept-language":"en-GB,en;q=0.9,en-US;q=0.8,hi;q=0.7","cookie":"connect.sid=s%3A90PCwDOrv0v9yeSYZq-4I4Quactbcduq.8EpLenv6t1lCPqLcV5R1o1%2FF15p9kEBqrZ82FBHiYJA"},"method":"GET","httpVersion":"1.1","originalUrl":"/ideas/category/hello","query":{}},"responseTime":2}}

1
You'll have to add a transport to save to your database. You could use some module or a custom transport depending on your database.Stephen S

1 Answers

0
votes

After doing some research I got my answer. we can achieve this using winston-sql-transport npm package.see the code given below:

const { Logger } = require('winston');
const { SQLTransport } = require('./../lib/winston-sql-transport');

const logger = new Logger({
  transports: [
    new SQLTransport({
      tableName: 'winston_logs',
    })]
});

module.exports = logger;