NodeJs中Express框架使用morgan中間件記錄日誌javascript
Express中的app.js文件已經默認引入了該中間件var logger = require('morgan');java
使用app.use(logger('dev'));能夠將請求信息打印在控制檯,便於開發調試,但實際生產環境中,須要將日誌記錄在log文件裏,可使用以下代碼node
示例:express
Simple app that will log all request in the Apache combined format to STDOUTqnpm
Log出全部Apache請求(結合STTDOUT格式)app
var express = require('express') var morgan = require('morgan') var app = express() app.use(morgan('combined')) app.get('/', function (req, res) { res.send('hello, world!') })
Simple app that will log all request in the Apache combined format to STDOUT框架
var finalhandler = require('finalhandler') var http = require('http') var morgan = require('morgan') // create "middleware"
var logger = morgan('combined') http.createServer(function (req, res) { var done = finalhandler(req, res) logger(req, res, function (err) { if (err) return done(err) // respond to request
res.setHeader('content-type', 'text/plain') res.end('hello, world!') }) })
Simple app that will log all requests in the Apache combined format to the file access.log
.ui
Log列出全部Apache請求到access.log中url
var express = require('express') var fs = require('fs') var morgan = require('morgan') var path = require('path') var app = express() // create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'}) // setup the logger
app.use(morgan('combined', {stream: accessLogStream})) app.get('/', function (req, res) { res.send('hello, world!') })
Simple app that will log all requests in the Apache combined format to one log file per day in the log/
directory using the rotating-file-stream module.spa
天天將全部Apache請求記錄到log中
var express = require('express') var fs = require('fs') var morgan = require('morgan') var path = require('path') var rfs = require('rotating-file-stream') var app = express() var logDirectory = path.join(__dirname, 'log') // ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory) // create a rotating write stream
var accessLogStream = rfs('access.log', { interval: '1d', // rotate daily
path: logDirectory }) // setup the logger
app.use(morgan('combined', {stream: accessLogStream})) app.get('/', function (req, res) { res.send('hello, world!') })
The morgan
middleware can be used as many times as needed, enabling combinations like:
Sample app that will log all requests to a file using Apache format, but error responses are logged to the console:
Log全部Apache請求,錯誤Log到console中
var express = require('express') var fs = require('fs') var morgan = require('morgan') var path = require('path') var app = express() // log only 4xx and 5xx responses to console
app.use(morgan('dev', { skip: function (req, res) { return res.statusCode < 400 } })) // log all requests to access.log
app.use(morgan('common', { stream: fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'}) })) app.get('/', function (req, res) { res.send('hello, world!') })
Sample app that will use custom token formats. This adds an ID to all requests and displays it using the :id
token.
ID Token格式
var express = require('express') var morgan = require('morgan') var uuid = require('node-uuid') morgan.token('id', function getId (req) { return req.id }) var app = express() app.use(assignId) app.use(morgan(':id :method :url :response-time')) app.get('/', function (req, res) { res.send('hello, world!') }) function assignId (req, res, next) { req.id = uuid.v4() next() }