winston-meta-wrapper
Advanced tools
Comparing version 1.0.0 to 1.1.0
81
index.js
'use strict' | ||
var _ = require('lodash') | ||
var extend = require('extend.js') | ||
@@ -9,38 +8,60 @@ const formatRegExp = /%[sdj]/g | ||
var wrapper = function (logger) { | ||
var self = this | ||
var newLogger = {} | ||
return new WinstonMetaWrapper(logger) | ||
} | ||
_.each(_.functions(logger), function (funcName) { | ||
newLogger[funcName] = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
var meta = newLogger.getMeta() | ||
var fmtMatch = args[0] && args[0].match && args[0].match(formatRegExp) | ||
var formatArguments = fmtMatch ? fmtMatch.length : 0 | ||
if (_.isObject(args[formatArguments + 1])) { | ||
args[formatArguments + 1] = extend(meta, args[formatArguments + 1]) | ||
} else { | ||
args.splice(formatArguments + 1, 0, meta) | ||
} | ||
logger[funcName].apply(logger, args) | ||
} | ||
}) | ||
var WinstonMetaWrapper = function (logger) { | ||
this._meta = {} | ||
this._logger = logger | ||
} | ||
newLogger.addMeta = function (meta) { | ||
if (!this.meta) { | ||
this.meta = {} | ||
} | ||
this.meta = extend(this.meta, meta) | ||
WinstonMetaWrapper.prototype.log = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
var fmtMatch = args[1] && args[1].match && args[1].match(formatRegExp) | ||
var formatArguments = fmtMatch ? fmtMatch.length : 0 | ||
if (_.isObject(args[formatArguments + 2])) { | ||
var meta = _.cloneDeep(this._meta) | ||
_.merge(meta, args[formatArguments + 2]) | ||
args[formatArguments + 2] = meta | ||
} else { | ||
args.splice(formatArguments + 2, 0, _.cloneDeep(this._meta)) | ||
} | ||
this._logger.log.apply(this._logger, args) | ||
} | ||
newLogger.getMeta = function () { | ||
if (!this.meta) { | ||
return {} | ||
} else { | ||
return this.meta | ||
} | ||
} | ||
WinstonMetaWrapper.prototype.error = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
args.unshift('error') | ||
this.log.apply(this, args) | ||
} | ||
return newLogger | ||
WinstonMetaWrapper.prototype.warn = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
args.unshift('warn') | ||
this.log.apply(this, args) | ||
} | ||
WinstonMetaWrapper.prototype.info = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
args.unshift('info') | ||
this.log.apply(this, args) | ||
} | ||
WinstonMetaWrapper.prototype.verbose = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
args.unshift('verbose') | ||
this.log.apply(this, args) | ||
} | ||
WinstonMetaWrapper.prototype.debug = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
args.unshift('debug') | ||
this.log.apply(this, args) | ||
} | ||
WinstonMetaWrapper.prototype.silly = function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
args.unshift('silly') | ||
this.log.apply(this, args) | ||
} | ||
WinstonMetaWrapper.prototype.addMeta = function (meta) { | ||
_.merge(this._meta, meta) | ||
} | ||
module.exports = wrapper |
{ | ||
"name": "winston-meta-wrapper", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Wraps Winston logger to always add a set of metadata", | ||
@@ -25,8 +25,8 @@ "main": "index.js", | ||
"dependencies": { | ||
"extend.js": "0.0.2", | ||
"lodash": "^4.13.1" | ||
"lodash": "^4.14.2" | ||
}, | ||
"devDependencies": { | ||
"publish": "^0.6.0" | ||
"publish": "^0.6.0", | ||
"winston": "^2.2.0" | ||
} | ||
} |
# winston-meta-wrapper | ||
Wraps winston logger instance to always add a set of metadata | ||
This wrapper around winston loggers allows you to log a static or dynamic set of metadata with each log statement being executed without explicitely adding it as meta argument. | ||
It also fixes the [JSON string interpolation issue in winston](https://github.com/winstonjs/winston/issues/745) | ||
## Installation | ||
```js | ||
npm install winston-meta-wrapper | ||
``` | ||
## Usage | ||
### Wrapping Winston Logger to add static metadata | ||
```js | ||
var winston = require('winston') | ||
var winstonWrapper = require('winston-meta-wrapper') | ||
var logger = new winston.Logger({ | ||
transports: [ | ||
new winston.transports.Console() | ||
] | ||
}) | ||
logger = winstonWrapper(logger) | ||
logger.addMeta({ | ||
test: 'Test Meta' | ||
}) | ||
logger.info('test message') | ||
``` | ||
### Wrapping Winston Logger to add dynamic metadata | ||
```js | ||
var winston = require('winston') | ||
var winstonWrapper = require('winston-meta-wrapper') | ||
var logger = new winston.Logger({ | ||
transports: [ | ||
new winston.transports.Console() | ||
] | ||
}) | ||
logger = winstonWrapper(logger) | ||
logger.getMeta = function () { | ||
return { | ||
module: Math.random() | ||
} | ||
} | ||
logger.info('test message') | ||
logger.info('test message') | ||
``` | ||
### Chaining wrapped Winston Loggers | ||
```js | ||
var winston = require('winston') | ||
var winstonWrapper = require('winston-meta-wrapper') | ||
var logger = new winston.Logger({ | ||
transports: [ | ||
new winston.transports.Console() | ||
] | ||
}) | ||
logger = winstonWrapper(logger) | ||
logger.addMeta({ | ||
test: 'Test Meta' | ||
}) | ||
logger = winstonWrapper(logger) | ||
logger.addMeta({ | ||
test2: 'Test 2 Meta' | ||
}) | ||
logger.info('test message') | ||
``` |
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
6861
1
9
100
87
2
- Removedextend.js@0.0.2
- Removedextend.js@0.0.2(transitive)
Updatedlodash@^4.14.2