在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:koajs/bunyan-logger开源软件地址:https://github.com/koajs/bunyan-logger开源编程语言:JavaScript 100.0%开源软件介绍:bunyan-logger
NOTE: that you're reading the README of the version which targets Koa v2.x, if you want to read about the old 1.X version, switch to Tag 1.3.0. Flexible log context and request logging middleware for koa using bunyan. Inspired by koa-bunyan, koa-json-logger, bunyan-request, and others. A primary goal of this module is to be as flexible as possible, while remaining relatively lightweight, leaving advanced functionality and customization the app. const koa = require('koa');
const koaBunyanLogger = require('koa-bunyan-logger');
const app = koa();
app.use(koaBunyanLogger());
app.use(function (ctx, next) {
ctx.log.info('Got a request from %s for %s', ctx.request.ip, ctx.path);
return next();
});
app.listen(8000); Server:
Client:
Server output:
Request loggingapp.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(koaBunyanLogger.requestLogger()); Server:
Client:
Server output:
Ignoring specific path from loggingIt is possible to skip logs from some endpoints with app.use(koaBunyanLogger.requestLogger({ ignorePath: ['/ping'] })) Suppressing default error stack tracesTo ensure that stack traces from request handling don't get logged in their raw non-JSON forms, you can disable the app's default error handler: app.on('error', function () {}); API ReferencekoaBunyanLogger(logger)Parameters:
ExamplesUse an existing logger: const bunyan = require('bunyan');
const koaBunyanLogger = require('koa-bunyan-logger');
const appLogger = bunyan.createLogger({
name: 'myapp',
level: 'debug',
serializers: bunyan.stdSerializers
});
app.use(koaBunyanLogger(appLogger)); Shortcut to create a new logger: const koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger({
name: 'myapp',
level: 'debug'
})); koaBunyanLogger.requestLogger(opts)Options:
ExamplesBasic usage: app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestLogger()); Add custom fields to include in request and response logs: app.use(koaBunyanLogger.requestLogger({
// Custom fields for both request and response
updateLogFields: function (fields) {
fields.authorized_user = this.user.id;
fields.client_version = this.request.get('X-Client-Version');
},
// Custom fields for response only
updateResponseLogFields: function (fields, err) {
if (err) {
fields.last_db_query = this.db.lastDbQuery();
}
}
})); koaBunyanLogger.requestIdContext(opts)Get X-Request-Id header, or if the header does not exist, generates a random unique id for each request. Options:
Examplesconst koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext()); Or use a different header: const koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext({
header: 'Request-Id'
})); By default, the request id will be accessible as this.reqId and this.request.reqId: const koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(function (ctx) {
ctx.response.set('X-Server-Request-Id', ctx.reqId);
ctx.body = "Hello world";
}); koaBunyanLogger.timeContext(opts)Adds Calls to time() and timeEnd() can be nested or interleaved as long as they're balanced for each label. Options:
Examplesconst koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(koaBunyanLogger.timeContext());
app.use(function (ctx) {
ctx.time('get data');
return getUser()
.then(u => {
return getFriend(u)
.then(f => [u, f]);
})
.then(data => {
let user = data[0];
let friends = data[1];
ctx.timeEnd('get data');
ctx.time('serialize');
ctx.body = serialize(user, friends);
ctx.timeEnd('serialize');
});
}); The same but using async functions const koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(koaBunyanLogger.timeContext());
app.use(async function (ctx) {
ctx.time('get data');
let user = await getUser();
let friends = await getFriend(user);
ctx.timeEnd('get data');
ctx.time('serialize');
ctx.body = serialize(user, friends);
ctx.timeEnd('serialize');
}); Example output: {"name":"koa","hostname":"localhost","pid":9228,"level":10,"label":"get data","duration":102,"msg":"","time":"2014-11-07T01:45:53.711Z","v":0}
{"name":"koa","hostname":"localhost","pid":9228,"level":10,"label":"serialize","duration":401,"msg":"","time":"2014-11-07T01:45:54.116Z","v":0} To return different fields, such as nesting the data under
a single field, add a const koaBunyanLogger = require('koa-bunyan-logger');
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(koaBunyanLogger.timeContext({
updateLogFields: function (fields) {
return {
request_trace: {
name: fields.label,
time: fields.duration
}
};
}
})); bunyan exportThe internal copy of bunyan is exported as const koaBunyanLogger = require('koa-bunyan-logger');
const bunyan = koaBunyanLogger.bunyan; Sponsored byLicense |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论