npm package: @pushwoosh/logger
Description: Logger module for get info from pushwoosh projects.
Latest version: 1.0.6

# Logger
Logger module for get info from pushwoosh projects.

## Getting Started

### Info

#### message
```javascript
{
  applicationCode: string | undefined,
  applicationType: string,
  domain: string | undefined,
  text: string,
  type: string,
  code: string | number | undefined,
  deviceInfo: string | object,
  
  [key: string]: any
}
``` 


#### Levels
Table of rules when call subscribers:

| Config level / Action | Silent  | Fatal   | Error  | Warn    | Info    | Debug   |
| --------------------- | :-----: | :-----: | -----: | :-----: | :-----: | :-----: |
| fatal                 | 0       |  X      |  X     |  X      | X       | X       |
| error                 | 0       |  0      |  X     |  X      | X       | X       |
| warn                  | 0       |  0      |  0     |  X      | X       | X       |
| info                  | 0       |  0      |  0     |  0      | X       | X       |
| debug                 | 0       |  0      |  0     |  0      | 0       | X       |

### Example Usage
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger({
  level: 'info'
});

logger.subscribe('fatal', (messsage) => console.log(messsage.text));
logger.subscribe('error', (messsage) => console.log(messsage.text));
logger.subscribe('warn', (messsage) => console.log(messsage.text));
logger.subscribe('info', (messsage) => console.log(messsage.text));
logger.subscribe('debug', (messsage) => console.log(messsage.text));


logger.fatal('Some fatal message', 'FATAL-XXX');    // =>  Some fatal message
logger.error('Some error message', 'ERROR-XXX');    // =>  Some error message
logger.warn('Some warn message', 'WARN-XXX');       // =>  Some warn message
logger.info('Some info message');                   // =>  Some info message
logger.debug('Some debug message');                 // none, because level is 'info';
```

or

```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger({
  level: 'info'
});

logger.fatal('Some fatal message', 'FATAL-XXX', {
  handler: (message) => console.log(`${message.code}: ${message.text}`)
});
// =>  FATAL-XXX: Some fatal message

logger.debug('Some debug message', {
  handler: (message) => console.log(message.text)
});
// =>  // none, because level is 'info';
```


### Installation
Install dependency:
```
npm install --save @pushwoosh/logger
```


### Config ```new Logger([config])```
#### level - ```'silent', 'fatal', 'error', 'warn', 'info', 'debug'``` default - ```'error'```;
Usage:
```javascript
new Logger({level: 'silent'})
```

#### applicationType - ```'sdk', 'cp'``` default ```'sdk'```;
Usage:
```javascript
new Logger({
  applicationType: 'cp'
})
```

#### subscribers - array of subscriber default ```[]```;
Usage:
```javascript
new Logger({
  subscribers: [{
    level: 'debug',
    handler: (message) => {
      console.log(message);
    }
  }]
})
```


## API

#### `subscribe(level, (message) => {...})` - add new handler to processing log
Usage:
```javascript
const logger = new Logger();

logger.subscribe('fatal', (message) => console.log(message.text));
```


#### ```fatal(message, [code], [options])``` - emit fatal log
#### ```error(message, [code], [options])``` - emit error log
#### ```warn(message, [options])```  - emit warn log
#### ```debug(message, [options])```  - emit debug log
#### ```info(message, [options])```  - emit info log
Usage:
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger({level: 'info'});

logger.subscribe('error', (message) => {
  console.log(message);
});


logger.fatal('Some error message', 'fatal-XXX');
// =>  FATAL-XXX: Some fatal message

logger.fatal('Some error message', 'fatal-XXX', {
  handler: (message) => console.log(message.someKey),
  someKey: 'someValue'
});
// =>  FATAL-XXX: Some fatal message
// =>  someValue
```

#### `updateApplicationCode(code)` - update applicationCode
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger();

logger.updateApplicationCode('XXXXX-XXXXX');

logger.subscribe('fatal', (message) => console.log(message));

logger.fatal('Some fatal message', 'FATAL-XXX'); 
// => 
// {
//   applicationCode: 'XXXXX-XXXXX',
//   applicationType: 'sdk',
//   text: 'Some fatal message, 
//   code: 'FATAL-XXX'
// };
```

#### `updateDomain(domain)` - update domain
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger();

logger.updateDomain('https://localhost');

logger.subscribe('fatal', (message) => console.log(message));

logger.fatal('Some fatal message', 'FATAL-XXX'); 
// => 
// {
//   domain: 'https://localhost',
//   applicationType: 'sdk',
//   text: 'Some fatal message, 
//   code: 'FATAL-XXX'
// };
```

#### `updateApplicationType(type)` - update applicationType
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger();

logger.updateApplicationType('cp');
logger.subscribe('fatal', (message) => console.log(message));
logger.fatal('Some fatal message', 'FATAL-XXX'); 
// => 
// {
//   applicationType: 'cp',
//   text: 'Some fatal message, 
//   code: 'FATAL-XXX'
// };
```

#### `updateLogLevel(type)` - update applicationType
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger();

logger.subscribe('debug', (message) => console.log(message));

logger.debug('Some fatal message'); 
// => nothing

logger.updateLogLevel('debug');
logger.debug('Some fatal message'); 
// => 
// {
//   applicationType: 'sdk',
//   text: 'Some debug message, 
// };
```

#### `updateDeviceInfo(type)` - update applicationType
```javascript
import {Logger} from '@pushwoosh/logger';

const logger = new Logger();

logger.updateDeviceInfo({key: 'value'});
logger.subscribe('fatal', (message) => console.log(message));
logger.fatal('Some fatal message', 'FATAL-XXX'); 
// => 
// {
//   applicationType: 'cp',
//   text: 'Some fatal message, 
//   code: 'FATAL-XXX',
//   deviceInfo: {key: 'value'}
// };
```

## License

MIT