Logging in express js to a output file?

后端 未结 7 1124
醉话见心
醉话见心 2020-12-12 23:26

What is best way to log my express js webserver? The inbuilt express.logger() just displays logs on screen. Can I also log them into a file in /log folder? Also the current

7条回答
  •  时光取名叫无心
    2020-12-13 00:21

    For HTTP request logging: https://github.com/expressjs/morgan#write-logs-to-a-file

    var express = require('express')
    var fs = require('fs')
    var morgan = require('morgan')
    
    var app = express()
    
    // create a write stream (in append mode)
    var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
    
    // setup the logger
    app.use(morgan('combined', {stream: accessLogStream}))
    
    app.get('/', function (req, res) {
      res.send('hello, world!')
    })
    

提交回复
热议问题