@knowark/loggarkjs 0.2.0 → 0.3.1

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.
@@ -0,0 +1,7 @@
1
+ {
2
+ "workbench.colorCustomizations": {
3
+ "activityBar.background": "#2C2D34",
4
+ "titleBar.activeBackground": "#3E3F49",
5
+ "titleBar.activeForeground": "#FAFAFB"
6
+ }
7
+ }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export declare class Logger {
2
2
  constructor (dependencies?: {
3
3
  namespace?: string,
4
+ labels?: object,
4
5
  global?: object
5
6
  })
6
7
 
package/lib/logger.js CHANGED
@@ -1,30 +1,37 @@
1
1
  export class Logger {
2
- constructor ({ namespace = '', global = globalThis } = {}) {
2
+ constructor ({ namespace = '', labels = null, global = globalThis } = {}) {
3
3
  const levels = ['error', 'warn', 'info', 'debug']
4
4
  const logvar = [namespace, 'LOGLEVEL'].filter(
5
5
  Boolean).join('_').toUpperCase().replaceAll(' ', '_')
6
6
  const loglevel = String(global[logvar]).toLowerCase()
7
7
  this.global = global
8
8
  this.logindex = levels.indexOf(loglevel)
9
+ this.labels = labels
9
10
  }
10
11
 
11
12
  log (...args) {
12
- if (this.logindex >= 0) this.global.console.log('[LOG]', ...args)
13
+ if (this.logindex >= 0) log(this.global, 'log', this.labels, args)
13
14
  }
14
15
 
15
16
  error (...args) {
16
- if (this.logindex >= 0) this.global.console.error('[ERROR]', ...args)
17
+ if (this.logindex >= 0) log(this.global, 'error', this.labels, args)
17
18
  }
18
19
 
19
20
  warn (...args) {
20
- if (this.logindex >= 1) this.global.console.warn('[WARN]', ...args)
21
+ if (this.logindex >= 1) log(this.global, 'warn', this.labels, args)
21
22
  }
22
23
 
23
24
  info (...args) {
24
- if (this.logindex >= 2) this.global.console.info('[INFO]', ...args)
25
+ if (this.logindex >= 2) log(this.global, 'info', this.labels, args)
25
26
  }
26
27
 
27
28
  debug (...args) {
28
- if (this.logindex >= 3) this.global.console.debug('[DEBUG]', ...args)
29
+ if (this.logindex >= 3) log(this.global, 'debug', this.labels, args)
29
30
  }
30
31
  }
32
+
33
+ function log (global, level, labels, args) {
34
+ return global.console[level](
35
+ `[${level.toUpperCase()}]`,
36
+ ...[labels ? JSON.stringify(labels) : false, ...args].filter(Boolean))
37
+ }
@@ -64,4 +64,41 @@ describe('Logger', () => {
64
64
  logger.debug('Logging something...')
65
65
  expect(mockGlobal.debugArgs).toEqual(['[DEBUG]', 'Logging something...'])
66
66
  })
67
+
68
+ it('adds the provided logging labels to its final output', () => {
69
+ mockGlobal.LOGLEVEL = 'debug'
70
+ const labels = { correlationId: 'ABCD1234' }
71
+ const logger = new Logger({ global: mockGlobal, labels })
72
+
73
+ logger.log('Logging something...')
74
+ expect(mockGlobal.logArgs).toEqual([
75
+ '[LOG]',
76
+ JSON.stringify({ correlationId: 'ABCD1234' }),
77
+ 'Logging something...'])
78
+
79
+ logger.labels.interactor = 'Informer'
80
+ logger.error('Logging something...')
81
+ expect(mockGlobal.errorArgs).toEqual([
82
+ '[ERROR]',
83
+ JSON.stringify({ correlationId: 'ABCD1234', interactor: 'Informer' }),
84
+ 'Logging something...'])
85
+
86
+ logger.warn('Logging something...')
87
+ expect(mockGlobal.warnArgs).toEqual([
88
+ '[WARN]',
89
+ JSON.stringify({ correlationId: 'ABCD1234', interactor: 'Informer' }),
90
+ 'Logging something...'])
91
+
92
+ logger.info('Logging something...')
93
+ expect(mockGlobal.infoArgs).toEqual([
94
+ '[INFO]',
95
+ JSON.stringify({ correlationId: 'ABCD1234', interactor: 'Informer' }),
96
+ 'Logging something...'])
97
+
98
+ logger.debug('Logging something...')
99
+ expect(mockGlobal.debugArgs).toEqual([
100
+ '[DEBUG]',
101
+ JSON.stringify({ correlationId: 'ABCD1234', interactor: 'Informer' }),
102
+ 'Logging something...'])
103
+ })
67
104
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowark/loggarkjs",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Utilitarian Logging Library",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",
@@ -23,6 +23,6 @@
23
23
  },
24
24
  "homepage": "https://github.com/knowark/loggarkjs#readme",
25
25
  "devDependencies": {
26
- "jest": "^29.1.1"
26
+ "jest": "^29.7.0"
27
27
  }
28
28
  }