Skip to content

Commit

Permalink
Moves initialization of console transport to Space.Module, adds addit…
Browse files Browse the repository at this point in the history
…ional helper methods to WinstonAdapter
  • Loading branch information
qejk committed Apr 28, 2016
1 parent 971bd8a commit fa22f9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
8 changes: 5 additions & 3 deletions source/server/module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const winston = Npm.require('winston');

Space.Module.define('Space.logging.Winston', {

dependencies: {
Expand All @@ -12,7 +14,7 @@ Space.Module.define('Space.logging.Winston', {
transports = [this._setupWinstonConsoleTransport()]
}

const adapter = new Space.Logger.WinstonAdapter(transports);
const adapter = new Space.Logger.WinstonAdapter(winston, transports);
this.injector.map('Space.Logger.WinstonAdapter').toStaticValue(adapter);
log.addAdapter('winston', adapter);
},
Expand All @@ -23,6 +25,6 @@ Space.Module.define('Space.logging.Winston', {
prettyPrint: true,
level: 'info'
};
return Space.Logger.WinstonAdapter.console(options);
return new winston.transports.Console(options);
}
});
});
38 changes: 21 additions & 17 deletions source/server/winston-adapter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const winston = Npm.require('winston');

const WinstonAdapter = Space.Logger.Adapter.extend('Space.Logger.WinstonAdapter', {

Constructor(transports) {
Constructor(winston, transports) {
if (!winston) {
throw new Error(this.ERRORS.winstonMissing)
}
const lib = new winston.Logger({
transports: transports || []
});
Expand All @@ -18,29 +19,32 @@ const WinstonAdapter = Space.Logger.Adapter.extend('Space.Logger.WinstonAdapter'
return this._lib.remove.apply(this._lib, arguments);
},

hasTransport(transportName) {
return this._lib.transports[transportName] !== null;
hasTransport(name) {
return this._lib.transports[name] !== null &&
this._lib.transports[name] !== undefined;
},

transport(name) {
return this._lib.transports[name] || null;
},

transports() {
return this._lib.transports;
},

setLevel(transportName, levelName) {
if (!this.hasTransport(transportName)) {
throw new Error(this.ERRORS.transportNotAdded(transportName));
throw new Error(this.ERRORS.transportNotFound(transportName));
}
this._lib.transports[transportName].level = levelName;
},

ERRORS: {
transportNotAdded(transportName) {
return `Winston transport with ${transportName} is not added`;
winstonMissing() {
return 'Winston library must be provided as first constructor argument';
},
transportNotFound(transportName) {
return `Winston transport with name ${transportName} is not added`;
}
}
});

WinstonAdapter.console = (options) => {
const mergedOptions = _.extend({}, {
colorize: true,
prettyPrint: true,
level: 'info'
}, options);
return new winston.transports.Console(mergedOptions);
};

0 comments on commit fa22f9f

Please sign in to comment.