在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:pablosichert/concurrency-logger开源软件地址:https://github.com/pablosichert/concurrency-logger开源编程语言:JavaScript 100.0%开源软件介绍:concurrency-loggerHTTP logging middleware especially useful to unwind concurrent operations without losing the request context
Install
UsagekoaWithBasic usageimport Koa from 'koa';
import createLogger from 'concurrency-logger';
const app = new Koa;
// Logger is stateful as it contains information about concurrent requests
// Same instance needs to be reused across requests
const logger = createLogger(/* options */);
app.use(logger); Log from middleware// Log something in context to a specific request to trace it back easily -
// also when there are multiple concurrent requests
app.use(async (context, next) => {
context.log('Log!');
context.log.info('Info!');
context.log.error('Error!');
await next();
}); context to the logAttach moreconst logger = createLogger({
req: context => (
context.originalUrl + '\n' +
context.get('User-Agent')
)
}); Include localized timestampsconst logger = createLogger({
timestamp: true
}); Write log to fileimport { createWriteStream } from 'fs';
// To read log use program that interprets ANSI escape codes,
// e.g. cat or less -r
const log = createWriteStream('logs/requests.log');
const logger = createLogger({
reporter: log
}); Adjust alert levels per method and response timeconst logger = createLogger({
getLevel: (responseTime, context) => {
/*
GET
0 - 99ms: 0
100 - 149ms: 1
150 - 199ms: 2
200 - 249ms: 3
250 - 299ms: 4
300 - 349ms: 5
> 350ms : 6
POST
0 - 149ms: 0
150 - 225ms: 1
... : ...
*/
let threshold = 50; // ms
if (['POST', 'PUT'].includes(context.method)) {
threshold *= 1.5;
}
return Math.floor(responseTime / threshold) - 1;
}
}); Standaloneimport createLogger from 'concurrency-logger';
const logger = createLogger(/* options */);
(async () => {
const context = {
method: 'GET',
originalUrl: '/'
};
const next = async () => {
await new Promise(resolve => setTimeout(resolve, 100));
context.status = 200;
};
try {
await logger(context, next);
} catch (error) {
// Errors are passed through
}
})(); API
DevelopingInstall development dependencies
Create new fixtures to test against
Manually review fixtures (you need a program that renders ANSI escape codes)
Run tests
Run code linter
Compile to ES5 from /src to /lib
Initialize demo project
Build demo
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论