-
Notifications
You must be signed in to change notification settings - Fork 1
/
bunyan.js
140 lines (131 loc) · 5.73 KB
/
bunyan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var config;
var logstashConfig;
var bunyan = require('bunyan')
// var bformat = require('bunyan-format')
// var prettyformatOut = bformat({ outputMode: 'short' });
var PrettyStream = require('bunyan-prettystream');
var prettyStdOut = new PrettyStream();
prettyStdOut.pipe(process.stdout);
var bunyan4udp = require('./logstash/udp-bunyan');
var bunyan4tcp = require('./logstash/tcp-bunyan');
var RotatingFileStream = require('bunyan-rotating-file-stream');
var getLogger4logstashUDP = function (category, options) {
options = options || {};
return bunyan.createLogger({
name: category || 'logstash_udp',
streams: [{
level: options.logLevel4console || config.logLevel4console || 'info',
// stream: options.pretty ? prettyformatOut : process.stdout
stream: options.pretty ? prettyStdOut : process.stdout
}, {
level: options.logLevel || logstashConfig.logLevel || config.logLevel || 'info',
type: "raw",
stream: bunyan4udp.createStream(logstashConfig.udp)
}],
level: options.logLevel || config.logLevel || 'info',
src: options.src
});
}
var getLogger4logstashTCP = function (category, options) {
options = options || {};
return bunyan.createLogger({
name: category || 'logstash_tcp',
streams: [{
level: options.logLevel4console || config.logLevel4console || 'info',
// stream: options.pretty ? prettyformatOut : process.stdout
stream: options.pretty ? prettyStdOut : process.stdout
}, {
level: options.logLevel || logstashConfig.logLevel || config.logLevel || 'info',
type: "raw",
stream: bunyan4tcp.createStream(logstashConfig.tcp)
}],
level: options.logLevel || config.logLevel || 'info',
src: options.src
});
}
var getLogger4Rotating = function (category, options, logstashOpts) {
var workerId4prefix = config.workerId4prefix || ( process.env.pm_id === undefined ?
"" : process.env.pm_id + "-" ); // sample: workerId4prefix = '1-' or ''
options = options || {};
category = category || 'rotation';
logstashOpts = logstashOpts || {};
var rotateConfig = options.rotateConfig || {};
if (logstashOpts.enableLogstash4console) {
var bunyan4logstash = getLogger4logstashUDP(category, options);;
switch (logstashOpts.currentLogstashInput) {
case 'tcp':
bunyan4logstash = getLogger4logstashTCP(category, options);
break;
default:
bunyan4logstash = getLogger4logstashUDP(category, options);
break;
}
return bunyan4logstash;
}
if (category === 'console') {
return bunyan.createLogger({
name: category,
streams: [{
level: options.logLevel4console || config.logLevel4console || 'info',
// stream: options.pretty ? prettyformatOut : process.stdout
stream: options.pretty ? prettyStdOut : process.stdout
}],
level: options.logLevel || config.logLevel || 'info',
src: options.src
});
}
// bunyan的bug, 拼接的路径存在 %Y-%m-%d, 则动态变量必须放在固定字符串的前面或后面
// ${workerId4prefix}%Y-%m-%d.info.log 有效
// %Y-%m-%d.info.log${workerId4prefix} 有效
// info${workerId4prefix}.log 有效
// %Y-%m-%d.${workerId4prefix}info.log 无效
return bunyan.createLogger({
name: category || 'rotating',
streams: [{
level: options.logLevel4console || config.logLevel4console || 'info',
// stream: options.pretty ? prettyformatOut : process.stdout
stream: options.pretty ? prettyStdOut : process.stdout
},
{
type: 'raw',
level: options.logLevel || config.logLevel || 'info',
stream: new RotatingFileStream({
path: rotateConfig.path ? `${rotateConfig.path}/info/${workerId4prefix}%Y-%m-%d.log` : `logs/${workerId4prefix}${category}-%Y-%m-%d.log`,
// path: 'logs/ex.%Y-%m-%d %H:%M:%S.log',
period: rotateConfig.period || '1d', // rotation
totalFiles: rotateConfig.totalFiles === undefined ? 10 : rotateConfig.totalFiles, // keep 10 back copies
rotateExisting: true, // Give ourselves a clean file when we start up, based on period
threshold: rotateConfig.threshold || '10m', // Rotate log files larger than 10 megabytes
// totalSize: '20m', // Don't keep more than 20mb of archived log files
gzip: false // Compress the archive log files to save space
})
},
// {
// type: 'raw',
// level: 'error',
// stream: new RotatingFileStream({
// path: rotateConfig.path ? `${rotateConfig.path}/error/${workerId4prefix}%Y-%m-%d.log` : `logs/${category}/error/${workerId4prefix}%Y-%m-%d.log`,
// // path: 'logs/ex.%Y-%m-%d %H:%M:%S.log',
// period: rotateConfig.period || '1d', // rotation
// totalFiles: rotateConfig.totalFiles || 15, // keep 10 back copies
// rotateExisting: true, // Give ourselves a clean file when we start up, based on period
// threshold: rotateConfig.threshold || '100m', // Rotate log files larger than 10 megabytes
// // totalSize: '20m', // Don't keep more than 20mb of archived log files
// gzip: false // Compress the archive log files to save space
// })
// }
],
level: options.logLevel || config.logLevel || 'info',
src: options.src
});
}
module.exports = function cwlog(options) {
if (!options) throw new Error('请提供cw_log_config配置信息');
config = config || options || {};
logstashConfig = logstashConfig || config.logstash || options.logstash || {};
return {
logstashUDP: getLogger4logstashUDP,
logstashTCP: getLogger4logstashTCP,
rotating: getLogger4Rotating
};
}