#Express - Logging The express.js node.js web application framework comes with a built-in logging module called logger which is the connect.js logger. It is really handy to enable and you can use it just like any other Express module. app.use(express.logger());
Without any configuration, the logger middleware will generate a detailed log using what is called the default format. The logger actually supports four predefined log formats: default, short ,tiny, and dev. Each of these predefined formats show various amounts of detail. You can specify one of them this way:
app.use(express.logger('dev'));
If you prefer, you can customize the precise details to be logged using the the following options to format the output of the logger:
Token | Content |
---|---|
:req[header] | The specific HTTP header of the request |
:res[header] | The specific HTTP header of the response |
:http-version | The HTTP version |
:response-time | How long it took to generate the response |
:remote-addr | The user agent's IP address |
:date | Date and time of request |
:method | The HTTP method used for making the request |
:url | The requested URL |
:referrer | The URL that referred the current URL |
:user-agent | The user-agent signature |
:status | The HTTP statusL |
To specify the format just specify it like this:
app.use(express.logger({
format: ':remote-addr :method :url' }
));
By default it will log everything to STDOUT but you can also configure it to log to a file like:
var http = require('http');
var express = require('express');
var fs = require('fs');
var app = express();
app.use(express.logger({
format: 'dev',
stream: fs.createWriteStream('app.log', {'flags': 'w'})
}));
yeah, that's really stupid and confusing. The lack of good logging support out of the box in express is really stupid. There is no single use-case for zero logging support period. Plus Morgan only supports 3 datetime formats, in only UTC, and doesn't allow custom formats.