-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbunext.js
More file actions
executable file
·191 lines (175 loc) · 4.8 KB
/
bunext.js
File metadata and controls
executable file
·191 lines (175 loc) · 4.8 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
var fs = require('fs')
, program = require('commander')
, chalk = require('chalk')
, moment = require('moment')
, liner = require('./liner');
var dateStart, dateEnd;
/**
* displayError
* Display error messages in red
* @param message
*/
function displayError(message) {
console.log(chalk.red.bold(message));
}
/**
* processJSON
* @param jsObj
*/
function processedJSON(jsObj) {
var text;
if (program.source) {
text = JSON.stringify(jsObj, undefined, 2);
} else {
text = JSON.stringify(jsObj, undefined);
}
return text;
}
/**
* processLine
* @param line
*/
function processLine(line) {
var jsObj
, text
, output
, startQuote = false;
try {
text = '';
jsObj = JSON.parse(line);
if (program.timestamp) {
if (program['array']) {
text += '"';
startQuote = true;
}
text += jsObj.time + ' | ';
} else if (program.prettydate) {
text = moment(jsObj.time).format("MMMM Do YYYY, hh:mm:ss a") + ' | ';
}
if (program.dates) {
var logEntryDate = moment(jsObj.time).unix();
if (logEntryDate < dateStart || logEntryDate > dateEnd) {
return null;
}
}
if (program.expression) {
try {
var result = eval('jsObj.' + program.expression);
if (result) {
var isBool = typeof result === 'boolean';
var isString = typeof result === 'string';
var isNumber = typeof result === 'number';
var isObject = typeof result === 'object' ;
if (isObject) {
text += processedJSON(jsObj);
} else if (isBool) {
var subExps = program.expression.split(' ');
var res = eval('jsObj.' + subExps[0]);
if (typeof res !== 'boolean') {
text += res;
}
text += processedJSON(jsObj);
} else if (isString || isNumber) {
if (isString) {
if (program['array'] && startQuote === false) {
text += '"';
}
if (program['compare']) {
if (result.indexOf(program['compare']) > -1) {
text += processedJSON(jsObj);
} else {
return '';
}
} else {
if (program['raw'] || program['source']) {
text += processedJSON(jsObj);
} else {
text += result;
}
}
if (program['array']) {
text += '"';
}
} else if (isNumber) {
text += result;
}
}
output = text;
}
} catch (e) {
output = '';
}
} else {
output = text + processedJSON(jsObj);
}
} catch (e) {
displayError(e);
process.exit(-1);
}
return output;
}
/**
* main
*/
function main() {
program
.version('1.0.3')
.usage('[options] bunyan.log')
.option('-a, --array', 'Output results in array format')
.option('-c, --compare [pattern]', 'Used with -e to filter by sub pattern')
.option('-d, --dates [start,end]', 'Specify a date range')
.option('-e, --expression [expression]', 'Filter using expression predicate')
.option('-r, --raw', 'Output raw bunyan data')
.option('-s, --source', 'Extract source (JSON) object')
.option('-t, --timestamp', 'Show timestamp in results')
.option('-p, --prettydate', 'Show pretty timestamp in results')
.parse(process.argv);
if (!program.args.length) {
program.help();
} else {
var arrayOutput = program['array']
, source = fs.createReadStream(program.args[0]);
if (arrayOutput) {
console.log('[');
}
if (program.dates) {
try {
var dates = program.dates.split(',');
if (dates.length !== 2) {
displayError('date range should consist of two commas separated dates');
process.exit(-1);
}
dateStart = moment(dates[0].trim()).unix();
dateEnd = (dates[1].trim() === 'now') ? moment().unix() : moment(dates[1].trim()).unix();
} catch (e) {
displayError('date range should consist of two commas separated dates');
process.exit(-1);
}
}
source.pipe(liner);
source.on('error', function(err) {
if (err.errno === 34) {
displayError('Can\'t open ' + err.path);
} else {
displayError(err);
}
process.exit(-1);
});
liner.on('readable', function() {
var lineIn, lineOut;
while (lineIn = liner.read()) {
lineOut = processLine(lineIn);
if (lineOut) {
console.log(lineOut + ((arrayOutput) ? ',' : ''));
}
}
});
process.on('exit', function() {
if (arrayOutput) {
console.log(']');
}
});
}
}
main();