node服务端开发中少不了日志打点,而在koa框架下的日志打点在多进程环境中日志信息往往无法对应上下文,而且在高并发下直接进行写buffer操作(内核调用writev)也会造成内存泄漏,因此Midlog就是为了缓解这种问题而产生的,其采用多种缓冲调度策略尽可能降低writev的代价,减缓内存溢出的速率。
日志系统的三个关键要素:稳定、性能和易用。
当前log4js遇到的问题。
设计新系统的前提。
回顾node原生模块--“流”的读与写,涉及到了多个缓冲区以及他们缓冲区的实现(stream2.0的 Transform流同时包括了读写缓冲区)
优化点与优化方式。
Midlog系统架构图,其中reqContainer收集同一个上下文的日志信息并转码;
StrategyMaker负责制定双缓冲缓存策略和超时刷新以及强制刷新策略;
Writeable则负责真正的写buffer操作,内部通过链表保存数据;
详细介绍每个模块的功能。
app.js
var koa = require('koa');
var midlog = require('midlog');
var app = koa();
// 配置日志中间件
var firstValve = midlog({
env: 'online',
exportGlobalLogger: true,
appender: [{
type: 'INFO',
logdir: '/tmp/log/midlog',
pattern: '%d %r %x{name}:%z %p - %m%n',
rollingFile: false,
duation: 60000,
name: 'info.log',
nameformat: '[info.]HH-mm-ss[.log]',
tokens: {
name: 'helloworld'
},
cacheSize: 5 * 1024 * 1024,
flushTimeout: 15000
},{
type: 'ERROR',
logdir: '/tmp/log/midlog',
pattern: '%d %r %x{name}:%z %p - %m%n',
rollingFile: false,
duation: 60000,
name: 'error.log',
nameformat: '[info.]HH-mm-ss[.log]',
tokens: {
name: 'helloworld'
},
cacheSize: 10240,
flushTimeout: 10000
},{
type: 'TRACE',
logdir: '/tmp/log/midlog',
pattern: '%d %r %x{name}:%z %p - %m%n',
rollingFile: false,
duation: 60000,
name: 'trace.log',
nameformat: '[info.]HH-mm-ss[.log]',
tokens: {
name: 'helloworld'
},
cacheSize: 5 * 1024 * 1024,
flushTimeout: 10000
}]
});
// 使用全局的logger接口
logger.info('i am the global logger');
// 将midlog放在中间件的前列
app.use(firstValve);
// 业务中间件
app.use(function*(next){
this.logger.info(this.url+' this is the first valve!! ');
this.logger.error('midlog tracing' + this.url+' this is the first valve!! ');
this.logger.trace('midlog tracing' + this.url+' this is the first valve!! ');
yield next;
});
app.use(function*(){
this.logger.info(this.url+' this is the 2cd valve!! ');
this.logger.error('midlog tracing' + this.url+' this is the 2cd valve!! ');
this.logger.trace('midlog tracing' + this.url+' this is the 2cd valve!!');
this.body = '<h1>hello midlog</h1>';
});
app.listen(8888);
midlog提供了3种日志刷新级别:
TRACE、INFO、ERROR,
并且提供了两种写日志文件的方式:
midlog采用和log4js相同的layout格式和语法,生成可定制的日志输出格式。
最后,midlog采用多级缓冲的架构(针对单文件写模式采用双缓冲,文件分时写模式采用单缓冲),可以有效的控制Stream写的频率,而缓冲的大小和刷新频率可以由开发者根据实际需要自由设置。
如定义nameformat为 pattern: '%d %r %x{name}:%z %p - %m%n'
且tokens设置为 {name: 'helloworld'}
则输出日志格式为:
(%d) (%r) (%x{name}) (%z) (%p) (%m) (%n)
2017-01-16 10:59:55.611 10:59:55 helloworld:13736 INFO - / this is the first valve!!