【问题标题】:How to get the OriginURL using morgan and winston.js format in nodejs?如何在 nodejs 中使用 morgan 和 winston.js 格式获取 OriginURL?
【发布时间】:2019-03-12 13:55:43
【问题描述】:

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

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 获取 origineURL请问?

【问题讨论】:

    标签: javascript node.js morgan winstone


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,我按照您的建议做了,但什么也没发生,必须对 winston.js 进行更改,我认为可以控制控制台的输出
    • 啊,好的,我知道了,但是还有一个问题,请问有没有办法知道请求的执行时间?
    • 如果您将:response-time ms 添加到您的morgan(),它应该可以满足您的需求:)
    • 是的,谢谢,有没有办法从 URL 中获取参数?
    • 仍然基于文档,您可以创建morgan-token like this 让您可以记录url参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2019-01-25
    • 2018-03-13
    • 2019-06-16
    • 2021-08-02
    • 2018-08-14
    • 2016-10-08
    相关资源
    最近更新 更多