在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:expressjs/morgan开源软件地址:https://github.com/expressjs/morgan开源编程语言:JavaScript 100.0%开源软件介绍:morganHTTP request logger middleware for node.js
InstallationThis is a Node.js module available through the
npm registry. Installation is done using the
$ npm install morgan APIvar morgan = require('morgan') morgan(format, options)Create a new morgan logger middleware function using the given The Using a predefined format stringmorgan('tiny') Using format string of predefined tokensmorgan(':method :url :status :res[content-length] - :response-time ms') Using a custom format functionmorgan(function (tokens, req, res) {
return [
tokens.method(req, res),
tokens.url(req, res),
tokens.status(req, res),
tokens.res(req, res, 'content-length'), '-',
tokens['response-time'](req, res), 'ms'
].join(' ')
}) OptionsMorgan accepts these properties in the options object. immediateWrite log line on request instead of response. This means that a requests will be logged even if the server crashes, but data from the response (like the response code, content length, etc.) cannot be logged. skipFunction to determine if logging is skipped, defaults to // EXAMPLE: only log error responses
morgan('combined', {
skip: function (req, res) { return res.statusCode < 400 }
}) streamOutput stream for writing log lines, defaults to Predefined FormatsThere are various pre-defined formats provided: combinedStandard Apache combined log output.
commonStandard Apache common log output.
devConcise output colored by response status for development use. The
shortShorter than default, also including response time.
tinyThe minimal output.
TokensCreating new tokensTo define a token, simply invoke morgan.token('type', function (req, res) { return req.headers['content-type'] }) Calling The token function is expected to be called with the arguments :date[format]The current date and time in UTC. The available formats are:
If no format is given, then the default is :http-versionThe HTTP version of the request. :methodThe HTTP method of the request. :referrerThe Referrer header of the request. This will use the standard mis-spelled Referer header if exists, otherwise Referrer. :remote-addrThe remote address of the request. This will use :remote-userThe user authenticated as part of Basic auth for the request. :req[header]The given :res[header]The given :response-time[digits]The time between the request coming into The :statusThe status code of the response. If the request/response cycle completes before a response was sent to the
client (for example, the TCP socket closed prematurely by a client aborting
the request), then the status will be empty (displayed as :total-time[digits]The time between the request coming into The :urlThe URL of the request. This will use :user-agentThe contents of the User-Agent header of the request. morgan.compile(format)Compile a format string into a The function returned from Normally formats are defined using Examplesexpress/connectSample app that will log all request in the Apache combined format to STDOUT var express = require('express')
var morgan = require('morgan')
var app = express()
app.use(morgan('combined'))
app.get('/', function (req, res) {
res.send('hello, world!')
}) vanilla http serverSample 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!')
})
}) write logs to a filesingle fileSample app that will log all requests in the Apache combined format to the file
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!')
}) log file rotationSample app that will log all requests in the Apache combined format to one log
file per day in the var express = require('express')
var morgan = require('morgan')
var path = require('path')
var rfs = require('rotating-file-stream') // version 2.x
var app = express()
// create a rotating write stream
var accessLogStream = rfs.createStream('access.log', {
interval: '1d', // rotate daily
path: path.join(__dirname, 'log')
})
// setup the logger
app.use(morgan('combined', { stream: accessLogStream }))
app.get('/', function (req, res) {
res.send('hello, world!')
}) split / dual loggingThe
Sample app that will log all requests to a file using Apache format, but error responses are logged to the 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!')
}) use custom token formatsSample app that will use custom token formats. This adds an ID to all requests and displays it using the 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()
} License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论