`bunyan -L` (or `bunyan --time local`) to show local time.

Fixes #103
master
Trent Mick 2015-01-16 22:21:23 -08:00
parent d412ecef12
commit 0d040acb3a
2 changed files with 52 additions and 13 deletions

View File

@ -8,6 +8,10 @@ Known issues:
## bunyan 1.2.5 (not yet released) ## bunyan 1.2.5 (not yet released)
- [issue #103] `bunyan -L` (or `bunyan --time local`) to show local time.
Bunyan log records store `time` in UTC time. Sometimes it is convenient
to display in local time.
- [issue #205] Fix the "The Bunyan CLI crashed!" checking to properly warn of - [issue #205] Fix the "The Bunyan CLI crashed!" checking to properly warn of
the common failure case when `-c CONDITION` is being used. the common failure case when `-c CONDITION` is being used.

View File

@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
/** /**
* Copyright (c) 2014 Trent Mick. All rights reserved. * Copyright (c) 2015 Trent Mick. All rights reserved.
* Copyright (c) 2014 Joyent Inc. All rights reserved. * Copyright (c) 2015 Joyent Inc. All rights reserved.
* *
* bunyan -- filter and pretty-print Bunyan log files (line-delimited JSON) * bunyan -- filter and pretty-print Bunyan log files (line-delimited JSON)
* *
@ -83,6 +83,13 @@ Object.keys(levelFromName).forEach(function (name) {
}); });
// Display time formats.
TIME_UTC = 1; // the default, bunyan's native format
TIME_LOCAL = 2;
var timezoneOffsetMs; // used for TIME_LOCAL display
// The current raw input line being processed. Used for `uncaughtException`. // The current raw input line being processed. Used for `uncaughtException`.
var currLine = null; var currLine = null;
@ -402,7 +409,8 @@ function parseArgv(argv) {
level: null, level: null,
strict: false, strict: false,
pids: null, pids: null,
pidsType: null pidsType: null,
timeFormat: null // one of the TIME_ constants
}; };
// Turn '-iH' into '-i -H', except for argument-accepting options. // Turn '-iH' into '-i -H', except for argument-accepting options.
@ -489,6 +497,25 @@ function parseArgv(argv) {
case '-0': case '-0':
parsed.outputMode = OM_BUNYAN; parsed.outputMode = OM_BUNYAN;
break; break;
case '-L':
parsed.timeFormat = TIME_LOCAL;
break;
case '--time':
var timeArg = args.shift();
switch (timeArg) {
case 'utc':
parsed.timeFormat = TIME_UTC;
break
case 'local':
parsed.timeFormat = TIME_LOCAL;
break
case undefined:
throw new Error('missing argument to "--time"');
default:
throw new Error(format('invalid time format: "%s"',
timeArg));
}
break;
case '-p': case '-p':
if (!parsed.pids) { if (!parsed.pids) {
parsed.pids = []; parsed.pids = [];
@ -723,17 +750,25 @@ function emitRecord(rec, line, opts, stylize) {
delete rec.v; delete rec.v;
/* var time = rec.time;
* We assume the Date is formatted according to ISO8601, in which switch (opts.timeFormat) {
* case we can safely chop off the date information. case TIME_UTC:
*/ break;
if (short && rec.time[10] == 'T') { case TIME_LOCAL:
var time = rec.time.substr(11); if (!timezoneOffsetMs) {
time = stylize(time, 'XXX'); timezoneOffsetMs
} else { = (new Date(time)).getTimezoneOffset() * 60 * 1000;
var time = stylize('[' + rec.time + ']', 'XXX'); }
time = new Date(
(new Date(time)).getTime() - timezoneOffsetMs).toISOString()
break;
}
if (short && rec.time[10] === 'T') {
// Presuming `time` is ISO8601 formatted, i.e. safe to drop date.
time = stylize(time.substr(11), 'XXX');
} else {
time = stylize('[' + time + ']', 'XXX');
} }
delete rec.time; delete rec.time;
var nameStr = rec.name; var nameStr = rec.name;