log4js-json-layout
Advanced tools
Comparing version 1.0.5 to 2.0.0
@@ -1,6 +0,1 @@ | ||
/** | ||
* Created by id0sch on 08/05/2016. | ||
*/ | ||
'use strict'; | ||
module.exports = require('./lib/jsonLayout'); | ||
module.exports = require('./lib/jsonLayout'); |
@@ -1,80 +0,110 @@ | ||
/** | ||
* Created by id0sch on 08/05/2016. | ||
*/ | ||
'use strict'; | ||
var util = require('util'); | ||
var _ = require('lodash'); | ||
const util = require('util'); | ||
const _ = require('lodash'); | ||
const colors = require('colors/safe'); | ||
const defaultLevelColors = { | ||
ALL: 'grey', | ||
TRACE: 'blue', | ||
DEBUG: 'cyan', | ||
INFO: 'green', | ||
WARN: 'yellow', | ||
ERROR: 'red', | ||
FATAL: 'magenta', | ||
MARK: 'grey', | ||
OFF: 'grey', | ||
}; | ||
function wrapErrorsWithInspect(items) { | ||
return _.chain(items).map(function (item) { | ||
if ((item instanceof Error) && item.stack) { | ||
return { | ||
inspect: function () { | ||
return util.format(item) + '\n' + item.stack; | ||
} | ||
}; | ||
} else if (!_.isObject(item)) { | ||
return item; | ||
} | ||
}).compact().value(); | ||
return _(items).map((item) => { | ||
if (_.isError(item) && item.stack) { | ||
return { | ||
inspect() { | ||
return `${util.format(item)}\n${item.stack}`; | ||
}, | ||
}; | ||
} else if (!_.isObject(item)) { | ||
return item; | ||
} | ||
return undefined; | ||
}).compact().value(); | ||
} | ||
function formatLogData(logData) { | ||
var data = Array.isArray(logData) ? logData : Array.prototype.slice.call(arguments); | ||
return util.format.apply(util, wrapErrorsWithInspect(data)); | ||
function formatLogData(data) { | ||
return util.format(...wrapErrorsWithInspect(data)); | ||
} | ||
function createDataOverlays(items) { | ||
// var data = Array.isArray(items) ? items : Array.prototype.slice.call(items); | ||
let overlay = {}; | ||
for (let item of items) { | ||
if (_.isObject(item)) { | ||
Object.assign(overlay, item); | ||
} | ||
const overlay = {}; | ||
_.forEach(items, (item) => { | ||
if (_.isObject(item)) { | ||
_.assign(overlay, item); | ||
} | ||
return overlay; | ||
}); | ||
return overlay; | ||
} | ||
function jsonLayout(config) { | ||
function formatter(data) { | ||
delete data.logger; | ||
let messageParam = config.messageParam || 'msg'; | ||
var output = { | ||
"startTime": data.startTime, | ||
"categoryName": data.categoryName, | ||
"level": data.level.levelStr | ||
}; | ||
if (config.source) { | ||
output.source = config.source; | ||
function formatter(raw) { | ||
const data = _.clone(raw); | ||
delete data.logger; | ||
const messageParam = config.messageParam || 'msg'; | ||
const output = { | ||
startTime: data.startTime, | ||
categoryName: data.categoryName, | ||
level: data.level.levelStr, | ||
}; | ||
if (config.source) { | ||
output.source = config.source; | ||
} | ||
const messages = _.isArray(data.data) ? data.data : [data.data]; | ||
output.data = formatLogData(messages); | ||
if (_.isEmpty(output.data)) { | ||
delete output.data; | ||
} | ||
const overlays = createDataOverlays(messages); | ||
if (_.has(overlays, messageParam)) { | ||
output.data = overlays[messageParam]; | ||
delete overlays[messageParam]; | ||
} | ||
_.assign(output, overlays); | ||
if (config.include && config.include.length) { | ||
const newOutput = {}; | ||
_.forEach(config.include, (key) => { | ||
if (_.has(output, key)) { | ||
newOutput[key] = output[key]; | ||
} | ||
let messages = Array.isArray(data.data) ? data.data : [data.data]; | ||
if (typeof messages !== 'string' && messages.length >= 1) { | ||
if (typeof messages[0] == 'string') { | ||
output.data = formatLogData(messages) | ||
} | ||
let overlays = createDataOverlays(messages); | ||
if (overlays.hasOwnProperty(messageParam)) { | ||
output.data = overlays[messageParam]; | ||
delete overlays[messageParam]; | ||
} | ||
Object.assign(output, overlays); | ||
} | ||
if (config.include && config.include.length) { | ||
var newOutput = {}; | ||
config.include.forEach(function (key) { | ||
if (output.hasOwnProperty(key)) { | ||
newOutput[key] = output[key]; | ||
} | ||
}); | ||
return newOutput; | ||
} else { | ||
return output; | ||
} | ||
}); | ||
return newOutput; | ||
} | ||
function layout(data) { | ||
var output = formatter(data); | ||
return JSON.stringify(output); | ||
return output; | ||
} | ||
return function layout(data) { | ||
let output = JSON.stringify(formatter(data)); | ||
// Add color to output; don't use this when logging. | ||
if (_.has(config, 'colors') && config.colors) { | ||
if (_.has(defaultLevelColors, data.level.levelStr)) { | ||
const color = defaultLevelColors[data.level.levelStr]; | ||
output = colors[color](output); | ||
} | ||
} | ||
return layout | ||
return output; | ||
}; | ||
} | ||
module.exports = jsonLayout; | ||
module.exports = jsonLayout; |
{ | ||
"name": "log4js-json-layout", | ||
"version": "1.0.5", | ||
"description": "provides a slim and easy to use json-layout for log4js-node", | ||
"version": "2.0.0", | ||
"description": "Provides a slim and easy to use json-layout for log4js-node", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/.bin/_mocha ./test.js", | ||
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls" | ||
"test": "nyc --reporter=html --reporter=text mocha", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"lint": "eslint ." | ||
}, | ||
@@ -15,2 +16,8 @@ "repository": { | ||
"author": "Ido Schachter <ido.schachter@ironsrc.com>", | ||
"contributors": [ | ||
{ | ||
"name": "Jon Peck", | ||
"email": "jpeck@fluxsauce.com" | ||
} | ||
], | ||
"license": "ISC", | ||
@@ -23,9 +30,20 @@ "bugs": { | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.9", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^2.4.5" | ||
"coveralls": "^2.13.1", | ||
"eslint": "^4.3.0", | ||
"eslint-config-airbnb": "^15.1.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-jsx-a11y": "^5.1.1", | ||
"eslint-plugin-lodash": "^2.4.4", | ||
"eslint-plugin-mocha": "^4.11.0", | ||
"eslint-plugin-react": "^7.1.0", | ||
"mocha": "^2.5.3", | ||
"nyc": "^11.0.3" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.15.0" | ||
"colors": "^1.1.2", | ||
"lodash": "^4.17.4" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
} | ||
} |
# log4js-json-layout | ||
A slim and easy to use JSON layout for [log4js-node](https://github.com/nomiddlename/log4js-node). | ||
[![NPM](https://nodei.co/npm/log4js-json-layout.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/log4js-json-layout/) | ||
provides a slim and easy to use json-layout for log4js-node (https://github.com/nomiddlename/log4js-node) | ||
[![Build Status](https://travis-ci.org/id0Sch/log4js-json-layout.svg?branch=master)](https://travis-ci.org/id0Sch/log4js-json-layout) | ||
# installation | ||
## Installation | ||
```bash | ||
npm install log4js-json-layout --save | ||
``` | ||
npm install log4js-json-layout | ||
``` | ||
# usage | ||
layout should be type 'json' | ||
## Example Output | ||
currently we support include options only, array of items is expected | ||
log object will contain these properties : ["startTime","categoryName","data","level"] | ||
source param will be added to each json object if provided | ||
Output: | ||
```json | ||
{"startTime":"2017-07-27T00:01:05.175Z","categoryName":"/path/to/file/redis-client.js","level":"DEBUG","data":"Connection to Redis successful!"} | ||
``` | ||
var log4js = require('log4js'); | ||
var jsonLayout = require('log4js-json-layout'); | ||
## Usage | ||
Set the layout type to `json`. | ||
Each log object contains the following properties: | ||
- `startTime` - time in ISO 8601 format | ||
- `categoryName` - specified when `log4js` is initialized | ||
- `data` - if the log message is a string, otherwise omitted | ||
- `level` - level in human readable format | ||
- `source` - if provided, will be included | ||
### Options | ||
- `type` - string, always `json` | ||
- `source` - optional string, just sets the property `source`. Example value: `development` | ||
- `include` - array of properties to include in the log object | ||
- `colors` - boolean; off by default. If set to true, colorizes the output using log4js default color scheme based on log level. Useful for development, do not use for storing logs. | ||
### Example Config | ||
```js | ||
const log4js = require('log4js'); | ||
const jsonLayout = require('log4js-json-layout'); | ||
log4js.layouts.addLayout('json', jsonLayout); | ||
``` | ||
Minimal: | ||
```js | ||
appenders = [{ | ||
type: 'console', | ||
messageParam : 'msg', | ||
layout: { | ||
type: 'json', | ||
source : 'development', | ||
include: ['startTime', 'categoryName'] | ||
} | ||
type: 'console', | ||
layout: { | ||
type: 'json', | ||
} | ||
]; | ||
}]; | ||
``` | ||
More options: | ||
```js | ||
appenders = [{ | ||
type: 'console', | ||
messageParam: 'msg', | ||
layout: { | ||
type: 'json', | ||
source: 'development', | ||
include: ['startTime', 'categoryName'], | ||
} | ||
}]; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
65220
22
245
76
2
11
1
+ Addedcolors@^1.1.2
+ Addedcolors@1.4.0(transitive)
Updatedlodash@^4.17.4