-
Notifications
You must be signed in to change notification settings - Fork 6
/
LoggerFactory.coffee
executable file
·50 lines (40 loc) · 1.91 KB
/
LoggerFactory.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@practical ?= {}
class practical.LoggerFactory
instance = null
@get: ->
instance ?= new practical.LoggerFactory()
# The 'global' namespace is checked first, in order to allow people to enforce
# a loglevel across the board.
_getSettingsLoglevel: (namespace = '', defaultLevel = 'info')->
expect(namespace).to.be.a('string')
expect(defaultLevel).to.be.a('string').that.has.length.above(0)
globalLevel = @_getNamespaceLoglevel('global')
return globalLevel if globalLevel?
level = @_getNamespaceLoglevel(namespace) if namespace.length > 0
level ?= @_getNamespaceLoglevel('default')
level ?= defaultLevel
# @returns Meteor.settings.loglevel.namespace server side
# or if called client side or it doesn't exist server side,
# Meteor.settings.public.loglevel.namespace.
# This allows to set only public loglevel for both client and server side.
_getNamespaceLoglevel: (namespace)->
expect(namespace).to.be.a('string').that.has.length.above(0)
level = Meteor.settings?.public?.loglevel?[namespace]
if Meteor.isServer
serverLevel = Meteor.settings?.loglevel?[namespace]
level = serverLevel if serverLevel?
return level
createLogger: (namespace = '', defaultLevel = 'info')->
log.debug 'LoggerFactory.createLogger()', arguments
expect(namespace).to.be.a('string')
expect(defaultLevel).to.be.a('string').that.has.length.above(0)
expect(Loglevel).to.be.a 'function'
options = {}
options.prefix = namespace + ':' if namespace.length > 0
options.level = @_getSettingsLoglevel(namespace, defaultLevel)
return Loglevel(options)
createPackageLogger: (packageName, defaultLevel = 'info')->
return @createLogger(packageName, defaultLevel)
createAppLogger: (appName = 'app', defaultLevel = 'info')->
return @createLogger(appName, defaultLevel)
loglevel = practical.LoggerFactory.get()