如何在nodejs中使用morgan和winston.js格式获取OriginURL?

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

我正在尝试在 nodejs 项目(代码)中使用 winston.js 和 morgan 从消息属性中获取原始 URL:

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

的输出是:

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

我正在寻找的是只获取这一部分:"GET /api/bar/origin/000a HTTP/1.1" 不在消息属性上使用子字符串或切片,请问有没有办法从 myFormat const 获取 originineURL?

好吧,基于 Morgan documentation,您可以使用预定义格式(combinedtiny、...)更改输出格式,或者制作您自己的格式喜欢 app.use(morgan(':method :url HTTP/:http-version', { stream: winston.stream }));