Logger ·
####Introduction
Zero dependencies Logger module, for consoling messages with given level and metadata.
Installing
$ yarn add @homeappcorporate/logger
or
$ npm install @homeappcorporate/logger --save
Usage
For initialization, you need to create an instance of the Logger
.
Basic example:
import Logger from '@homeapp/logger';
const logger = new Logger();
logger.error(new Error('Some message'), { someMeta: 'someValue' });
logger.info('Info message', { someMeta: 'someValue' });
Logger constructor Parameters
Parameter | Type | Default | Required | Description |
---|---|---|---|---|
loggers | array<object> |
[] | no | List of loggers, must have required methods (error , info , warn ) |
Logger instance Methods
Method | Description |
---|---|
error(event, metadata) | Consoling a given event with error level |
info(event, metadata) | Same as error(event, metadata) , but with level info
|
warn(event, metadata) | Same as error(event, metadata) , but with level warn
|
WARNING! Every logger, which was passed into loggers
array in a constructor, must have three required methods: error
, warn
and info
.
Usage example
import Logger from '@homeapp/logger';
function post(event, meta, level) {
console.log(`...requesting custom logger to remote server with level - ${level}`);
fetch('hhtp://some-logger-server', {
method: 'POST',
body: JSON.stringify({ event, meta, level }),
});
}
class MyCustomLogger {
constructor(config) {
// some cutom logic
}
error(event, metadata) {
post(event, metadata, 'error');
}
warn(event, metadata) {
post(event, metadata, 'warn');
}
info(event, metadata) {
post(event, metadata, 'info');
}
}
const customLogger = new MyCustomLogger({ someConfig: 'value' });
const logger = new Logger({ loggers: [customLogger] });
logger.error('Some message', { someMeta: 'someValue' });
this code will console
Error: Some message
meta data: {
"someMeta": someValue
}
...requesting custom logger to remote server, with level error