Comparing version 1.4.1 to 1.5.0
# bedrock ChangeLog | ||
## 1.5.0 - 2017-07-24 | ||
### Added | ||
- Add `child(meta)` method to create a child logger with common metadata for | ||
each logging call. The special `module` meta key can be used to prefix | ||
messages with `[module] ` and is removed from the message details. | ||
`child(name)` is a shortcut for `child({module: name})`. | ||
## 1.4.1 - 2017-02-02 | ||
@@ -4,0 +12,0 @@ |
@@ -71,2 +71,9 @@ # Contributing to Bedrock | ||
periods for chaining function calls. | ||
* When writing backend (node.js) code, use ES2016+, when writing frontend | ||
code (browser) use ES5. | ||
* When writing arrow functions (`=>`), if they can be kept on a single | ||
line, do so. Functions are just mappings from x to y (or `x => y`); it | ||
is easier to read when they are short and on a single line. If the | ||
function is longer (or requires curly braces `{`), break after the | ||
arrow. See examples. | ||
* Avoid getters and setters. | ||
@@ -84,2 +91,24 @@ * Do not override built-in prototypes unless you're fixing IE. | ||
#### Examples | ||
##### Arrow functions: | ||
If an arrow function will all fit on one line, do it: | ||
```js | ||
someLongFunctionOrExpression( | ||
a, b, | ||
(some, long, params) => someExpression()); | ||
``` | ||
If it won't fit on one line, then break after the arrow: | ||
```js | ||
someLongFunctionOrExpression( | ||
a, b, (some, long, params, tooManyParams) => | ||
someExpressionThatIsJustTooLong()); | ||
``` | ||
#### Linter | ||
A number of style and code rules can be checked with | ||
@@ -86,0 +115,0 @@ [jshint](http://jshint.com/) and [jscs](https://github.com/jscs-dev/node-jscs): |
@@ -80,2 +80,6 @@ /* | ||
config.loggers.console.colorize = true; | ||
// bedrock options | ||
config.loggers.console.bedrock = {}; | ||
// move 'module' meta to a pretty message prefix if available | ||
config.loggers.console.bedrock.modulePrefix = true; | ||
@@ -97,2 +101,4 @@ // file transport for app logging | ||
config.loggers.app.bedrock.enableChownDir = false; | ||
// move 'module' meta to a pretty message prefix if available | ||
config.loggers.app.bedrock.modulePrefix = false; | ||
@@ -114,2 +120,4 @@ // file transport for access logging | ||
config.loggers.access.bedrock.enableChownDir = false; | ||
// move 'module' meta to a pretty message prefix if available | ||
config.loggers.access.bedrock.modulePrefix = false; | ||
@@ -131,2 +139,4 @@ // file transport for error logging | ||
config.loggers.error.bedrock.enableChownDir = false; | ||
// move 'module' meta to a pretty message prefix if available | ||
config.loggers.error.bedrock.modulePrefix = false; | ||
@@ -133,0 +143,0 @@ // transport for email logging |
@@ -60,7 +60,23 @@ /* | ||
container.get = container.add = function(id) { | ||
var existing = container.loggers[id]; | ||
var logger = container._get.apply(container, arguments); | ||
const existing = container.loggers[id]; | ||
let logger = container._get.apply(container, arguments); | ||
if(!existing) { | ||
var wrapper = {}; | ||
wrapper.__proto__ = logger; | ||
const wrapper = Object.create(logger); | ||
wrapper.log = function(level, msg, meta) { | ||
// merge per-logger and per-log meta and call parent logger | ||
meta = Object.assign({}, this.meta, meta); | ||
return Object.getPrototypeOf(wrapper).log.apply( | ||
wrapper, [level, msg, meta]); | ||
}; | ||
wrapper.child = function(childMeta) { | ||
// simple string name is shortcut for {module: name} | ||
if(typeof childMeta === 'string') { | ||
childMeta = {module: childMeta}; | ||
} | ||
// create child logger with merged meta data from parent | ||
const child = Object.create(this); | ||
child.meta = Object.assign({}, this.meta, childMeta); | ||
child.setLevels(levels); | ||
return child; | ||
}; | ||
logger = container.loggers[id] = wrapper; | ||
@@ -111,2 +127,40 @@ } | ||
// class to handle pretty printing module name | ||
class ModuleConsoleTransport extends winston.transports.Console { | ||
constructor(config) { | ||
super(config); | ||
this.modulePrefix = config.bedrock.modulePrefix; | ||
} | ||
log(level, msg, meta, callback) { | ||
if(this.modulePrefix && 'module' in meta) { | ||
// add pretty module prefix | ||
msg = '[' + meta.module + '] ' + msg; | ||
// copy to avoid changing shared original | ||
meta = Object.assign({}, meta); | ||
// remove module so not re-printed as details | ||
delete meta.module; | ||
} | ||
super.log(level, msg, meta, callback); | ||
} | ||
} | ||
// class to handle pretty printing module name | ||
class ModuleFileTransport extends winston.transports.File { | ||
constructor(config) { | ||
super(config); | ||
this.modulePrefix = config.bedrock.modulePrefix; | ||
} | ||
log(level, msg, meta, callback) { | ||
if(this.modulePrefix && 'module' in meta) { | ||
// add pretty module prefix | ||
msg = '[' + meta.module + '] ' + msg; | ||
// copy to avoid changing shared original | ||
meta = Object.assign({}, meta); | ||
// remove module so not re-printed as details | ||
delete meta.module; | ||
} | ||
super.log(level, msg, meta, callback); | ||
} | ||
} | ||
/** | ||
@@ -121,6 +175,6 @@ * Initializes the logging system. | ||
var transports = container.transports; | ||
transports.console = new winston.transports.Console(config.loggers.console); | ||
transports.app = new winston.transports.File(config.loggers.app); | ||
transports.access = new winston.transports.File(config.loggers.access); | ||
transports.error = new winston.transports.File(config.loggers.error); | ||
transports.console = new ModuleConsoleTransport(config.loggers.console); | ||
transports.app = new ModuleFileTransport(config.loggers.app); | ||
transports.access = new ModuleFileTransport(config.loggers.access); | ||
transports.error = new ModuleFileTransport(config.loggers.error); | ||
transports.email = new WinstonMail(config.loggers.email); | ||
@@ -224,19 +278,27 @@ | ||
meta = meta || {}; | ||
var preformatted = null; | ||
if(brUtil.isObject(meta)) { | ||
preformatted = meta.preformatted; | ||
delete meta.preformatted; | ||
let preformatted = null; | ||
let metaIsObject = brUtil.isObject(meta); | ||
let module = null; | ||
if(metaIsObject) { | ||
if('preformatted' in meta) { | ||
preformatted = meta.preformatted; | ||
delete meta.preformatted; | ||
} | ||
if('module' in meta) { | ||
module = meta.module; | ||
delete meta.module; | ||
} | ||
} | ||
// stringify and include the worker PID in the meta information | ||
var json; | ||
let json; | ||
try { | ||
json = JSON.stringify(meta, null, 2); | ||
} catch(e) { | ||
json = JSON.stringify(cycle.decycle, null, 2); | ||
json = JSON.stringify(cycle.decycle(meta), null, 2); | ||
} | ||
var error; | ||
let error; | ||
if(meta instanceof Error) { | ||
error = ('stack' in meta) ? meta.stack : meta; | ||
meta = {error: error, workerPid: process.pid}; | ||
} else if(brUtil.isObject(meta) && 'error' in meta) { | ||
} else if(metaIsObject && 'error' in meta) { | ||
error = ('stack' in meta.error) ? meta.error.stack : meta.error; | ||
@@ -258,2 +320,7 @@ meta = {error: error, workerPid: process.pid}; | ||
// only add module if it was specified | ||
if(module) { | ||
meta.module = module; | ||
} | ||
// send logger message to master | ||
@@ -260,0 +327,0 @@ process.send({ |
{ | ||
"name": "bedrock", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "A core foundation for rich Web applications.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -696,2 +696,27 @@ # bedrock | ||
Bedrock supports multi-level child loggers with common metadata. These are | ||
created with `bedrock.loggers.get('app').child({...})`. Provided metadata will | ||
be added to child log output. A special `module` meta name can optionally be | ||
used for pretty output. A shortcut for creating named module loggers is | ||
`bedrock.loggers.get('app').child('name')`. | ||
Module prefix display can be controlled per-category: | ||
```js | ||
// get a child logger with custom module name | ||
let logger = bedrock.loggers.get('app').child('my-module'); | ||
// message module prefix controlled with a per-category config value | ||
bedrock.config.loggers.app.bedrock.modulePrefix = false; | ||
logger.info('an info message'); | ||
// module displayed as normal meta data: | ||
// 2017-06-30T12:34:56.789Z - info: an info message workerPid=1234, module=my-module | ||
// with module prefix enabled: | ||
bedrock.config.loggers.app.bedrock.modulePrefix = true; | ||
logger.info('an info message'); | ||
// displayed as an nice message prefix: | ||
// 2017-06-30T12:34:56.789Z - info: [my-module] an info message workerPid=1234 | ||
``` | ||
### bedrock.test | ||
@@ -698,0 +723,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
152401
2487
877
73