Logging in express js to a output file?

后端 未结 7 1152
醉话见心
醉话见心 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

    To send the express or connect logs to a file use Node's writeStream. For example to send the express logs to ./myLogFile.log :

    open the stream to your file in append mode with :

    var logFile = fs.createWriteStream('./myLogFile.log', {flags: 'a'}); //use {flags: 'w'} to open in write mode
    

    then, in your express config use :

    app.use(express.logger({stream: logFile}));
    

    should also work for connect.logger.

提交回复
热议问题