Skip to content

Commit

Permalink
Adds basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
qejk committed Apr 26, 2016
1 parent 81c9cdb commit f5fcbff
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ Package.onUse(function(api) {
]);

api.addFiles([
'source/server/module.js',
'source/server/winston-adapter.js'
], 'server');
});
27 changes: 27 additions & 0 deletions source/server/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Space.Module.define('Space.logging.Winston', {

dependencies: {
configuration: 'configuration',
log: 'Space.Logger',
},

onInitialize() {
let log = this.injector.get('log');

let transports = lodash.get(this.configuration, 'log.winston.transports', [
this._setupWinstonConsoleTransport()
])
let adapter = new Space.Logger.WinstonAdapter(transports);
this.injector.map('Space.Logger.WinstonAdapter').toStaticValue(adapter);
log.addAdapter('winston', adapter);
},

_setupWinstonConsoleTransport() {
options = {
colorize: true,
prettyPrint: true,
level: 'info',
}
return Space.Logger.WinstonAdapter.console(options);
}
});
48 changes: 48 additions & 0 deletions source/server/winston-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const winston = Npm.require('winston');

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

Constructor: function(transports) {
let lib = new winston.Logger({
transports: transports || []
});
lib.setLevels(winston.config.syslog.levels);
this.setLib(lib);
},

addTransport: function() {
return this._lib.add.apply(this._lib, arguments);
},

removeTransport: function() {
return this._lib.remove.apply(this._lib, arguments);
},

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

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

ERRORS: {
transportNotAdded: function(transportName) {
return `Winston transport with ${transportName} is not added`;
}
}
});

WinstonAdapter.console = function(options) {
if (options == null) {
let options = {
colorize: true,
prettyPrint: true,
level: 'info'
};
}
return new winston.transports.Console(options);
};

0 comments on commit f5fcbff

Please sign in to comment.