Projekt

Obecné

Profil

Stáhnout (3.42 KB) Statistiky
| Větev: | Revize:
1
var util = require('util')
2
var _ = require('lodash')
3

    
4
var loggerInstance
5

    
6
var defaultProvider = {
7
  log: console.log,
8
  debug: console.log, // use .log(); since console does not have .debug()
9
  info: console.info,
10
  warn: console.warn,
11
  error: console.error
12
}
13

    
14
// log level 'weight'
15
var LEVELS = {
16
  debug: 10,
17
  info: 20,
18
  warn: 30,
19
  error: 50,
20
  silent: 80
21
}
22

    
23
module.exports = {
24
  // singleton
25
  getInstance: function() {
26
    if (!loggerInstance) {
27
      loggerInstance = new Logger()
28
    }
29

    
30
    return loggerInstance
31
  },
32
  getArrow: getArrow
33
}
34

    
35
function Logger() {
36
  var logLevel
37
  var provider
38

    
39
  var api = {
40
    log: log,
41
    debug: debug,
42
    info: info,
43
    warn: warn,
44
    error: error,
45
    setLevel: function(v) {
46
      if (isValidLevel(v)) {
47
        logLevel = v
48
      }
49
    },
50
    setProvider: function(fn) {
51
      if (fn && isValidProvider(fn)) {
52
        provider = fn(defaultProvider)
53
      }
54
    }
55
  }
56

    
57
  init()
58

    
59
  return api
60

    
61
  function init() {
62
    api.setLevel('info')
63
    api.setProvider(function() {
64
      return defaultProvider
65
    })
66
  }
67

    
68
  // log will log messages, regardless of logLevels
69
  function log() {
70
    provider.log(_interpolate.apply(null, arguments))
71
  }
72

    
73
  function debug() {
74
    if (_showLevel('debug')) {
75
      provider.debug(_interpolate.apply(null, arguments))
76
    }
77
  }
78

    
79
  function info() {
80
    if (_showLevel('info')) {
81
      provider.info(_interpolate.apply(null, arguments))
82
    }
83
  }
84

    
85
  function warn() {
86
    if (_showLevel('warn')) {
87
      provider.warn(_interpolate.apply(null, arguments))
88
    }
89
  }
90

    
91
  function error() {
92
    if (_showLevel('error')) {
93
      provider.error(_interpolate.apply(null, arguments))
94
    }
95
  }
96

    
97
  /**
98
   * Decide to log or not to log, based on the log levels 'weight'
99
   * @param  {String}  showLevel [debug, info, warn, error, silent]
100
   * @return {Boolean}
101
   */
102
  function _showLevel(showLevel) {
103
    var result = false
104
    var currentLogLevel = LEVELS[logLevel]
105

    
106
    if (currentLogLevel && currentLogLevel <= LEVELS[showLevel]) {
107
      result = true
108
    }
109

    
110
    return result
111
  }
112

    
113
  // make sure logged messages and its data are return interpolated
114
  // make it possible for additional log data, such date/time or custom prefix.
115
  function _interpolate() {
116
    var fn = _.spread(util.format)
117
    var result = fn(_.slice(arguments))
118

    
119
    return result
120
  }
121

    
122
  function isValidProvider(fnProvider) {
123
    var result = true
124

    
125
    if (fnProvider && !_.isFunction(fnProvider)) {
126
      throw new Error('[HPM] Log provider config error. Expecting a function.')
127
    }
128

    
129
    return result
130
  }
131

    
132
  function isValidLevel(levelName) {
133
    var validLevels = _.keys(LEVELS)
134
    var isValid = _.includes(validLevels, levelName)
135

    
136
    if (!isValid) {
137
      throw new Error('[HPM] Log level error. Invalid logLevel.')
138
    }
139

    
140
    return isValid
141
  }
142
}
143

    
144
/**
145
 * -> normal proxy
146
 * => router
147
 * ~> pathRewrite
148
 * ≈> router + pathRewrite
149
 *
150
 * @param  {String} originalPath
151
 * @param  {String} newPath
152
 * @param  {String} originalTarget
153
 * @param  {String} newTarget
154
 * @return {String}
155
 */
156
function getArrow(originalPath, newPath, originalTarget, newTarget) {
157
  var arrow = ['>']
158
  var isNewTarget = originalTarget !== newTarget // router
159
  var isNewPath = originalPath !== newPath // pathRewrite
160

    
161
  if (isNewPath && !isNewTarget) {
162
    arrow.unshift('~')
163
  } else if (!isNewPath && isNewTarget) {
164
    arrow.unshift('=')
165
  } else if (isNewPath && isNewTarget) {
166
    arrow.unshift('')
167
  } else {
168
    arrow.unshift('-')
169
  }
170

    
171
  return arrow.join('')
172
}
(6-6/8)