Completed
Push — dev ( 763fa0...b49d63 )
by Fike
29s
created

Slf4j.js ➔ ... ➔ substitutions.reduce   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 2
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
    context.getWriter(name).write(render(prefix + ': ' + pattern, subs))
56
  }
57
58
  // noinspection JSUnusedGlobalSymbols
59
  this.log = log
60
61
  Level.explicit.forEach(function (level) {
62
    self[level.id.toLowerCase()] = function () {
63
      log.apply(null, [level].concat([].slice.call(arguments)))
64
    }
65
  })
66
67
  this.getName = function () { return name }
68
69
  this.setWriter = function (writer) {
70
    context.setWriter(name, writer)
71
    return this
72
  }
73
74
  this.getWriter = function () { return context.getWriter(name) }
75
76
  this.setLevel = function (level) {
77
    context.setLevel(name, level)
78
    return this
79
  }
80
81
  this.getLevel = function () { return context.getLevel(name) }
82
83
  /**
84
   * @deprecated
85
   */
86
  this.setThreshold = this.setLevel
87
88
  /**
89
   * @deprecated
90
   */
91
  this.getThreshold = this.getLevel
92
93
  this.getContext = function () { return context }
94
95
  this.attach = function (name, value) { mdc[name] = value }
96
97
  this.detach = function (name) { delete mdc[name] }
98
99
  this.attachAll = function (values) { mdc = values }
100
101
  this.detachAll = function () {
102
    var b = mdc
103
    mdc = {}
104
    return b
105
  }
106
107
  /**
108
   * @function Slf4j#trace
109
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
110
   * @param {...object} parameters List of pattern parameters
111
   */
112
113
  /**
114
   * @function Slf4j#debug
115
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
116
   * @param {...object} parameters List of pattern parameters
117
   */
118
119
  /**
120
   * @function Slf4j#notice
121
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
122
   * @param {...object} parameters List of pattern parameters
123
   */
124
125
  /**
126
   * @function Slf4j#info
127
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
128
   * @param {...object} parameters List of pattern parameters
129
   */
130
131
  /**
132
   * @function Slf4j#warn
133
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
134
   * @param {...object} parameters List of pattern parameters
135
   */
136
137
  /**
138
   * @function Slf4j#error
139
   * @param {string} pattern Logging pattern with `{}` as a place for substitution
140
   * @param {...object} parameters List of pattern parameters
141
   */
142
}
143
144
/**
145
 * @class
146
 * @extends {Context.<Slf4j>}
147
 */
148
Slf4j.Context = function () {
149
  Context.apply(this, arguments)
150
  var self = this
151
  /**
152
   * @param {string} name
153
   * @param {Level} [threshold]
154
   * @param {IWritable} [writer]
155
   * @return {Slf4j}
156
   */
157
  this.create = function (name, threshold, writer) {
158
    if (threshold) {
159
      self.setLevel(name, threshold)
160
    }
161
    if (writer) {
162
      self.setWriter(name, writer)
163
    }
164
    return new Slf4j(name, self)
165
  }
166
}
167
Slf4j.Context.prototype = Object.create(Context.prototype)
168
Slf4j.Context.constructor = Slf4j.Context
169
170
var DefaultContext = new Slf4j.Context()
171
172
/**
173
 * @function Slf4j.create
174
 * @param {string} name
175
 * @param {Level} [level]
176
 * @param {IWritable} [writer]
177
 * @return {Slf4j}
178
 */
179
180
/**
181
 * @function Slf4j.getWriter
182
 * @param {string} name
183
 * @return {IWritable}
184
 */
185
186
/**
187
 * @function Slf4j.setWriter
188
 * @param {string} name
189
 * @param {IWritable} writer
190
 * @return {ILoggerContext.<Slf4j>}
191
 */
192
193
/**
194
 * @function Slf4j.getLevel
195
 * @param {string} name
196
 * @return {Level}
197
 */
198
199
/**
200
 * @function Slf4j.setLevel
201
 * @param {string} name
202
 * @param {Level} level
203
 * @return {ILoggerContext.<Slf4j>}
204
 */
205
206
/**
207
 * @deprecated
208
 * @function Slf4j.getThreshold
209
 * @param {string} name
210
 * @return {Level}
211
 */
212
213
/**
214
 * @deprecated
215
 * @function Slf4j.setThreshold
216
 * @param {string} name
217
 * @param {Level} level
218
 * @return {ILoggerContext.<Slf4j>}
219
 */
220
Object.keys(DefaultContext).forEach(function (method) {
221
  Slf4j[method] = DefaultContext[method].bind(DefaultContext)
222
})
223
224
/**
225
 * @param {LoggerOptions} [options]
226
 * @param {String} [name] Default name to use in case options don't include it
227
 * @param {Context} [context] Default context
228
 * @param {Level} [level] Default logging level
229
 * @return {IVarArgLogger}
230
 */
231
Slf4j.factory = function (options, name, context, level) {
232
  options = options || {}
233
  if (typeof options.log === 'function') {
234
    // then instance of logger have been passed
235
    return options
236
  }
237
  if (options.instance) {
238
    return options.instance
239
  }
240
  context = options.context || context || Slf4j
241
  var logger = context.create(options.name || name, level || options.level)
242
  logger.attachAll(options.mdc || {})
243
  return logger
244
}
245
246
Slf4j.Level = Level
247
248
module.exports = {
249
  Slf4j: Slf4j,
250
  /** @deprecated */
251
  Context: Slf4j.Context,
252
  /** @deprecated use Slf4j.Level */
253
  Level: Level
254
}
255