0

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 ?

1 Answers1

0

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 }));

mJehanno
  • 836
  • 1
  • 12
  • 19
  • Thanks for your answer, I did what u suggested but nothing happened, the change must be done to winston.js I think to controle the output of the console –  Mar 12 '19 at 14:48
  • Ah, Ok I got it, but another question pleasr is there a way to know the executed time of the request please ? –  Mar 12 '19 at 14:52
  • if you add `:response-time ms` to your `morgan()` it should do what you want :) – mJehanno Mar 12 '19 at 14:56
  • yes I did that thanks, is there a way to take args from the URL ? –  Mar 12 '19 at 15:11
  • still based on the documentation, you can create `morgan-token` [like this](https://github.com/expressjs/morgan#use-custom-token-formats) giving you the possibility to log url parameters. – mJehanno Mar 12 '19 at 15:19
  • its not easy this time, the args are passed in the controller, how i can match between app.js and ctr ? –  Mar 12 '19 at 15:32