Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in express js to a output file?

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 logger automatically logs the request and responses. I need to log some application data into the log files. Can this be done using express.logger?

Regards, Lalith

like image 499
Lalith Avatar asked Mar 30 '11 17:03

Lalith


People also ask

Where is node JS log file?

Answer. Note: Node. js can also records logs directly into /var/www/vhosts/system/example.com/logs/ directory, but this functionality is a part of Passenger Enterprise which is not included into the free edition that is provided by Plesk.

How do I enable logging in node JS?

Logging in Node. By default, it will not produce any output. To enable this logger, you have run your application with a special environment variable, called DEBUG . Once you do that, the debug module will come to life and will start producing log events for stdout.


1 Answers

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.

like image 149
Did Avatar answered Sep 30 '22 20:09

Did