@knowark/loggarkjs 0.1.0
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/LICENSE +21 -0
- package/Makefile +20 -0
- package/README.md +2 -0
- package/lib/index.js +1 -0
- package/lib/logger.js +26 -0
- package/lib/logger.test.js +69 -0
- package/package.json +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Knowark
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Makefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
help:
|
|
2
|
+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
|
|
3
|
+
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
|
4
|
+
|
|
5
|
+
install: ## Install the project's dependencies
|
|
6
|
+
npm install
|
|
7
|
+
|
|
8
|
+
test: ## Run the project's tests
|
|
9
|
+
npm run test
|
|
10
|
+
|
|
11
|
+
standard: ## Format the project's source code with StandardJS
|
|
12
|
+
npx standard --fix
|
|
13
|
+
|
|
14
|
+
push: ## Push git repository with its tags
|
|
15
|
+
git push && git push --tags
|
|
16
|
+
|
|
17
|
+
gitmessage: ## Add .gitmessage file as git commit template
|
|
18
|
+
touch .gitmessage
|
|
19
|
+
echo "\n# commit message\n.gitmessage" >> .gitignore
|
|
20
|
+
git config commit.template .gitmessage
|
package/README.md
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Logger } from './logger.js'
|
package/lib/logger.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class Logger {
|
|
2
|
+
constructor ({ namespace = '', global = globalThis } = {}) {
|
|
3
|
+
const levels = ['error', 'warn', 'info', 'debug']
|
|
4
|
+
const logvar = [namespace, 'LOGLEVEL'].filter(
|
|
5
|
+
Boolean).join('_').toUpperCase().replaceAll(' ', '_')
|
|
6
|
+
const loglevel = String(global[logvar]).toLowerCase()
|
|
7
|
+
this.global = global
|
|
8
|
+
this.logindex = levels.indexOf(loglevel)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
error (...args) {
|
|
12
|
+
if (this.logindex >= 0) this.global.console.error('[ERROR]', ...args)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
warn (...args) {
|
|
16
|
+
if (this.logindex >= 1) this.global.console.warn('[WARN]', ...args)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
info (...args) {
|
|
20
|
+
if (this.logindex >= 2) this.global.console.info('[INFO]', ...args)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
debug (...args) {
|
|
24
|
+
if (this.logindex >= 3) this.global.console.debug('[DEBUG]', ...args)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it, beforeEach } from '@jest/globals'
|
|
2
|
+
import { Logger } from './logger.js'
|
|
3
|
+
|
|
4
|
+
const mockGlobal = {
|
|
5
|
+
console: {
|
|
6
|
+
error (...args) { mockGlobal.erroArgs = args },
|
|
7
|
+
warn (...args) { mockGlobal.warnArgs = args },
|
|
8
|
+
info (...args) { mockGlobal.infoArgs = args },
|
|
9
|
+
debug (...args) { mockGlobal.debugArgs = args }
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('Logger', () => {
|
|
14
|
+
let mockGlobal = null
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mockGlobal = {
|
|
18
|
+
console: {
|
|
19
|
+
error (...args) { mockGlobal.errorArgs = args },
|
|
20
|
+
warn (...args) { mockGlobal.warnArgs = args },
|
|
21
|
+
info (...args) { mockGlobal.infoArgs = args },
|
|
22
|
+
debug (...args) { mockGlobal.debugArgs = args }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('can be instantiated', () => {
|
|
28
|
+
const logger = new Logger()
|
|
29
|
+
expect(logger).toBeTruthy()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('can be instantiated', () => {
|
|
33
|
+
const logger = new Logger()
|
|
34
|
+
expect(logger).toBeTruthy()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('is disabled by default', () => {
|
|
38
|
+
const logger = new Logger({ global: mockGlobal })
|
|
39
|
+
|
|
40
|
+
logger.error('Logging something...')
|
|
41
|
+
expect(mockGlobal.errorArgs).toBeFalsy()
|
|
42
|
+
|
|
43
|
+
logger.warn('Logging something...')
|
|
44
|
+
expect(mockGlobal.warnArgs).toBeFalsy()
|
|
45
|
+
|
|
46
|
+
logger.info('Logging something...')
|
|
47
|
+
expect(mockGlobal.infoArgs).toBeFalsy()
|
|
48
|
+
|
|
49
|
+
logger.debug('Logging something...')
|
|
50
|
+
expect(mockGlobal.debugArgs).toBeFalsy()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('adds a prefix to the logging methods according to loglevel', () => {
|
|
54
|
+
mockGlobal.LOGLEVEL = 'debug'
|
|
55
|
+
const logger = new Logger({ global: mockGlobal })
|
|
56
|
+
|
|
57
|
+
logger.error('Logging something...')
|
|
58
|
+
expect(mockGlobal.errorArgs).toEqual(['[ERROR]', 'Logging something...'])
|
|
59
|
+
|
|
60
|
+
logger.warn('Logging something...')
|
|
61
|
+
expect(mockGlobal.warnArgs).toEqual(['[WARN]', 'Logging something...'])
|
|
62
|
+
|
|
63
|
+
logger.info('Logging something...')
|
|
64
|
+
expect(mockGlobal.infoArgs).toEqual(['[INFO]', 'Logging something...'])
|
|
65
|
+
|
|
66
|
+
logger.debug('Logging something...')
|
|
67
|
+
expect(mockGlobal.debugArgs).toEqual(['[DEBUG]', 'Logging something...'])
|
|
68
|
+
})
|
|
69
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@knowark/loggarkjs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Utilitarian Logging Library",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"options": "NODE_OPTIONS='--experimental-vm-modules --no-warnings'",
|
|
9
|
+
"test": "npm run options -- npx jest --coverage"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/knowark/loggarkjs.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"logging",
|
|
17
|
+
"console"
|
|
18
|
+
],
|
|
19
|
+
"author": "Knowark",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/knowark/loggarkjs/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/knowark/loggarkjs#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"jest": "^29.1.1"
|
|
27
|
+
}
|
|
28
|
+
}
|