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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With