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 #7 from Enrise/feature/simplify-settings
Browse files Browse the repository at this point in the history
Simplify configuration and remove getXConfig functions
  • Loading branch information
Edward Smit authored Nov 3, 2016
2 parents c6d280f + adb82cb commit 960e810
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 89 deletions.
31 changes: 9 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ The `log` object contains functions for each [log-level](#levels):
The default configuration looks as follows. Everything can be overwritten on initialization.
```javascript
{
transports: ['Console'],
getConsoleConfig: (config) => {
return config.console;
},
getLogstashUDPConfig: (config) => {
return config.logstashUDP;
},
winston: {
console: {
transports: {
Console: {
level: 'info',
colorize: true
},
logstashUDP: {}
}
},
levels: {
error: 0,
Expand All @@ -57,17 +49,12 @@ The default configuration looks as follows. Everything can be overwritten on ini
}
```

#### `transports`
Define all transports that the logger should use. Defaults to only the Console. The other possible transport is `LogstashUDP`. These can be used together.

#### `getConsoleConfig` & `getLogstashUDPConfig`
Overwrite and modify the configuration used for their corresponding transport. The `config` object is _always_ a clone of the `config.winston` object.

#### `winston`
An object with specific configuration for the transporters.
- `console`: [winston.Console documentation](https://github.com/winstonjs/winston)
- `logstashUDP`: [winston.LogstashUDP documentation](https://www.npmjs.com/package/winston-logstash-udp)
#### `transports: Object`
The keys define the transports that the logger should use, the value is the configuration passed to the transport constructor. Multiple transports can be combined. Defaults to only the Console with the settings above. To exclude the Console transport, set it to `null`. Possible transports are:
- `Console`: [winston.Console documentation](https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport)
- `File`: [winston.File documentation](https://github.com/winstonjs/winston/blob/master/docs/transports.md#file-transport)
- `Http`: [winston.Http documentation](https://github.com/winstonjs/winston/blob/master/docs/transports.md#http-transport)
- `LogstashUDP`: [winston.LogstashUDP documentation](https://www.npmjs.com/package/winston-logstash-udp)

#### `levels`
The node-logger uses more detailed log-levels than winston does. The higher the priority the more important the message is considered to be, and the lower the corresponding integer priority. These levels can be modified to your liking.

13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ function Logger(settings) {
}

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

const transports = _.map(this._settings.transports, (transport) => {
const transportSettings = conf[_.lowerFirst(transport)];
// Filter out falsey values
const transportKeys = _.keys(_.pickBy(this._settings.transports, _.identity));

const transports = _.map(transportKeys, (transport) => {
const transportSettings = conf[transport];

transportSettings.label = label;
transportSettings.level = level || transportSettings.level;

const transportConfig = this._settings[`get${transport}Config`](conf);

return new winston.transports[transport](transportConfig);
return new winston.transports[transport](transportSettings);
});

// Create a new logger with the console transport layer by default
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.1.1",
"version": "0.2.0",
"author": "Team MatchMinds @ Enrise",
"main": "index.js",
"license": "MIT",
Expand Down
16 changes: 4 additions & 12 deletions settings.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
module.exports = {
getConsoleConfig: (config) => {
return config.console;
},
getLogstashUDPConfig: (config) => {
return config.logstashUDP;
},
transports: ['Console'],
longtrace: false,
winston: {
console: {
transports: {
Console: {
level: 'info',
colorize: true
},
logstashUDP: {}
}
},
longtrace: false,
levels: {
error: 0,
warn: 1,
Expand Down
80 changes: 32 additions & 48 deletions test/logger.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const _ = require('lodash');
const chai = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
Expand Down Expand Up @@ -35,10 +34,6 @@ const winston = {
loggers: {
add: sinon.stub().returns(winstonCLI)
},
transports: {
Console: sinon.stub(),
LogstashUDP: sinon.stub()
},
Container: sinon.stub().returns(winstonContainer)
};

Expand All @@ -47,6 +42,13 @@ const Logger = proxyquire('../index.js', {
winston: winston
});

beforeEach(() => {
winston.transports = {
Console: sinon.stub(),
LogstashUDP: sinon.stub()
};
});

describe('Logger', () => {
it('throws an error when trying to use without instantiating', () => {
let err;
Expand All @@ -71,28 +73,22 @@ describe('Logger', () => {

it('overwrites and merges with default settings', () => {
const logger = new Logger({
winston: {
console: {
transports: {
Console: {
level: 'verbose'
}
}
});
expect(logger._settings).to.deep.equal({
transports: ['Console'],
getConsoleConfig: logger._settings.getConsoleConfig,
getLogstashUDPConfig: logger._settings.getLogstashUDPConfig,
longtrace: false,
winston: {
console: {
transports: {
Console: {
level: 'verbose',
colorize: true
},
logstashUDP: {}
}
},
levels: levels
});
expect(logger._settings.getConsoleConfig).to.be.a('function');
expect(logger._settings.getLogstashUDPConfig).to.be.a('function');
});

it('creates the default transports defined by config.transports', () => {
Expand All @@ -104,45 +100,15 @@ describe('Logger', () => {
});
});

it('calls a custom get config function with a clone of the winston data', () => {
let winstonConfig;
const loggerConfig = {
winston: {
console: {}
},
getConsoleConfig: (_config) => {
winstonConfig = _config;
return winstonConfig;
}
};
new Logger(loggerConfig).get('LABEL');
expect(loggerConfig.winston).to.not.equal(winstonConfig);
expect(_.set(loggerConfig.winston, 'console.label', 'LABEL')).to.deep.equal(winstonConfig);
});

it('calls a custom get config function for the console transport', () => {
new Logger({
getConsoleConfig: () => {
return {
foo: 'bar'
};
}
}).get('TEST');
expect(winston.transports.Console).to.have.been.calledWith({
foo: 'bar'
});
});

it('correctly sets the log-levels', () => {
new Logger().get('TEST');
expect(winstonLogger.setLevels).to.have.been.calledWith(levels);
});

it('allows modifying the transports configuration', () => {
new Logger({
transports: ['Console', 'LogstashUDP'],
winston: {
logstashUDP: {
transports: {
LogstashUDP: {
bar: 'foo',
level: 'verbose'
}
Expand All @@ -159,4 +125,22 @@ describe('Logger', () => {
label: 'TEST'
});
});

it('allows removal of default Console transport', () => {
new Logger({
transports: {
Console: null,
LogstashUDP: {
bar: 'foo',
level: 'verbose'
}
}
}).get('TEST');
expect(winston.transports.Console).to.not.have.been.called;
expect(winston.transports.LogstashUDP).to.have.been.calledWith({
bar: 'foo',
level: 'verbose',
label: 'TEST'
});
});
});

0 comments on commit 960e810

Please sign in to comment.