【问题标题】:get the morgan combined format string获取摩根组合格式字符串
【发布时间】:2019-06-12 09:37:02
【问题描述】:

我怎样才能返回摩根组合格式字符串

async function sample(){
  const res = await axios.get("http://localhost:3000/sample")
  const morganFormattedString = morgan('combined', {stream: {write: res => res }}) 
}

res 将是一个对象,我需要将 res 的对象转换为以下字符串,由摩根内部完成

::ffff:127.0.0.1 - - [18/Jan/2019:04:59:10 +0000] "GET /sample HTTP/1.1" 200 2 "http://localhost/" "Mozilla/5.0 (linux) AppleWebKit /537.36(KHTML,像 Gecko)jsdom/11.12.0"

【问题讨论】:

    标签: node.js morgan


    【解决方案1】:

    morgan 返回应该用于记录传入请求的服务器端中间件。您正在尝试使用它client-side 来记录outgoing 请求,这不是它的目的。对于您要完成的工作,axios-debug-log 应该可以工作,但为了让它以combined 格式记录请求,您必须使用configure it yourself

    供参考,this is how morgan currently defines the combined log format:

    morgan.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')
    

    【讨论】:

    • 感谢您的回复,但如果我保存代码 nodemon 将重新启动 index.js 并创建文件夹,我能够以正确的格式记录摩根日志,这就是我编写自定义函数的原因要检查,因为 morgan 返回字符串格式,而开发人员给出的 winston 返回是对象格式。检查这个问题我已经详细解释stackoverflow.com/questions/54250372/…问题是问题中的第二个问题,
    • 您应该提及另一个问题以及您在此问题中遇到的问题的完整描述。
    • 你能检查一下其他问题,所以我可以删除这个问题。我已经把所有的东西都贴在那里了
    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 2016-02-28
    • 2020-03-08
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多