Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9b715a0
Refactors Space.Logger class to use arguments on Constructor, removes…
qejk Apr 19, 2016
03aef7d
Refactors Space.Module to: not use global Space.log, instantiate unde…
qejk Apr 19, 2016
dd32c4e
Moves temporarily Space.Logger code to coffeescript, adds support for…
qejk Apr 24, 2016
193f43b
Refactors code: moves methods to base Space.Logger.Addapter class, ad…
qejk Apr 24, 2016
c71872c
Adds default configuration from ENV to module configuration, sets min…
qejk Apr 24, 2016
a778812
Refactors
qejk Apr 24, 2016
c8ea9ea
Removes blank lines
qejk Apr 26, 2016
8942054
Moves instantiation of console transport to Space.Logger.WinstonAdapter
qejk Apr 26, 2016
64f90e0
Simplifies initialization of transports, adds dedicated method for pr…
qejk Apr 26, 2016
462bfef
Removes winston npm dependency
qejk Apr 26, 2016
dc4914f
Simplifies logging initialization by moving it to separate modules co…
qejk Apr 26, 2016
a48baa3
Moves coffeescript to ES6
qejk Apr 26, 2016
0cd08c6
Removes unnecessary variable
qejk May 7, 2016
e5a071a
Adds state related additional helper methods to Space.Logger
qejk May 7, 2016
e06d264
Changes injector mapping type
qejk May 7, 2016
37cafb0
Fixes module unit tests
qejk May 7, 2016
319b591
Refactors Space.Logger unit tests
qejk May 7, 2016
fa9c2dc
One semicolon here, one semicolon there...
qejk May 7, 2016
e2d7773
Merge branch 'develop' of https://github.com/meteor-space/base into f…
qejk Jan 7, 2017
d886a5b
Removes back winston dependency from meteor
qejk Jan 7, 2017
cbb3ca5
Refactors Logger
qejk Jan 7, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 4 additions & 36 deletions source/logger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
let config = Space.configuration;

if (Meteor.isServer) {
winston = Npm.require('winston');
}

Space.Object.extend(Space, 'Logger', {

_logger: null,
Expand All @@ -18,31 +12,12 @@ Space.Object.extend(Space, 'Logger', {
'debug': 7
},

Constructor() {
if (Meteor.isServer) {
this._logger = new winston.Logger({
transports: [
new winston.transports.Console({
colorize: true,
prettyPrint: true
})
]
});
this._logger.setLevels(winston.config.syslog.levels);
}
if (Meteor.isClient) {
this._logger = console;
}
Constructor(logger) {
this._logger = logger;
},

setMinLevel(name) {
let newCode = this._levelCode(name);
if (this._minLevel !== newCode) {
this._minLevel = newCode;
if (Meteor.isServer) {
this._logger.transports.console.level = name;
}
}
addTransport() {
this._logger.add.apply(this._logger, arguments);
},

start() {
Expand Down Expand Up @@ -95,10 +70,3 @@ Space.Object.extend(Space, 'Logger', {
}

});

Space.log = new Space.Logger();

if (config.log.enabled) {
Space.log.setMinLevel(config.log.minLevel);
Space.log.start();
}
57 changes: 49 additions & 8 deletions source/module.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class Space.Module extends Space.Object
return if not @is('constructed') # only initialize once
if not @injector? then throw new Error @ERRORS.injectorMissing
@_state = 'configuring'
Space.log.debug("#{@constructor.publishedAs}: initialize")
unless isSubModule
@log = @_setupLogger()
else
@log = @injector.get('log')
@log.debug("#{@constructor.publishedAs}: initialize")

# Setup basic mappings required by all modules if this the top-level module
unless isSubModule
@injector.map('Injector').to @injector
Expand Down Expand Up @@ -114,7 +119,7 @@ class Space.Module extends Space.Object
# calling the instance hooks before, on, and after
_runLifeCycleAction: (action, func) ->
@_invokeActionOnRequiredModules action
Space.log.debug("#{@constructor.publishedAs}: #{action}")
@log.debug("#{@constructor.publishedAs}: #{action}")
this["before#{Space.capitalizeString(action)}"]?()
func?()
this["on#{Space.capitalizeString(action)}"]?()
Expand All @@ -125,7 +130,7 @@ class Space.Module extends Space.Object
@_invokeActionOnRequiredModules '_runOnInitializeHooks'
# Never run this hook twice
if @is('configuring')
Space.log.debug("#{@constructor.publishedAs}: onInitialize")
@log.debug("#{@constructor.publishedAs}: onInitialize")
@_state = 'initializing'
# Inject required dependencies into this module
@injector.injectInto this
Expand All @@ -135,15 +140,15 @@ class Space.Module extends Space.Object
_autoMapSingletons: ->
@_invokeActionOnRequiredModules '_autoMapSingletons'
if @is('initializing')
Space.log.debug("#{@constructor.publishedAs}: _autoMapSingletons")
@log.debug("#{@constructor.publishedAs}: _autoMapSingletons")
@_state = 'auto-mapping-singletons'
# Map classes that are declared as singletons
@injector.map(singleton).asSingleton() for singleton in @singletons

_autoCreateSingletons: ->
@_invokeActionOnRequiredModules '_autoCreateSingletons'
if @is('auto-mapping-singletons')
Space.log.debug("#{@constructor.publishedAs}: _autoCreateSingletons")
@log.debug("#{@constructor.publishedAs}: _autoCreateSingletons")
@_state = 'auto-creating-singletons'
# Create singleton classes
@injector.create(singleton) for singleton in @singletons
Expand All @@ -153,7 +158,7 @@ class Space.Module extends Space.Object
@_invokeActionOnRequiredModules '_runAfterInitializeHooks'
# Never run this hook twice
if @is('auto-creating-singletons')
Space.log.debug("#{@constructor.publishedAs}: afterInitialize")
@log.debug("#{@constructor.publishedAs}: afterInitialize")
@_state = 'initialized'
# Call custom lifecycle hook if existant
@afterInitialize?()
Expand All @@ -165,11 +170,47 @@ class Space.Module extends Space.Object
this[hook] ?= ->
this[hook] = _.wrap(this[hook], wrapper)

_setupLogger: ->
config = Space.configuration.log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can do away with the Space.configuration as that was only needed since we were previously initialising the logger instance outside the module chain. This will be really nice to see gone


if Meteor.isServer
type = 'winston'
lib = @_setupWinstonLogger(config)
if Meteor.isClient
type = 'console'
lib = console

logger = new Space.Logger(lib)
@_setupLoggerTransports(type, logger)
logger.start() if config.enabled == true
return logger

_setupWinstonLogger: (config) ->
winston = Npm.require('winston')
options =
colorize: true
prettyPrint: true
options.level = config.minLevel if config.minLevel?

lib = new (winston.Logger)(transports: [
new (winston.transports.Console)(options)
])
lib.setLevels winston.config.syslog.levels
return lib

_setupLoggerTransports: (type, logger) ->
config = @constructor.prototype.configuration
transports = config.log?[type]?.transports
return unless transports

for transport in transports
logger.addTransport.apply(logger, transport)

_mapSpaceServices: ->
@injector.map('log').to Space.log
@injector.map('log').toStaticValue(@log)

_mapMeteorApis: ->
Space.log.debug("#{@constructor.publishedAs}: _mapMeteorApis")
@log.debug("#{@constructor.publishedAs}: _mapMeteorApis")
# Map Meteor standard packages
@injector.map('Meteor').to Meteor
if Package.ejson?
Expand Down