Here, we will briefly introduce the application of the morgan module in express through examples. Most examples are directly from the morgan documentation.
1. Usage and parameters
First, install the morgan module:
$ Npm install morgan -- save
Morgan's API:
Morgan (format, options );
Here, format indicates the log format. morgan predefines some log formats, which are represented by constant strings, such as 'combined', 'common', 'short ', and 'dev; options indicates an option, such as outputting logs to a terminal or file. This parameter is optional.
Sample log When format is 'combined:
: 1--[28/Aug/2016: 10: 50: 53 + 0000] "GET/home HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Macintosh; intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
Sample log When format is 'short:
: 1-GET/home HTTP/1.1 304---3.345 MS
2. Application scenario example
2.1 Log output to terminal
Var express = require ('Express ');
Var morgan = require ('Morgan ');
Var app = express ();
App. use (morgan ('Short '));
// App. use (morgan ('Short ', {stream: process. stdout }));
//: 1-GET/home HTTP/1.1 304---3.345 MS
App. get ('/to-stdout', function (req, res, next ){
Res. send ('done .');
});
App. listen (3000 );
When the second parameter of morgan (format, options) is omitted, morgan outputs logs to the terminal by default. Therefore, the default value of the second parameter is {stream: process. stdout }.
2.2 Log output to file
Var express = require ('Express ');
Var morgan = require ('Morgan ');
Var fs = require ('Fs ');
Var path = require ('PATH ');
Var app = express ();
Var accessLogStream = fs. createWriteStream (path. join (_ dirname, 'Access. Log '));
App. use (morgan ('common', {stream: accessLogStream }));
App. get ('/to-file', function (req, res ){
Res. send ('Done! ');
});
App. listen (3000 );
The second parameter of morgan (format, options) needs to be defined to indicate the file output stream.
2.3 logs are output to files and rotated around the clock
Var express = require ('Express ');
Var morgan = require ('Morgan ');
Var fs = require ('Fs ');
Var path = require ('PATH ');
Var fileStreamRotator = require ('file-stream-rotator ');
Var app = express ();
Var logDir = path. join (_ dirname, 'logs ');
// Ensure log directory exists
Fs. existsSync (logDir) | fs. mkdirSync (logDir );
// Create a rotating write stream
Var accessLogStream = fileStreamRotator. getStream ({
Date_format: 'yyyymmdd ',
Filename: path. join (logDir, 'Access-% DATE %. Log '),
Frequency: 'Daily ',
Verbose: true
});
App. use (morgan ('common', {stream: accessLogStream }));
App. get ('/to-rotate-file', function (req, res ){
Res. send ('Done! ');
});
App. listen (3000 );
The file-stream-rotator module is required to create a rotation file stream.
2.4 logs are output to terminals and files at the same time
Load the morgan object that is output to the terminal and file at the same time, such:
App. use (morgan ('combined '));
App. use (morgan ('common', {stream: accessLogStream }));
2.5 Custom log format
Var express = require ('Express ');
Var morgan = require ('Morgan ');
Var uuid = require ('node-uuid ');
Morgan. token ('id', function (req ){
Return req. id;
});
Function assignId (req, res, next ){
Req. id = uuid. v4 ();
Next ();
};
Var app = express ();
App. use (assignId );
App. use (morgan (': id: method: url: response-time ms '));
App. get ('/mizmized-format', function (req, res ){
Res. send ('Done! ');
});
App. listen (3000 );
Use uuid to identify each request. The log is as follows:
A08516c4-201a-4d8a-9f22-d1b6de5e225f GET/customized-format 3.118 MS