Skip to content

Commit

Permalink
Merge pull request #14 from veseo/sentry
Browse files Browse the repository at this point in the history
Multiadapter support & sentry аdapter implementation
  • Loading branch information
tecs committed Feb 8, 2021
2 parents 283f7fa + ccf30f0 commit cf324bb
Show file tree
Hide file tree
Showing 9 changed files with 11,668 additions and 238 deletions.
107 changes: 99 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,121 @@
# Logger Factory

This logger factory produces Logger instances that can have different prefixes,
but all share the same logging level that is set for the LoggerFactory instance.
but all share the same logging level that is set for the LoggerFactory instance.
Note that logging level is configured per adapter, i.e. you can be more verbose with one adapter
and less verbose for another.

### Usage

The following example creates a LoggerFactory instance, using only a console adapter with log level `info`,
and that instance is used to create two Logger instances with `Classname` and `Classname2` prefixes respectively.

```
const LoggerFactory = require('./LoggerFactory');
const infoLoggerFactory = new LoggerFactory('info');
const infoLoggerFactory = new LoggerFactory({
adapters: [{
name: Adapters.Console,
config: {
logLevel: 'info'
}
}]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');
```

This example creates a LoggerFactory instance of log level `info`, and that instance is used to
create two Logger instances with `Classname` and `Classname2` prefixes respectively.
The following example creates a LoggerFactory instance, using only two adapters - console and sentry, with log levels `info` and `warn` respectively,
meaning that all messages above info will only be logged via the Console adapter, while warn and above will also be logged via Sentry adapter.
After that, similarly to our previous example, that LoggerFactory instance is used to create two Logger instances with `Classname` and `Classname2` prefixes respectively.

```
const LoggerFactory = require('./LoggerFactory');
const infoLoggerFactory = new LoggerFactory({
adapters: [
{
name: Adapters.Console,
config: {
logLevel: 'info'
}
},
{
name: Adapters.Sentry,
config: {
logLevel: 'warn',
dsn: 'https://5637ee1e65504e02b1ba62255ac1f23a@youserver.com/6',
environment: 'testing',
}
}
]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');
```

### LoggerFactory methods

- `create({ logLevel, adapter})` - Creates an instance of the requested adapter (specified by `adapter`, defaults to `console`)
with the provided `logLevel` (defaults to `warn`). List of supported log levels is provided below in this README.
- `constructor ({ adapters })` - Creates an instance LoggerFactory instance with the requested array of adapters. List of the supported adapters,
along with their configuration requirements, follows below.
- `create(prefix?: string)` - Creates an instance of the logger, using already specified adapters.

### Adapters

Currently, only one adapter is supported - a console logger, that can be instanced by providing `console`
as adapter value.
The following is a list of all supported adapters, along wit an example usage of each of them.
The possible logging levels are the same for each of the adapters and are listed below this list.

Default configuration (works for all adapters):

- `skipTimestamps` Whether to include the timestamps in the logged message. Can be turned off if using kubernetes,
since it has integrated functionality to timestamp all messages. Defaults to false.
- `logLevel` - The minimum logging level that will be logged with that adapter.

#### Console adapter

Console adapter has no additional configuration.

```
const LoggerFactory = require('./LoggerFactory');
const infoLoggerFactory = new LoggerFactory({
adapters: [{
name: Adapters.Console,
config: {
logLevel: 'info'
}
}]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');
```

#### Sentry adapter

Sentry has the following additional configuration settings:

- `dsn` - Client key, used by Sentry to determine where to send the event to
- `environment` - Used to separate errors from different environments
- `debug` - Whether to enable Sentry adapter in debug mode (NOTE: even in debug mode, not working Sentry server
will not crash the code). Defaults to false.

```
const LoggerFactory = require('./LoggerFactory');
const infoLoggerFactory = new LoggerFactory({
adapters: [{
name: Adapters.Sentry,
config: {
logLevel: 'warn',
dsn: 'https://5637ee1e65504e02b1ba62255ac1f23a@yoursentryserver.com/6',
environment: 'testing',
debug: false,
}
}]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');
```

### Log levels

Expand Down
Loading

0 comments on commit cf324bb

Please sign in to comment.