I've just created an app from express generator and it uses two loggers debug
and morgan
:
/bin/www.js
const debug = require('debug')('myapp:server');
....
server.on('listening', onListening);
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
--^^^^^^^--
}
/app.js
var logger = require('morgan');
...
app.use(logger('dev'));
Why use both? Can't only one of them be used for both purposes?
morgan
is a library you typically attach as middleware, and then it automatically logs information as requests flow through your system, especially in regards to timing it seems. I don't believe it is something you would typically invoke manually for additional logging.
debug
is something you would add in manually with additional information that morgan
doesn't necessarily know or care about. You can also turn debug
logging on and off through the use of a regular expression in an environment variable, which is useful for limiting namespaced logs down to the ones you care about when debugging an issue. See here.
So sure, you could use debug
to log the same way and the same things morgan
does, but you'd have to do it all by hand (for better or worse).
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