Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Node.js is a popular runtime platform to create programs that run on it.
It lets us run JavaScript outside the browser.
In this article, we’ll look at how to start using Node.js to create programs.
Logging with Morgan
To let us debug our Node.js apps easily, we can use the morgan
package to add a logger into our app.
To install it, we run:
npm i morgan
Then we can use it by writing:
const Morgan = require('morgan'),
Router = require('router'),
http = require('http');
router = new Router();
router.use(Morgan('tiny'));
http.createServer(function(request, response) {
router(request, response, function(error) {
if (!error) {
response.writeHead(404);
} else {
console.log(error.message, error.stack);
response.writeHead(400);
}
response.end('n');
});
}).listen(8000);
We add the Morgan
middleware into our router
by calling the router.use
method with the Morgan('tiny')
middleware.
Now we see logging in our app.
Now we can remove the console.log
and write:
const Morgan = require('morgan'),
Router = require('router'),
http = require('http');
router = new Router();
router.use(Morgan('tiny'));
http.createServer(function(request, response) {
router(request, response, function(error) {
let info = process.versions;
info = JSON.stringify(info);
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(info);
});
}).listen(8000);
and still get logging.
Alternatively, we can use the bunyan
package to do the logging.
To install it, we run:
npm i bunyan
Then we can use it by writing:
const Bunyan = require('bunyan');
const logger = Bunyan.createLogger({
name: 'example'
});
logger.info('Hello logging');
We require the bunyan
package. Then we create our logger with the createLogger
method.
The name
is the name of the logger.
Then we use the logger.info
method to log anything we want.
We can log items with other levels of logging by using various methods.
For example, we can write:
const Bunyan = require('bunyan');
const logger = Bunyan.createLogger({
name: 'example'
});
logger.info('Hello logging');
logger.trace('Trace');
logger.debug('Debug');
logger.info('Info');
logger.warn('Warn');
logger.error('Error');
logger.fatal('Fatal');
Then we get object with various values of the level
property logged to let us know the severity level of the log item.
We can also write:
const Bunyan = require('bunyan');
const logger = Bunyan.createLogger({
name: 'example',
level: Bunyan.TRACE
});
logger.info('Hello logging');
logger.trace('Trace');
logger.debug('Debug');
logger.info('Info');
logger.warn('Warn');
logger.error('Error');
logger.fatal('Fatal');
to set the level in when we call createLogger
.
With Bunyan, we can also write the logged data to a file.
To do that, we write:
const Bunyan = require('bunyan');
const logger = Bunyan.createLogger({
name: 'example',
streams: [{
level: Bunyan.INFO,
path: './log.log'
},
{
level: Bunyan.INFO,
stream: process.stdout
}
]
});
logger.info('Hello logging');
logger.trace('Trace');
logger.debug('Debug');
logger.info('Info');
logger.warn('Warn');
logger.error('Error');
logger.fatal('Fatal');
We add the streams
property to add objects to specify where the log data goes.
The first entry of the streams
array has the path
property to specify the location to write to.
The 2nd object specifies that we write to stdout, which means we print the items on the screen.
Conclusion
We can add logging with various loggers.
We can use Morgan or Bunyan to add logging capabilities to our Node.js apps.