debug-logfmt 1.4.6 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +31 -16
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "debug-logfmt",
3
3
  "description": "debug module using logfmt format",
4
4
  "homepage": "https://github.com/Kikobeats/debug-logfmt",
5
- "version": "1.4.6",
5
+ "version": "1.4.8",
6
6
  "types": "./src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "exports": {
package/src/index.js CHANGED
@@ -27,36 +27,51 @@ const createDebug = require('./lazy')(origDebug)
27
27
 
28
28
  const LEVELS = ['info', 'warn', 'error']
29
29
 
30
- const createLogger =
31
- log =>
32
- (...args) =>
33
- log(
34
- args.reduce((result, arg, index) => {
35
- const encoded = typeof arg === 'string' ? arg : encode(arg)
36
- if (!encoded) return result
37
- return result + (index > 0 ? ' ' : '') + encoded
38
- }, '')
39
- )
30
+ const createLogger = log => {
31
+ const logger = (...args) => {
32
+ if (!log.enabled) return
33
+
34
+ log(
35
+ args.reduce((result, arg, index) => {
36
+ const encoded = typeof arg === 'string' ? arg : encode(arg)
37
+ if (!encoded) return result
38
+ return result + (index > 0 ? ' ' : '') + encoded
39
+ }, '')
40
+ )
41
+ }
42
+
43
+ Object.defineProperty(logger, 'enabled', {
44
+ enumerable: true,
45
+ get: () => log.enabled
46
+ })
47
+
48
+ return logger
49
+ }
40
50
 
41
51
  module.exports = (env, { levels = LEVELS } = {}) => {
42
52
  const debug = createLogger(createDebug(env))
43
53
  levels.forEach(level => (debug[level] = createLogger(createDebug(`${env}:${level}`))))
44
54
 
45
55
  debug.duration = (...args) => {
46
- const duration = timeSpan()
56
+ let duration = debug.enabled ? timeSpan() : undefined
47
57
 
48
58
  const create =
49
- type =>
59
+ logger =>
50
60
  (...opts) => {
51
- ;(type ? debug[type] : debug)(...args, ...opts, {
61
+ if (!logger.enabled) return true
62
+
63
+ duration = duration || timeSpan()
64
+
65
+ logger(...args, ...opts, {
52
66
  duration: duration()
53
67
  })
68
+
54
69
  return true
55
70
  }
56
71
 
57
- const fn = create()
58
- fn.error = create('error')
59
- fn.info = create('info')
72
+ const fn = create(debug)
73
+ fn.error = create(debug.error)
74
+ fn.info = create(debug.info)
60
75
 
61
76
  return fn
62
77
  }