@knowark/loggarkjs 0.3.5 → 0.3.7

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.
package/lib/index.d.ts CHANGED
@@ -9,6 +9,8 @@ export declare class Logger {
9
9
 
10
10
  context?: object | null
11
11
 
12
+ logindex: number
13
+
12
14
  log(...data: any[]): void
13
15
 
14
16
  error(...data: any[]): void
@@ -19,3 +21,15 @@ export declare class Logger {
19
21
 
20
22
  debug(...data: any[]): void
21
23
  }
24
+
25
+ export declare class Translator {
26
+ static translate(key: string, options?: object): string
27
+
28
+ constructor (options?: object)
29
+
30
+ translate(key: string, options?: object): string
31
+
32
+ t(key: string, options?: object): string
33
+ }
34
+
35
+ export declare function t(key: string, options?: object): string
package/lib/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export { Logger } from './logger.js'
2
+ export { Translator, t } from './translator.js'
package/lib/logger.js CHANGED
@@ -6,12 +6,10 @@ export class Logger {
6
6
  #context = {}
7
7
 
8
8
  constructor ({ namespace = '', context = null, global = globalThis } = {}) {
9
- const levels = ['error', 'warn', 'info', 'debug']
10
- const logvar = [namespace, 'LOGLEVEL'].filter(
9
+ this.levels = ['error', 'warn', 'info', 'debug']
10
+ this.logvar = [namespace, 'LOGLEVEL'].filter(
11
11
  Boolean).join('_').toUpperCase().replaceAll(' ', '_')
12
- const loglevel = String(global[logvar]).toLowerCase()
13
12
  this.global = global
14
- this.logindex = levels.indexOf(loglevel)
15
13
  this.#context = context
16
14
  }
17
15
 
@@ -20,6 +18,11 @@ export class Logger {
20
18
  return !(context || this.#context) ? null : { ...context, ...this.#context }
21
19
  }
22
20
 
21
+ get logindex () {
22
+ const loglevel = String(this.global[this.logvar]).toLowerCase()
23
+ return this.levels.indexOf(loglevel)
24
+ }
25
+
23
26
  log (...args) {
24
27
  if (this.logindex >= 0) log(this.global, 'log', this.context, args)
25
28
  }
@@ -1,7 +1,6 @@
1
- import { test, expect } from '@jest/globals'
1
+ import { it, expect } from '@jest/globals'
2
2
  import { Logger } from './logger.js'
3
3
 
4
-
5
4
  function setup () {
6
5
  const mockGlobal = {
7
6
  console: {
@@ -15,17 +14,17 @@ function setup () {
15
14
  return {mockGlobal}
16
15
  }
17
16
 
18
- test('can be instantiated', () => {
17
+ it('can be instantiated', () => {
19
18
  const logger = new Logger()
20
19
  expect(logger).toBeTruthy()
21
20
  })
22
21
 
23
- test('can be instantiated', () => {
22
+ it('can be instantiated', () => {
24
23
  const logger = new Logger()
25
24
  expect(logger).toBeTruthy()
26
25
  })
27
26
 
28
- test('is disabled by default', () => {
27
+ it('is disabled by default', () => {
29
28
  const { mockGlobal } = setup()
30
29
  const logger = new Logger({ global: mockGlobal })
31
30
 
@@ -45,7 +44,7 @@ test('is disabled by default', () => {
45
44
  expect(mockGlobal.debugArgs).toBeFalsy()
46
45
  })
47
46
 
48
- test('adds a prefix to the logging methods according to loglevel', () => {
47
+ it('adds a prefix to the logging methods according to loglevel', () => {
49
48
  const { mockGlobal } = setup()
50
49
  mockGlobal.LOGLEVEL = 'debug'
51
50
  const logger = new Logger({ global: mockGlobal })
@@ -66,7 +65,7 @@ test('adds a prefix to the logging methods according to loglevel', () => {
66
65
  expect(mockGlobal.debugArgs).toEqual(['[DEBUG]', 'Logging something...'])
67
66
  })
68
67
 
69
- test('adds the provided logging context labels to its final output', () => {
68
+ it('adds the provided logging context labels to its final output', () => {
70
69
  const { mockGlobal } = setup()
71
70
  mockGlobal.LOGLEVEL = 'debug'
72
71
  const context = { correlationId: 'ABCD1234', interactor: 'Informer' }
@@ -103,7 +102,7 @@ test('adds the provided logging context labels to its final output', () => {
103
102
  'Logging something...'])
104
103
  })
105
104
 
106
- test('supports including static context labels', () => {
105
+ it('supports including static context labels', () => {
107
106
  Logger.context = () => {
108
107
  return { correlationId: 'ABCD1234' }
109
108
  }
@@ -0,0 +1,22 @@
1
+ export class Translator {
2
+ static translate (key, _options) {
3
+ return key
4
+ }
5
+
6
+ #options = {}
7
+
8
+ constructor (options = {}) {
9
+ this.#options = options
10
+ }
11
+
12
+ translate (key, options) {
13
+ return this.constructor.translate(
14
+ key, { ...this.#options, ...options })
15
+ }
16
+
17
+ get t () {
18
+ return this.translate.bind(this)
19
+ }
20
+ }
21
+
22
+ export const t = (key, options) => Translator.translate(key, options)
@@ -0,0 +1,35 @@
1
+ import { it, expect } from '@jest/globals'
2
+ import { Translator, t } from './translator.js'
3
+
4
+ it('can be instantiated', () => {
5
+ const translator = new Translator()
6
+ expect(translator).toBeTruthy()
7
+ })
8
+
9
+ it('defines a translate function with options', () => {
10
+ const translator = new Translator()
11
+
12
+ const result = translator.translate('any.key')
13
+
14
+ expect(result).toBe('any.key')
15
+ })
16
+
17
+ it('defines a t shortcut for function translate', () => {
18
+ const t = new Translator().t
19
+
20
+ const result = t('any.key')
21
+
22
+ expect(result).toBe('any.key')
23
+ })
24
+
25
+ it('defines a global t shortcut translate', () => {
26
+ const originalTranslate = Translator.translate
27
+ Translator.translate = (key, _options) => {
28
+ return `translated:${key}`
29
+ }
30
+
31
+ const result = t('any.key')
32
+
33
+ expect(result).toBe('translated:any.key')
34
+ Translator.translate = originalTranslate
35
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowark/loggarkjs",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "description": "Utilitarian Logging Library",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",