Search code examples
javascriptnode.jsmorganwinstone

How to get the OriginURL using morgan and winston.js format in nodejs?


I'm trying to get the originalURL from the message attribute using winston.js and morgan in a nodejs project (the code):

winstone.je

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp}  ${level}: ${message}`;
});

const logger = createLogger({
  format: combine(
    label({ label: 'right meow!' }),
    timestamp(),
    myFormat
  ),
  transports: [new transports.Console()]
});

app.js

app.use(morgan('combined', { stream: winston.stream }));

the output of is :

2019-03-12T13:35:50.112Z : ::1 - - [12/Mar/2019:13:35:50 +0000] "GET /api/bar/origin/000a  HTTP/1.1"

what I'm looking for is to get just this part : "GET /api/bar/origin/000a HTTP/1.1" without using substring or slice on the message attribute, Is there a way the get origineURL from myFormat const please ?


Solution

  • Well, based on the Morgan documentation you can change the output format either using a predefined format (combined, tiny, ...) or make your own which should look like app.use(morgan(':method :url HTTP/:http-version', { stream: winston.stream }));