log4js-json-layout
Advanced tools
Comparing version 2.1.0 to 2.2.2
@@ -70,2 +70,15 @@ const util = require('util'); | ||
// Emit properties set by using functions defined in 'dynamic' | ||
if (_.has(config, 'dynamic')) { | ||
// Iterate over the 'dynamic' properties | ||
_.forIn(config.dynamic, (dynamicFunction, key) => { | ||
if (_.isFunction(dynamicFunction)) { | ||
const value = dynamicFunction(); | ||
if (_.isString(value) || _.isNumber(value) || _.isBoolean(value)) { | ||
output[key] = value; | ||
} | ||
} | ||
}); | ||
} | ||
const messages = _.isArray(data.data) ? data.data : [data.data]; | ||
@@ -72,0 +85,0 @@ |
{ | ||
"name": "log4js-json-layout", | ||
"version": "2.1.0", | ||
"version": "2.2.2", | ||
"description": "Provides a slim and easy to use json-layout for log4js-node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,2 +44,3 @@ # log4js-json-layout | ||
- `static` - object, name value pairs of this object are added to output | ||
- `dynamic` - object, where each value is a no-argument function that must return a string/number/boolean. The key and the function's return value are added to the output | ||
@@ -52,3 +53,3 @@ ### Example Config | ||
log4js.layouts.addLayout('json', jsonLayout); | ||
log4js.addLayout('json', jsonLayout); | ||
``` | ||
@@ -79,2 +80,13 @@ | ||
}, | ||
dynamic: { | ||
transactionId: function() { | ||
return 'tx-id'; // string | ||
}, | ||
isAdmin: function() { | ||
return 1>0; // boolean | ||
}, | ||
countLogin: function() { | ||
return 20; // number | ||
} | ||
}, | ||
include: ['startTime', 'categoryName'], | ||
@@ -84,2 +96,10 @@ } | ||
const logger = log4js.getLogger('info'); | ||
logger.info('Test log'); | ||
``` | ||
Output: | ||
```json | ||
{"startTime":"2017-07-27T00:01:05.175Z","categoryName":"/path/to/file/redis-client.js","level":"DEBUG","transactionId": "tx-id","isAdmin": true,"countLogin": 20,"data":"Test log",} | ||
``` |
@@ -153,3 +153,3 @@ const _ = require('lodash'); | ||
const actual = JSON.parse(layout({ | ||
static: { appName: 'testapp', source: 'development' } | ||
static: { appName: 'testapp', source: 'development' }, | ||
})(data)); | ||
@@ -162,2 +162,32 @@ actual.id.should.equal(123); | ||
it('should add dynamic fields when configured', function () { | ||
data.data = [{ id: 123, data: 'aaa' }]; | ||
// eslint-disable-next-line lodash/prefer-constant | ||
const getTransactionId = () => 'tx-id'; | ||
// eslint-disable-next-line lodash/prefer-constant | ||
const getIsAdmin = () => true; | ||
// eslint-disable-next-line lodash/prefer-constant | ||
const getCountLogin = () => 10; | ||
const actual = JSON.parse(layout({ | ||
dynamic: { | ||
// Supported | ||
transactionId: getTransactionId, | ||
isAdmin: getIsAdmin, | ||
countLogin: getCountLogin, | ||
// Function with arguements not supported | ||
someArg: x => x, | ||
// Non string/number/boolean returning functions not supported | ||
nonSupportedReturn: () => new Date(), | ||
} })(data)); | ||
actual.should.deep.equal({ startTime: '615 Ludlam Place, Nicholson, New Mexico, 5763', categoryName: '572efdaaa64be9dbc56369ae', level: 'INFO', transactionId: getTransactionId(), isAdmin: getIsAdmin(), countLogin: getCountLogin(), id: 123, data: 'aaa' }); | ||
}); | ||
it('should still pick specific keys when static fields are configured', function () { | ||
@@ -164,0 +194,0 @@ const actual = layout({ |
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
301
101
17595
13