Sometimes, we want to use the Node.js Morgan logger.
In this article, we’ll look at how to use the Node.js Morgan logger.
How to use the Node.js Morgan logger?
To use the Node.js Morgan logger, we can use it with log4js
.
For instance, we write
const express = require("express");
const log4js = require("log4js");
const morgan = require("morgan");
//...
const theAppLog = log4js.getLogger();
const theHTTPLog = morgan({
"format": "default",
"stream": {
write: (str) => {
theAppLog.debug(str);
}
}
});
//...
const theServer = express();
theServer.use(theHTTPLog);
to create a logger instance with log4js.getLogger
.
Then we call morgan
with an object sets some options.
We set stream.write
to a function that calls theAppLog.debug
to use log4js
with morgan
to do the logging.
And then we call use
with theHTTPLog
to use the logger created by morgan
to do the logging.
Conclusion
To use the Node.js Morgan logger, we can use it with log4js
.