Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from Enrise/feature/extra-transport-config
Browse files Browse the repository at this point in the history
Higher customization options of transports
  • Loading branch information
Christiaan van Bemmel authored Nov 18, 2016
2 parents 960e810 + bebac79 commit ead88f3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ The `log` object contains functions for each [log-level](#levels):
- `log.info('Some log message');`
- `log.error(new Error('Some error'));`

#### `.get(name: String, [level: String], [transportConfig: Object])`
The `.get()` function allows additional customization. The level can overwrite the logging-level defined during initialization. A third argument can be passed to overwrite transport configuration. This will be merged onto the object passed to the transports.

### Configuration

The default configuration looks as follows. Everything can be overwritten on initialization.
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Logger(settings) {
return new Logger(settings);
}

this._settings = _.defaultsDeep(settings, defaultSettings);
this._settings = _.defaultsDeep(_.cloneDeep(settings), defaultSettings);
this._container = new winston.Container({
exitOnError: false
});
Expand All @@ -20,9 +20,13 @@ function Logger(settings) {
module.exports = this;
}

Logger.prototype.get = function (label, level) {
Logger.prototype.get = function (label, level, transportConfig) {
const conf = _.cloneDeep(this._settings.transports);

if (_.isPlainObject(transportConfig)) {
_.merge(conf, transportConfig);
}

// Filter out falsey values
const transportKeys = _.keys(_.pickBy(this._settings.transports, _.identity));

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "enrise-logger",
"description": "Logger used within Enrise projects and module's",
"version": "0.2.0",
"version": "0.2.1",
"author": "Team MatchMinds @ Enrise",
"main": "index.js",
"license": "MIT",
Expand Down
35 changes: 35 additions & 0 deletions test/logger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,39 @@ describe('Logger', () => {
label: 'TEST'
});
});

it('clones the passed configuration', () => {
const config = {
foo: 'bar',
deep: {
yes: []
}
};

const logger = new Logger(config);

// Default values will be merged on the config, it is not modified if it is still the same.
expect(config).to.deep.equal({
foo: 'bar',
deep: {
yes: []
}
});
expect(config).to.not.equal(logger._settings);
});

it('allows passing extra transport configuration to the .get() function', () => {
new Logger().get('label', null, {
Console: {
extra: 'settings'
}
});

expect(winston.transports.Console).to.have.been.calledWith({
level: 'info',
colorize: true,
extra: 'settings',
label: 'label'
});
});
});

0 comments on commit ead88f3

Please sign in to comment.