Abstract Winston JSON Logging Tool
A Simple JSON Logging Tool that is built on top of Winston
Installation
Yarn:
yarn add abstract-winston-json
NPM:
npm install abstract-winston-json
Output
{
"type": "log.type",
"message": {
"data": {
"more": "data",
},
},
"metadata": {
"userId": "123ABC",
},
}
API
Logger
Parameters
Param | Type | Description | Default Value |
---|
metadata | Object | Metadata Object | {} |
debugMode | Boolean | Debug Mode | false |
Example
const Logger = require('abstract-winston-json');
const metadata = {
userId: 'ABC123',
};
const logger = new Logger({ metadata });
const logger = new Logger({
metadata,
debugMode: true,
});
addMetadata(key, value)
Add Metadata to include in every log.
Parameters
Param | Type | Description | Default Value |
---|
key | String | Metadata Key | |
value | Any | Metadata Value | |
Example
logger.addMetadata('key', 'value');
removeMetadata(key)
Remove specific Metadata from logs from now on.
Parameters
Param | Type | Description | Default Value |
---|
key | String | Metadata Key | {} |
Example
logger.removeMetadata('key');
log(type, message)
Log via the info
level.
Parameters
Param | Type | Description | Default Value |
---|
type | String | Log Type Key | |
message | Any | Log Message | |
Example
logger.log('log.type', {
requestInfo: 'example',
data: {},
});
warn(type, message)
Log via the warn
level.
Parameters
Param | Type | Description | Default Value |
---|
type | String | Log Type Key | |
message | Any | Log Message | |
Example
logger.warn('warn.type', {
requestInfo: 'example',
data: {},
});
error(type, message) || err(type, message)
Log via the error
level.
Parameters
Param | Type | Description | Default Value |
---|
type | String | Log Type Key | |
message | Any | Log Message | |
Example
logger.error('error.type', {
requestInfo: 'example',
data: {},
});
logger.err('error.type', {
requestInfo: 'example',
data: {},
});
debug(type, message)
Will only log when debug mode is set to true
.
Logs via the info
level.
Parameters
Param | Type | Description | Default Value |
---|
type | String | Log Type Key | |
message | Any | Log Message | |
Example
logger.debug('error.type', {
requestInfo: 'example',
data: {},
});
getLogger()
Gets the underlying Winston Logging Instance
Example
const winston = logger.getLogger();