在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:pinojs/pino-http开源软件地址:https://github.com/pinojs/pino-http开源编程语言:JavaScript 85.9%开源软件介绍:pino-httpHigh-speed HTTP logger for Node.js To our knowledge, BenchmarksBenchmarks log each request/response pair while returning
All benchmarks where taken on a Macbook Pro 2013 (2.6GHZ i7, 16GB of RAM). Install
Example'use strict'
const http = require('http')
const server = http.createServer(handle)
const logger = require('pino-http')()
function handle (req, res) {
logger(req, res)
req.log.info('something else')
res.end('hello world')
}
server.listen(3000)
APIpinoHttp([opts], [stream])
ExamplesLogger options'use strict'
const http = require('http')
const server = http.createServer(handle)
const pino = require('pino')
const logger = require('pino-http')({
// Reuse an existing logger instance
logger: pino(),
// Define a custom request id function
genReqId: function (req) { return req.id },
// Define custom serializers
serializers: {
err: pino.stdSerializers.err,
req: pino.stdSerializers.req,
res: pino.stdSerializers.res
},
// Set to `false` to prevent standard serializers from being wrapped.
wrapSerializers: true,
// Logger level is `info` by default
useLevel: 'info',
// Define a custom logger level
customLogLevel: function (req, res, err) {
if (res.statusCode >= 400 && res.statusCode < 500) {
return 'warn'
} else if (res.statusCode >= 500 || err) {
return 'error'
} else if (res.statusCode >= 300 && res.statusCode < 400) {
return 'silent'
}
return 'info'
},
// Define a custom success message
customSuccessMessage: function (req, res) {
if (res.statusCode === 404) {
return 'resource not found'
}
return `${req.method} completed`
},
// Define a custom receive message
customReceivedMessage: function (req, res) {
return 'request received: ' + req.method
},
// Define a custom error message
customErrorMessage: function (req, res, err) {
return 'request errored with status code: ' + res.statusCode
},
// Override attribute keys for the log object
customAttributeKeys: {
req: 'request',
res: 'response',
err: 'error',
responseTime: 'timeTaken'
},
// Define additional custom request properties
customProps: function (req, res) {
return {
customProp: req.customProp,
// user request-scoped data is in res.locals for express applications
customProp2: res.locals.myCustomData
}
}
})
function handle (req, res) {
logger(req, res)
req.log.info('something else')
res.end('hello world')
}
server.listen(3000) PinoHttp.logger (P.Logger)The const pinoHttp = require('pinoHttp')();
pinoHttp.logger.level = 'silent'; pinoHttp.startTime (Symbol)The const http = require('http')
const logger = require('pino-http')()
const someImportantThingThatHasToBeFirst = require('some-important-thing')
http.createServer((req, res) => {
res[logger.startTime] = Date.now()
someImportantThingThatHasToBeFirst(req, res)
logger(req, res)
res.end('hello world')
}).listen(3000) Custom formattersYou can customize the format of the log output by passing a Pino transport. const logger = require('pino-http')({
quietReqLogger: true, // turn off the default logging output
transport: {
target: 'pino-http-print', // use the pino-http-print transport and its formatting output
options: {
destination: 1,
all: true,
translateTime: true
}
}
}) Default serializerspinoHttp.stdSerializers.reqGenerates a JSONifiable object from the HTTP It returns an object in the form: {
pid: 93535,
hostname: 'your host',
level: 30,
msg: 'my request',
time: '2016-03-07T12:21:48.766Z',
v: 0,
req: {
id: 42,
method: 'GET',
url: '/',
headers: {
host: 'localhost:50201',
connection: 'close'
},
remoteAddress: '::ffff:127.0.0.1',
remotePort: 50202
}
} pinoHttp.stdSerializers.resGenerates a JSONifiable object from the HTTP It returns an object in the form: {
pid: 93581,
hostname: 'myhost',
level: 30,
msg: 'my response',
time: '2016-03-07T12:23:18.041Z',
v: 0,
res: {
statusCode: 200,
header: 'HTTP/1.1 200 OK\r\nDate: Mon, 07 Mar 2016 12:23:18 GMT\r\nConnection: close\r\nContent-Length: 5\r\n\r\n'
}
} Custom serializersEach of the standard serializers can be extended by supplying a corresponding
custom serializer. For example, let's assume the const logger = require('pino-http')({
serializers: {
req (req) {
Object.keys(req.raw).forEach((k) => {
if (k.startsWith('foo')) {
req[k] = req.raw[k]
}
})
return req
}
}
}) If you prefer to work with the raw value directly, or you want to honor the custom
serializers already defined by const logger = require('pino-http')({
wrapSerializers: false,
serializers: {
req (req) {
// `req` is the raw `IncomingMessage` object, not the already serialized request from `pino.stdSerializers.req`.
return {
message: req.foo
};
}
}
}) Logging request bodyLogging of requests' bodies is disabled by default since it can cause security risks such as having private user information (password, other GDPR-protected data, etc.) logged (and persisted in most setups). However if enabled, sensitive information can be redacted as per redaction documentation. Furthermore, logging more bytes does slow down throughput. This video by pino maintainers Matteo Collina & David Mark Clements goes into this in more detail. After considering these factors, logging of the request body can be achieved as follows: const http = require('http')
const logger = require('pino-http')({
serializers: {
req(req) {
req.body = req.raw.body;
return req;
},
},
}); Custom serializers + custom log attribute keysIf custom attribute keys for
TeamMatteo Collinahttps://www.npmjs.com/~matteo.collina https://twitter.com/matteocollina David Mark Clementshttps://github.com/davidmarkclements https://www.npmjs.com/~davidmarkclements https://twitter.com/davidmarkclem AcknowledgementsThis project was kindly sponsored by nearForm. Logo and identity designed by Beibhinn Murphy O'Brien: https://www.behance.net/BeibhinnMurphyOBrien. LicenseMIT |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论