lib/logger/Slf4j.js   A
last analyzed

Complexity

Total Complexity 31
Complexity/F 1.48

Size

Lines of Code 251
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 31
c 1
b 0
f 0
nc 48
mnd 1
bc 32
fnc 21
dl 0
loc 251
rs 9.8
bpm 1.5238
cpm 1.4761
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Object.forEach 0 3 1
A Slf4j.Context 0 19 1
B Slf4j.js ➔ Slf4j 0 127 3
A Slf4j.factory 0 14 3
1
/**
2
 * @module logger/slf4j
3
 */
4
5
var Context = require('./_common').Context
6
var Renderer = require('./Renderer').Renderer
7
var Level = require('./Level').Level
8
9
/**
10
 * @class Slf4j
11
 *
12
 * @param {string} [name] Logger name.
13
 * @param {Context} [context] Logger context.
14
 *
15
 * @implements IVarArgLogger
16
 */
17
function Slf4j (name, context) {
18
  if (!context) {
19
    context = DefaultContext
20
  }
21
  // backward compatibility for 0.1.x/0.2.x
22
  if (!(context instanceof Context)) {
23
    context = new Slf4j.Context(context, arguments[2])
24
  }
25
  var mdc = {}
26
  var self = this
27
28
  function render (pattern, substitutions) {
29
    var chunks = pattern.split('{}')
30
    return substitutions.reduce(function (carrier, entry) {
31
      if (chunks.length > 0) {
32
        return carrier + Renderer.any(entry, false) + chunks.shift()
33
      } else {
34
        return carrier + '\r\n' + Renderer.any(entry, true)
35
      }
36
    }, chunks.shift())
37
  }
38
39
  function log (level, pattern) {
40
    level = Level.find(level)
41
    if (level.weight < context.getLevel(name).weight) {
42
      return
43
    }
44
    var prefix = '[' + level.id.toUpperCase() + ']'
45
    if (name) {
46
      prefix += ' ' + name
47
    }
48
    var mdcPrefix = Object.keys(mdc).map(function (key) {
49
      return key + '=' + Renderer.any(mdc[key])
50
    }).join(', ')
51
    if (mdcPrefix) {
52
      prefix += ' [' + mdcPrefix + ']'
53
    }
54
    var subs = [].slice.call(arguments, 2)
55
    var writer = context.getWriter(name) || Logger
56
    writer.write(render(prefix + ': ' + pattern, subs))
57
  }
58
59
  // noinspection JSUnusedGlobalSymbols
60
  this.log = log
61
62
  Level.explicit.forEach(function (level) {
63
    self[level.id.toLowerCase()] = function () {
64
      log.apply(null, [level].concat([].slice.call(arguments)))
65
    }
66
  })
67
68
  this.getName = function () { return name }
69
70
  this.setWriter = function (writer) {
71
    context.setWriter(name, writer)
72
    return this
73
  }
74
75
  this.getWriter = function () { return context.getWriter(name) }
76
77
  this.setLevel = function (level) {
78
    context.setLevel(name, level)
79
    return this
80
  }
81
82
  this.getLevel = function () { return context.getLevel(name) }
83
84
  /**
85
   * @deprecated
86
   */
87
  this.setThreshold = this.setLevel
88
89
  /**
90
   * @deprecated
91
   */
92
  this.getThreshold = this.getLevel
93
94
  this.getContext = function () { return context }
95
96
  this.attach = function (name, value) { mdc[name] = value }
97
98
  this.detach = function (name) { delete mdc[name] }
99
100
  this.attachAll = function (values) { mdc = values }
101
102
  this.detachAll = function () {
103
    var b = mdc
104
    mdc = {}
105
    return b
106
  }
107
108
  /**
109
   * @function Slf4j#trace
110
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
111
   * @param {...object} parameters List of pattern parameters
112
   */
113
114
  /**
115
   * @function Slf4j#debug
116
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
117
   * @param {...object} parameters List of pattern parameters
118
   */
119
120
  /**
121
   * @function Slf4j#notice
122
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
123
   * @param {...object} parameters List of pattern parameters
124
   */
125
126
  /**
127
   * @function Slf4j#info
128
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
129
   * @param {...object} parameters List of pattern parameters
130
   */
131
132
  /**
133
   * @function Slf4j#warn
134
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
135
   * @param {...object} parameters List of pattern parameters
136
   */
137
138
  /**
139
   * @function Slf4j#error
140
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
141
   * @param {...object} parameters List of pattern parameters
142
   */
143
}
144
145
/**
146
 * @class
147
 * @extends {Context.<Slf4j>}
148
 */
149
Slf4j.Context = function () {
150
  Context.apply(this, arguments)
151
  var self = this
152
  /**
153
   * @param {string} name
154
   * @param {Level} [threshold]
155
   * @param {IWritable} [writer]
156
   * @return {Slf4j}
157
   */
158
  this.create = function (name, threshold, writer) {
159
    if (threshold) {
160
      self.setLevel(name, threshold)
161
    }
162
    if (writer) {
163
      self.setWriter(name, writer)
164
    }
165
    return new Slf4j(name, self)
166
  }
167
}
168
Slf4j.Context.prototype = Object.create(Context.prototype)
169
Slf4j.Context.constructor = Slf4j.Context
170
171
var DefaultContext = new Slf4j.Context()
172
173
/**
174
 * @function Slf4j.create
175
 * @param {string} name
176
 * @param {Level} [level]
177
 * @param {IWritable} [writer]
178
 * @return {Slf4j}
179
 */
180
181
/**
182
 * @function Slf4j.getWriter
183
 * @param {string} name
184
 * @return {IWritable}
185
 */
186
187
/**
188
 * @function Slf4j.setWriter
189
 * @param {string} name
190
 * @param {IWritable} writer
191
 * @return {ILoggerContext.<Slf4j>}
192
 */
193
194
/**
195
 * @function Slf4j.getLevel
196
 * @param {string} name
197
 * @return {Level}
198
 */
199
200
/**
201
 * @function Slf4j.setLevel
202
 * @param {string} name
203
 * @param {Level} level
204
 * @return {ILoggerContext.<Slf4j>}
205
 */
206
207
/**
208
 * @deprecated
209
 * @function Slf4j.getThreshold
210
 * @param {string} name
211
 * @return {Level}
212
 */
213
214
/**
215
 * @deprecated
216
 * @function Slf4j.setThreshold
217
 * @param {string} name
218
 * @param {Level} level
219
 * @return {ILoggerContext.<Slf4j>}
220
 */
221
Object.keys(DefaultContext).forEach(function (method) {
222
  Slf4j[method] = DefaultContext[method].bind(DefaultContext)
223
})
224
225
/**
226
 * @param {LoggerOptions} [options]
227
 * @param {String} [name] Default name to use in case options don't include it
228
 * @param {Context} [context] Default context
229
 * @param {Level} [level] Default logging level
230
 * @return {IVarArgLogger}
231
 */
232
Slf4j.factory = function (options, name, context, level) {
233
  options = options || {}
234
  if (typeof options.log === 'function') {
235
    // then instance of logger have been passed
236
    return options
237
  }
238
  if (options.instance) {
239
    return options.instance
240
  }
241
  context = options.context || context || Slf4j
242
  var logger = context.create(options.name || name, level || options.level)
243
  logger.attachAll(options.mdc || {})
244
  return logger
245
}
246
247
Slf4j.Level = Level
248
249
module.exports = {
250
  Slf4j: Slf4j,
251
  /** @deprecated */
252
  Context: Slf4j.Context,
253
  /** @deprecated use Slf4j.Level */
254
  Level: Level
255
}
256