issue #4: add 'pid' automatic log record field
This commit is contained in:
parent
e8581c52c7
commit
c12a90a511
5 changed files with 35 additions and 28 deletions
|
@ -1,8 +1,9 @@
|
||||||
# bunyan Changelog
|
# bunyan Changelog
|
||||||
|
|
||||||
## bunyan 0.5.4 (not yet released)
|
## bunyan 0.6.0 (not yet released)
|
||||||
|
|
||||||
|
- Add 'pid' automatic log record field.
|
||||||
|
|
||||||
(nothing yet)
|
|
||||||
|
|
||||||
## bunyan 0.5.3
|
## bunyan 0.5.3
|
||||||
|
|
||||||
|
|
24
README.md
24
README.md
|
@ -52,7 +52,7 @@ be added, including support for custom formats.
|
||||||
|
|
||||||
$ node hi.js | ./bin/bunyan # CLI tool to filter/pretty-print JSON logs.
|
$ node hi.js | ./bin/bunyan # CLI tool to filter/pretty-print JSON logs.
|
||||||
[2012-01-31T00:08:11.387Z] INFO: myapp on banana.local: hi
|
[2012-01-31T00:08:11.387Z] INFO: myapp on banana.local: hi
|
||||||
|
|
||||||
$ node hi.js | ./bin/bunyan -o json
|
$ node hi.js | ./bin/bunyan -o json
|
||||||
{
|
{
|
||||||
"name": "myapp",
|
"name": "myapp",
|
||||||
|
@ -69,7 +69,7 @@ be added, including support for custom formats.
|
||||||
By default, log output is to stdout (**stream**) and at the "info" level.
|
By default, log output is to stdout (**stream**) and at the "info" level.
|
||||||
Explicitly that looks like:
|
Explicitly that looks like:
|
||||||
|
|
||||||
var log = new Logger({name: "myapp", stream: process.stdout,
|
var log = new Logger({name: "myapp", stream: process.stdout,
|
||||||
level: "info"});
|
level: "info"});
|
||||||
|
|
||||||
That is an abbreviated form for a single stream. **You can defined multiple
|
That is an abbreviated form for a single stream. **You can defined multiple
|
||||||
|
@ -101,7 +101,7 @@ same config as its parent, plus include the "component" field.
|
||||||
var log = new Logger(...);
|
var log = new Logger(...);
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
function Wuzzle(options) {
|
function Wuzzle(options) {
|
||||||
this.log = options.log;
|
this.log = options.log;
|
||||||
this.log.info("creating a wuzzle")
|
this.log.info("creating a wuzzle")
|
||||||
|
@ -109,7 +109,7 @@ same config as its parent, plus include the "component" field.
|
||||||
Wuzzle.prototype.woos = function () {
|
Wuzzle.prototype.woos = function () {
|
||||||
this.log.warn("This wuzzle is woosey.")
|
this.log.warn("This wuzzle is woosey.")
|
||||||
}
|
}
|
||||||
|
|
||||||
var wuzzle = new Wuzzle({log: log.child({component: "wuzzle"})});
|
var wuzzle = new Wuzzle({log: log.child({component: "wuzzle"})});
|
||||||
wuzzle.woos();
|
wuzzle.woos();
|
||||||
log.info("done with the wuzzle")
|
log.info("done with the wuzzle")
|
||||||
|
@ -225,14 +225,14 @@ The lowercase level names are aliases supported in the API.
|
||||||
Here is the API for changing levels in an existing logger:
|
Here is the API for changing levels in an existing logger:
|
||||||
|
|
||||||
log.level() -> INFO // gets current level (lowest level of all streams)
|
log.level() -> INFO // gets current level (lowest level of all streams)
|
||||||
|
|
||||||
log.level(INFO) // set all streams to level INFO
|
log.level(INFO) // set all streams to level INFO
|
||||||
log.level("info") // set all streams to level INFO
|
log.level("info") // set all streams to level INFO
|
||||||
|
|
||||||
log.levels() -> [DEBUG, INFO] // get array of levels of all streams
|
log.levels() -> [DEBUG, INFO] // get array of levels of all streams
|
||||||
log.levels(0) -> DEBUG // get level of stream at index 0
|
log.levels(0) -> DEBUG // get level of stream at index 0
|
||||||
log.levels("foo") // get level of stream with name "foo"
|
log.levels("foo") // get level of stream with name "foo"
|
||||||
|
|
||||||
log.levels(0, INFO) // set level of stream 0 to INFO
|
log.levels(0, INFO) // set level of stream 0 to INFO
|
||||||
log.levels(0, "info") // can use "info" et al aliases
|
log.levels(0, "info") // can use "info" et al aliases
|
||||||
log.levels("foo", WARN) // set stream named "foo" to WARN
|
log.levels("foo", WARN) // set stream named "foo" to WARN
|
||||||
|
@ -256,13 +256,14 @@ incorrect signature) is always a bug in Bunyan.**
|
||||||
|
|
||||||
A typical Bunyan log record looks like this:
|
A typical Bunyan log record looks like this:
|
||||||
|
|
||||||
{"name":"myserver","hostname":"banana.local","req":{"method":"GET","url":"/path?q=1#anchor","headers":{"x-hi":"Mom","connection":"close"}},"level":3,"msg":"start request","time":"2012-02-03T19:02:46.178Z","v":0}
|
{"name":"myserver","hostname":"banana.local","pid":123,"req":{"method":"GET","url":"/path?q=1#anchor","headers":{"x-hi":"Mom","connection":"close"}},"level":3,"msg":"start request","time":"2012-02-03T19:02:46.178Z","v":0}
|
||||||
|
|
||||||
Pretty-printed:
|
Pretty-printed:
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "myserver",
|
"name": "myserver",
|
||||||
"hostname": "banana.local",
|
"hostname": "banana.local",
|
||||||
|
"pid": 123,
|
||||||
"req": {
|
"req": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"url": "/path?q=1#anchor",
|
"url": "/path?q=1#anchor",
|
||||||
|
@ -296,6 +297,7 @@ Core fields:
|
||||||
- `hostname`: Required. String. Provided or determined at Logger creation.
|
- `hostname`: Required. String. Provided or determined at Logger creation.
|
||||||
You can specify your hostname at Logger creation or it will be retrieved
|
You can specify your hostname at Logger creation or it will be retrieved
|
||||||
vi `os.hostname()`.
|
vi `os.hostname()`.
|
||||||
|
- `pid`: Required. Integer. Filled in automatically at Logger creation.
|
||||||
- `time`: Required. String. Added by Bunion. Can be overriden.
|
- `time`: Required. String. Added by Bunion. Can be overriden.
|
||||||
The date and time of the event in [ISO 8601
|
The date and time of the event in [ISO 8601
|
||||||
Extended Format](http://en.wikipedia.org/wiki/ISO_8601) format and in UTC,
|
Extended Format](http://en.wikipedia.org/wiki/ISO_8601) format and in UTC,
|
||||||
|
@ -317,7 +319,7 @@ Recommended/Best Practice Fields:
|
||||||
|
|
||||||
- `err`: Object. A caught JS exception. Log that thing with `log.info(err)`
|
- `err`: Object. A caught JS exception. Log that thing with `log.info(err)`
|
||||||
to get:
|
to get:
|
||||||
|
|
||||||
...
|
...
|
||||||
"err": {
|
"err": {
|
||||||
"message": "boom",
|
"message": "boom",
|
||||||
|
@ -340,7 +342,7 @@ Recommended/Best Practice Fields:
|
||||||
|
|
||||||
- `req`: An HTTP server request. Bunyan provides `Logger.stdSerializers.req`
|
- `req`: An HTTP server request. Bunyan provides `Logger.stdSerializers.req`
|
||||||
to serialize a request with a suggested set of keys. Example:
|
to serialize a request with a suggested set of keys. Example:
|
||||||
|
|
||||||
{
|
{
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"url": "/path?q=1#anchor",
|
"url": "/path?q=1#anchor",
|
||||||
|
@ -353,7 +355,7 @@ Recommended/Best Practice Fields:
|
||||||
}
|
}
|
||||||
|
|
||||||
- `res`: An HTTP server response. Bunyan provides `Logger.stdSerializers.res`
|
- `res`: An HTTP server response. Bunyan provides `Logger.stdSerializers.res`
|
||||||
to serialize a response with a suggested set of keys. Example:
|
to serialize a response with a suggested set of keys. Example:
|
||||||
|
|
||||||
{
|
{
|
||||||
"statusCode": 200,
|
"statusCode": 200,
|
||||||
|
@ -446,5 +448,3 @@ See "TODO.md", but basically:
|
||||||
- Syslog support.
|
- Syslog support.
|
||||||
|
|
||||||
- Some speed comparisons with others to get a feel for Bunyan's speed.
|
- Some speed comparisons with others to get a feel for Bunyan's speed.
|
||||||
|
|
||||||
|
|
||||||
|
|
27
bin/bunyan
27
bin/bunyan
|
@ -5,7 +5,7 @@
|
||||||
// See <https://github.com/trentm/node-bunyan>.
|
// See <https://github.com/trentm/node-bunyan>.
|
||||||
//
|
//
|
||||||
|
|
||||||
var VERSION = "0.5.4";
|
var VERSION = "0.6.0";
|
||||||
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var pathlib = require('path');
|
var pathlib = require('path');
|
||||||
|
@ -77,7 +77,7 @@ if (!format) {
|
||||||
}
|
}
|
||||||
return objects.join(' ');
|
return objects.join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
var i = 1;
|
var i = 1;
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
var len = args.length;
|
var len = args.length;
|
||||||
|
@ -164,7 +164,7 @@ function parseArgv(argv) {
|
||||||
outputMode: OM_PAUL,
|
outputMode: OM_PAUL,
|
||||||
jsonIndent: 2
|
jsonIndent: 2
|
||||||
};
|
};
|
||||||
|
|
||||||
// Turn '-iH' into '-i -H', except for argument-accepting options.
|
// Turn '-iH' into '-i -H', except for argument-accepting options.
|
||||||
var args = argv.slice(2); // drop ['node', 'scriptname']
|
var args = argv.slice(2); // drop ['node', 'scriptname']
|
||||||
var newArgs = [];
|
var newArgs = [];
|
||||||
|
@ -231,7 +231,7 @@ function parseArgv(argv) {
|
||||||
}
|
}
|
||||||
parsed.args.push(arg);
|
parsed.args.push(arg);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//TODO: '--' handling and error on a first arg that looks like an option.
|
//TODO: '--' handling and error on a first arg that looks like an option.
|
||||||
|
|
||||||
|
@ -264,7 +264,7 @@ function handleLogLine(line, opts) {
|
||||||
|
|
||||||
switch (opts.outputMode) {
|
switch (opts.outputMode) {
|
||||||
case OM_PAUL:
|
case OM_PAUL:
|
||||||
// [time] LEVEL: name[/component] on hostname (src): msg* (extras...)
|
// [time] LEVEL: name[/component]/pid on hostname (src): msg* (extras...)
|
||||||
// msg*
|
// msg*
|
||||||
// --
|
// --
|
||||||
// long and multi-line extras
|
// long and multi-line extras
|
||||||
|
@ -286,6 +286,9 @@ function handleLogLine(line, opts) {
|
||||||
}
|
}
|
||||||
delete rec.component;
|
delete rec.component;
|
||||||
|
|
||||||
|
nameStr += '/' + rec.pid;
|
||||||
|
delete rec.pid;
|
||||||
|
|
||||||
var level = (upperNameFromLevel[rec.level] || "<level " + rec.level + ">");
|
var level = (upperNameFromLevel[rec.level] || "<level " + rec.level + ">");
|
||||||
delete rec.level;
|
delete rec.level;
|
||||||
|
|
||||||
|
@ -299,7 +302,7 @@ function handleLogLine(line, opts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete rec.src;
|
delete rec.src;
|
||||||
|
|
||||||
var hostname = rec.hostname;
|
var hostname = rec.hostname;
|
||||||
delete rec.hostname;
|
delete rec.hostname;
|
||||||
|
|
||||||
|
@ -310,7 +313,7 @@ function handleLogLine(line, opts) {
|
||||||
extras.push("req_id=" + rec.req_id);
|
extras.push("req_id=" + rec.req_id);
|
||||||
}
|
}
|
||||||
delete rec.req_id;
|
delete rec.req_id;
|
||||||
|
|
||||||
if (rec.latency) {
|
if (rec.latency) {
|
||||||
extras.push(rec.latency + "ms");
|
extras.push(rec.latency + "ms");
|
||||||
}
|
}
|
||||||
|
@ -367,7 +370,7 @@ function handleLogLine(line, opts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete rec.res;
|
delete rec.res;
|
||||||
|
|
||||||
if (rec.err && rec.err.stack) {
|
if (rec.err && rec.err.stack) {
|
||||||
details.push(indent(rec.err.stack))
|
details.push(indent(rec.err.stack))
|
||||||
}
|
}
|
||||||
|
@ -482,7 +485,7 @@ function main(argv) {
|
||||||
util.puts("bunyan " + getVersion());
|
util.puts("bunyan " + getVersion());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var leftover = ""; // Left-over partial line from last chunk.
|
var leftover = ""; // Left-over partial line from last chunk.
|
||||||
var stdin = process.openStdin();
|
var stdin = process.openStdin();
|
||||||
stdin.setEncoding('utf8');
|
stdin.setEncoding('utf8');
|
||||||
|
@ -493,7 +496,7 @@ function main(argv) {
|
||||||
leftover += lines[0];
|
leftover += lines[0];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length > 1) {
|
if (length > 1) {
|
||||||
handleLogLine(leftover + lines[0], opts);
|
handleLogLine(leftover + lines[0], opts);
|
||||||
}
|
}
|
||||||
|
@ -503,7 +506,7 @@ function main(argv) {
|
||||||
handleLogLine(lines[i], opts);
|
handleLogLine(lines[i], opts);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
stdin.on('end', function () {
|
stdin.on('end', function () {
|
||||||
if (leftover) {
|
if (leftover) {
|
||||||
handleLogLine(leftover, opts);
|
handleLogLine(leftover, opts);
|
||||||
|
@ -525,6 +528,6 @@ if (require.main === module) {
|
||||||
/* pass */
|
/* pass */
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
main(process.argv);
|
main(process.argv);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* The bunyan logging library for node.js.
|
* The bunyan logging library for node.js.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var VERSION = "0.5.4";
|
var VERSION = "0.6.0";
|
||||||
|
|
||||||
// Bunyan log format version. This becomes the 'v' field on all log records.
|
// Bunyan log format version. This becomes the 'v' field on all log records.
|
||||||
// `0` is until I release a version "1.0.0" of node-bunyan. Thereafter,
|
// `0` is until I release a version "1.0.0" of node-bunyan. Thereafter,
|
||||||
|
@ -384,6 +384,9 @@ function Logger(options, _childOptions, _childSimple) {
|
||||||
if (!fields.hostname) {
|
if (!fields.hostname) {
|
||||||
fields.hostname = os.hostname();
|
fields.hostname = os.hostname();
|
||||||
}
|
}
|
||||||
|
if (!fields.pid) {
|
||||||
|
fields.pid = process.pid;
|
||||||
|
}
|
||||||
Object.keys(fields).forEach(function (k) {
|
Object.keys(fields).forEach(function (k) {
|
||||||
self.fields[k] = fields[k];
|
self.fields[k] = fields[k];
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "bunyan",
|
"name": "bunyan",
|
||||||
"version": "0.5.4",
|
"version": "0.6.0",
|
||||||
"description": "a JSON Logger library for node.js servers",
|
"description": "a JSON Logger library for node.js servers",
|
||||||
"main": "./lib/bunyan.js",
|
"main": "./lib/bunyan.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|
Loading…
Reference in a new issue