To log Node Express JavaScript to a output file, we use the express.logger
method.
For instance, we write
const logFile = fs.createWriteStream("./myLogFile.log", { flags: "a" });
app.use(express.logger({ stream: logFile }));
to create the logFile
write stream with createWriteStream
.
We call createWriteStream
with the log file path and an object with flag
set to 'a'
for append.
Then we call express.logger
to with an object with stream
set to logFile
to return a middleware that we use in our app to add logging to the file.