s/service/name/ for Logger name field. "service" is unnecessarily tied to usage for a service.

No need to differ from log4j Logger "name".
This commit is contained in:
Trent Mick 2012-02-06 15:13:50 -08:00
parent 352c4177b7
commit 140c9fda91
12 changed files with 39 additions and 33 deletions

View file

@ -2,6 +2,8 @@
## bunyan 0.5.0 (not yet released) ## bunyan 0.5.0 (not yet released)
- s/service/name/ for Logger name field. "service" is unnecessarily tied
to usage for a service. No need to differ from log4j Logger "name".
- Add `log.level(...)` and `log.levels(...)` API for changing logger stream - Add `log.level(...)` and `log.levels(...)` API for changing logger stream
levels. levels.
- Add `TRACE|DEBUG|INFO|WARN|ERROR|FATAL` level constants to exports. - Add `TRACE|DEBUG|INFO|WARN|ERROR|FATAL` level constants to exports.

View file

@ -18,19 +18,19 @@ base soonish.
# Usage # Usage
**The usual.** All loggers must provide a "service" name. This is somewhat akin **The usual.** All loggers must provide a "name". This is somewhat akin
to log4j logger "name", but Bunyan doesn't do hierarchical logger names. to log4j logger "name", but Bunyan doesn't do hierarchical logger names.
$ cat hi.js $ cat hi.js
var Logger = require('bunyan'); var Logger = require('bunyan');
var log = new Logger({service: "myapp"}); var log = new Logger({name: "myapp"});
log.info("hi"); log.info("hi");
**Log records are JSON.** "hostname", "time" and "v" (the Bunyan log **Log records are JSON.** "hostname", "time" and "v" (the Bunyan log
format version) are added for you. format version) are added for you.
$ node hi.js $ node hi.js
{"service":"myapp","hostname":"banana.local","level":2,"msg":"hi","time":"2012-01-31T00:07:44.216Z","v":0} {"name":"myapp","hostname":"banana.local","level":2,"msg":"hi","time":"2012-01-31T00:07:44.216Z","v":0}
The full `log.{trace|debug|...|fatal}(...)` API is: The full `log.{trace|debug|...|fatal}(...)` API is:
@ -38,9 +38,9 @@ The full `log.{trace|debug|...|fatal}(...)` API is:
log.info(err); // Log an `Error` instance, adds "err" key with exception details log.info(err); // Log an `Error` instance, adds "err" key with exception details
// (including the stack) and sets "msg" to the exception message. // (including the stack) and sets "msg" to the exception message.
// A special case, b/c logging errors should be easy. // A special case, b/c logging errors should be easy.
log.info("hi"); // Log a simple string message. log.info('hi'); // Log a simple string message.
log.info("hi %s", bob, anotherVar); // Uses `util.format` for msg formatting. log.info('hi %s', bob, anotherVar); // Uses `util.format` for msg formatting.
log.info({foo: "bar"}, "hi"); // Adds "foo" field to log record. log.info({foo: 'bar'}, 'hi'); // Adds "foo" field to log record.
## bunyan tool ## bunyan tool
@ -55,7 +55,7 @@ be added, including support for custom formats.
$ node hi.js | ./bin/bunyan -o json $ node hi.js | ./bin/bunyan -o json
{ {
"service": "myapp", "name": "myapp",
"hostname": "banana.local", "hostname": "banana.local",
"level": 2, "level": 2,
"msg": "hi", "msg": "hi",
@ -69,14 +69,14 @@ 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({service: "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
streams at different levels**. streams at different levels**.
var log = new Logger({ var log = new Logger({
service: "amon", name: "amon",
streams: [ streams: [
{ {
level: "info", level: "info",
@ -183,7 +183,7 @@ be added to log records by using the `src: true` config option:
This adds the call source info with the 'src' field, like this: This adds the call source info with the 'src' field, like this:
{ {
"service": "src-example", "name": "src-example",
"hostname": "banana.local", "hostname": "banana.local",
"component": "wuzzle", "component": "wuzzle",
"level": 4, "level": 4,
@ -203,8 +203,8 @@ in production.**
# Levels # Levels
- "fatal" (6): the service is going to stop or become unusable now - "fatal" (6): the service/app is going to stop or become unusable now
- "error" (5): fatal for a particular request, but the service continues servicing other requests - "error" (5): fatal for a particular request, but the service/app continues servicing other requests
- "warn" (4): a note on something that should probably be looked at by an operator - "warn" (4): a note on something that should probably be looked at by an operator
- "info" (3): detail on regular operation - "info" (3): detail on regular operation
- "debug" (2): anything else, i.e. too verbose to be included in "info" level. - "debug" (2): anything else, i.e. too verbose to be included in "info" level.
@ -254,12 +254,12 @@ incorrect signature) is always a bug in Bunyan.**
A typical Bunyan log record looks like this: A typical Bunyan log record looks like this:
{"service":"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","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:
{ {
"service": "myserver", "name": "myserver",
"hostname": "banana.local", "hostname": "banana.local",
"req": { "req": {
"method": "GET", "method": "GET",
@ -288,8 +288,9 @@ Core fields:
format. Details will be in "CHANGES.md" (the change log). format. Details will be in "CHANGES.md" (the change log).
- `level`: Required. Integer. Added by Bunion. Cannot be overriden. - `level`: Required. Integer. Added by Bunion. Cannot be overriden.
See the "Levels" section. See the "Levels" section.
- `service`: Required. String. Provided at Logger creation. - `name`: Required. String. Provided at Logger creation.
You must specify a service name for your logger when creating it. You must specify a name for your logger when creating it. Typically this
is the name of the service/app using Bunyan for logging.
- `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()`.

View file

@ -253,16 +253,16 @@ function handleLogLine(line, opts) {
switch (opts.outputMode) { switch (opts.outputMode) {
case OM_PAUL: case OM_PAUL:
// Single-line msg: // Single-line msg:
// [time] LEVEL: service[/component] on hostname (src): msg (extras...) // [time] LEVEL: name[/component] on hostname (src): msg (extras...)
// Multi-line msg: // Multi-line msg:
// [time] LEVEL: service[/component] on hostname (src): (extras...) // [time] LEVEL: name[/component] on hostname (src): (extras...)
// msg // msg
// If 'req', show the request. // If 'req', show the request.
// If 'res', show the response. // If 'res', show the response.
// If 'err' and 'err.stack' then show that. // If 'err' and 'err.stack' then show that.
var serviceStr = rec.service; var nameStr = rec.name;
if (rec.component) { if (rec.component) {
serviceStr += '/' + rec.component; nameStr += '/' + rec.component;
} }
var levelStr = (upperNameFromLevel[rec.level] var levelStr = (upperNameFromLevel[rec.level]
|| "<unknown-level " + rec.level + ">"); || "<unknown-level " + rec.level + ">");
@ -316,7 +316,7 @@ function handleLogLine(line, opts) {
emit(format("[%s] %s: %s on %s%s:%s%s\n%s", emit(format("[%s] %s: %s on %s%s:%s%s\n%s",
rec.time, rec.time,
upperNameFromLevel[rec.level] || "<unknown-level " + rec.level + ">", upperNameFromLevel[rec.level] || "<unknown-level " + rec.level + ">",
serviceStr, nameStr,
rec.hostname || "<no-hostname>", rec.hostname || "<no-hostname>",
src, src,
onelineMsg, onelineMsg,

View file

@ -5,7 +5,7 @@ var Logger = require('../lib/bunyan');
var util = require('util'); var util = require('util');
var log = new Logger({ var log = new Logger({
service: "myserver", name: "myserver",
serializers: { serializers: {
err: Logger.stdSerializers.err, // <--- use this err: Logger.stdSerializers.err, // <--- use this
} }
@ -30,7 +30,7 @@ try {
$ node err.js | ../bin/bunyan -j $ node err.js | ../bin/bunyan -j
{ {
"service": "myserver", "name": "myserver",
"hostname": "banana.local", "hostname": "banana.local",
"err": { "err": {
"stack": "TypeError: boom\n at Object.<anonymous> (/Users/trentm/tm/node-bunyan/examples/err.js:15:9)\n at Module._compile (module.js:411:26)\n at Object..js (module.js:417:10)\n at Module.load (module.js:343:31)\n at Function._load (module.js:302:12)\n at Array.0 (module.js:430:10)\n at EventEmitter._tickCallback (node.js:126:26)", "stack": "TypeError: boom\n at Object.<anonymous> (/Users/trentm/tm/node-bunyan/examples/err.js:15:9)\n at Module._compile (module.js:411:26)\n at Object..js (module.js:417:10)\n at Module.load (module.js:343:31)\n at Function._load (module.js:302:12)\n at Array.0 (module.js:430:10)\n at EventEmitter._tickCallback (node.js:126:26)",

View file

@ -1,7 +1,7 @@
var Logger = require('../lib/bunyan'); var Logger = require('../lib/bunyan');
// Basic usage. // Basic usage.
var log = new Logger({service: "myapp", level: "info"}); var log = new Logger({name: "myapp", level: "info"});
// isInfoEnabled replacement // isInfoEnabled replacement
console.log("log.info() is:", log.info()) console.log("log.info() is:", log.info())

View file

@ -10,7 +10,7 @@ var assert = require('assert');
// Basic usage. // Basic usage.
var log = new Logger({ var log = new Logger({
service: 'example-level', name: 'example-level',
streams: [ streams: [
{ {
name: 'stdout', name: 'stdout',

View file

@ -1,6 +1,6 @@
var Logger = require('../lib/bunyan'); var Logger = require('../lib/bunyan');
log = new Logger({ log = new Logger({
service: "amon", name: "amon",
streams: [ streams: [
{ {
level: "info", level: "info",
@ -8,7 +8,7 @@ log = new Logger({
}, },
{ {
level: "error", level: "error",
path: "tmp/error.log" path: "multi.log"
} }
] ]
}); });

View file

@ -4,7 +4,7 @@ var http = require('http');
var Logger = require('../lib/bunyan'); var Logger = require('../lib/bunyan');
var log = new Logger({ var log = new Logger({
service: "myserver", name: "myserver",
serializers: { serializers: {
req: Logger.stdSerializers.req, req: Logger.stdSerializers.req,
res: Logger.stdSerializers.res res: Logger.stdSerializers.res

View file

@ -3,7 +3,7 @@
var Logger = require('../lib/bunyan'); var Logger = require('../lib/bunyan');
var log = new Logger({service: "src-example", src: true}); var log = new Logger({name: "src-example", src: true});
log.info("one"); log.info("one");
log.info("two"); log.info("two");

View file

@ -147,7 +147,7 @@ function resolveLevel(nameOrNum) {
* Create a Logger instance. * Create a Logger instance.
* *
* @param options {Object} See documentation for full details. At minimum * @param options {Object} See documentation for full details. At minimum
* this must include a "service" string key. Configuration keys: * this must include a "name" string key. Configuration keys:
* - streams: specify the logger output streams. This is an array of * - streams: specify the logger output streams. This is an array of
* objects of the form: * objects of the form:
* { * {
@ -192,6 +192,9 @@ function Logger(options, _childOptions, _childSimple) {
if (!options) { if (!options) {
throw new TypeError('options (object) is required'); throw new TypeError('options (object) is required');
} }
if (!parent && !options.name) {
throw new TypeError('options.name (string) is required');
}
if ((options.stream || options.level) && options.streams) { if ((options.stream || options.level) && options.streams) {
throw new TypeError('cannot mix "streams" with "stream" or "level" options'); throw new TypeError('cannot mix "streams" with "stream" or "level" options');
} }

View file

@ -17,7 +17,7 @@ var ben = require('ben'); // npm install ben
var Logger = require('../lib/bunyan'); var Logger = require('../lib/bunyan');
var log = new Logger({ var log = new Logger({
service: "svc", name: "svc",
streams: [ streams: [
{ {
path: __dirname + "/timechild.log" path: __dirname + "/timechild.log"

View file

@ -17,7 +17,7 @@ Collector.prototype.write = function(s) {
var collector = new Collector(); var collector = new Collector();
var logwith = new Logger({ var logwith = new Logger({
service: "with-src", name: "with-src",
src: true, src: true,
stream: collector stream: collector
}); });
@ -28,7 +28,7 @@ var ms = ben(1e5, function () {
console.log(' - log.info with src: %dms per iteration', ms); console.log(' - log.info with src: %dms per iteration', ms);
var logwithout = new Logger({ var logwithout = new Logger({
service: "without-src", name: "without-src",
stream: collector stream: collector
}); });
var ms = ben(1e5, function () { var ms = ben(1e5, function () {