An open API service indexing awesome lists of open source software.

https://github.com/salimkayabasi/log4json

JSON layout for log4js
https://github.com/salimkayabasi/log4json

json kibana log4js logging

Last synced: 2 months ago
JSON representation

JSON layout for log4js

Awesome Lists containing this project

README

          

[![NPM](https://nodei.co/npm/log4json.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/log4json)

![Github Actions](https://github.com/salimkayabasi/log4json/actions/workflows/check.yml/badge.svg)

[![Dependency Status](https://david-dm.org/salimkayabasi/log4json.svg)](https://david-dm.org/salimkayabasi/log4json)
[![DevDependency Status](https://david-dm.org/salimkayabasi/log4json/dev-status.svg)](https://david-dm.org/salimkayabasi/log4json#info=devDependencies)
[![PeerDependency Status](https://david-dm.org/salimkayabasi/log4json/peer-status.svg)](https://david-dm.org/salimkayabasi/log4json#info=peerDependencies)

# Log4JSON

JSON layout for [log4js](https://www.npmjs.com/package/log4js). Zero dependency and super slim.

## Options

### `separator` (default: `' '`)
Once you have multiple messages as in string format, `logger` will concat all of them and needs a separator.
This value will be used while concatenating the strings.

### `space` (default: `null`)
This parameter will help you to format your output. By default, it will log whole output in one line, otherwise multilines.
Please check the `space` argument on [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) documentation.

### `omitDefaultCategory` (default: `true`)
In order to reduce the redunnant log size, default category name will be omitted. In case of you want to have category name as always.
You can force logger to include category name even when it is deafult

### `props`
Any of the property names can be overridden. You can override all field names on output.
Default propery names are;
```json
{
"ts": "ts",
"level": "level",
"category": "category",
"stack": "stack",
"message": "message",
"callStack": "callStack"
}
```

## Full Example

https://runkit.com/salimkayabasi/log4json

```javascript
const log4js = require('log4js');
const log4json = require('log4json');

addLayout('log4json', log4json);

configure({
appenders: {
log4json: {
type: 'console',
layout: {
type: 'log4json',
separator: ' | ',
space: 2,
omitDefaultCategory: false,
props: {
ts: '@timestamp',
level: 'lvl',
category: 'cat', // default category will be omitted unless "omitDefaultCategory" is false
stack: 'error', // only available when an error object is being logged
message: 'context',
callStack: 'stack', // only available when "enableCallStack" is true
}
}
}
},
categories: {
default: {
appenders: ['log4json'],
level: 'all',
enableCallStack: true
}
}
});

const logger = getLogger('fancy-category');
logger.debug('string logs', 'another string log', {customProp: ['also listed', true]}, 'yet another string');
```

#### output
```json
{
"customProp": [
"also listed",
true
],
"context": "string logs | another string log | yet another string",
"@timestamp": "2021-05-10T15:25:49.130Z",
"lvl": "DEBUG",
"cat": "fancy-category",
"stack": "src/log4json.js:37:8"
}
```