hermodlog 1.0.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 ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) Alex Werner - 2023
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # hermodlog
2
+
3
+ Stupid simple logging for JS but with context for heavy log environments.
4
+
5
+ This is just to only pass along a single logger instance between modules and have a hierarchical way to sort and display such modules.
6
+
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm i hermodlog
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```js
17
+ import Logger from 'hermodlog';
18
+
19
+ logger = new Logger();
20
+ logger.log("Stuff");
21
+
22
+
23
+ contextLogger = logger.context('Node#4');
24
+
25
+ moduleLogger = contextLogger.module('myModule');
26
+ ```
27
+
28
+ ## API
29
+
30
+ ### Logger
31
+
32
+ #### `new Logger(options)`
33
+ Create a new logger instance.
34
+
35
+ ##### `context(name)`
36
+ Create a new context logger with the given name.
37
+
38
+ ##### `module(name)`
39
+ Create a new module logger with the given name.
40
+
41
+ ##### `log(message, level)`
42
+ Log a message with the given level.
43
+
44
+ ##### `debug(message)`
45
+ Log a message with the debug level.
46
+
47
+ ##### `info(message)`
48
+ Log a message with the info level.
49
+
50
+ ##### `warn(message)`
51
+ Log a message with the warn level.
52
+
53
+ ##### `error(message)`
54
+ Log a message with the error level.
55
+
56
+ ##### `fatal(message)`
57
+ Log a message with the fatal level.
58
+
59
+
60
+ ### License
61
+
62
+ MIT
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "hermodlog",
3
+ "version": "1.0.0",
4
+ "description": "Simple contextual logger to simplify log display in heavy load context and provide easy parsing",
5
+ "main": "src/Logger.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "vitest --dir .",
9
+ "test:dev": "vitest --dir . --watch"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/Alex-Werner/hermodlog.git"
14
+ },
15
+ "author": "Alex Werner <@obusco>",
16
+ "license": "MIT",
17
+ "bugs": {
18
+ "url": "https://github.com/Alex-Werner/hermodlog/issues"
19
+ },
20
+ "homepage": "https://github.com/Alex-Werner/hermodlog#readme",
21
+ "dependencies": {
22
+ "chalk": "^5.3.0"
23
+ },
24
+ "devDependencies": {
25
+ "vitest": "^0.34.4"
26
+ }
27
+ }
package/src/Logger.js ADDED
@@ -0,0 +1,146 @@
1
+ import chalk from 'chalk';
2
+ export default class Logger {
3
+ constructor(_date, level = 'info') {
4
+
5
+ this.level = level;
6
+
7
+ this.history = [];
8
+
9
+ this.contextName = null
10
+ this.methodName = null
11
+ this.moduleName = null
12
+ this.listenerName = null
13
+ if(_date){
14
+ this._date = _date;
15
+ }
16
+ }
17
+
18
+ context(_context) {
19
+ const logger = new Logger(this._date);
20
+ logger.contextName = _context;
21
+ logger.methodName = this.methodName;
22
+ logger.moduleName = this.moduleName;
23
+ logger.listenerName = this.listenerName;
24
+
25
+ logger._log = this._log.bind(logger);
26
+ return logger;
27
+ }
28
+ module(_module) {
29
+ const logger = new Logger(this._date);
30
+ logger.methodName = this.methodName;
31
+ logger.moduleName = _module;
32
+ logger.contextName = this.contextName;
33
+ logger.listenerName = this.listenerName;
34
+
35
+ logger._log = this._log.bind(logger);
36
+ return logger;
37
+ }
38
+ listener(_listener) {
39
+ const logger = new Logger(this._date);
40
+ logger.moduleName = this.moduleName;
41
+ logger.contextName = this.contextName;
42
+ logger.listenerName = _listener;
43
+ logger._log = this._log.bind(logger);
44
+ return logger;
45
+ }
46
+ method(_method) {
47
+ const logger = new Logger(this._date);
48
+ logger.moduleName = this.moduleName;
49
+ logger.contextName = this.contextName;
50
+ logger.listenerName = this.listenerName;
51
+ logger.methodName = _method;
52
+ logger._log = this._log.bind(logger);
53
+ return logger;
54
+ }
55
+ _log(message) {
56
+ console.log(message)
57
+ }
58
+
59
+ log(...args) {
60
+ let _message = args[0];
61
+ if(args.length > 1){
62
+ _message = args.slice(0).join(' | ');
63
+ }
64
+ const date = this._date || new Date();
65
+ let message = `[${chalk.gray(date.toISOString())}]`;
66
+
67
+
68
+ if(this.contextName){
69
+ message += ` context: ${chalk.blueBright(this.contextName)} |`;
70
+ }
71
+ if(this.moduleName){
72
+ message += ` module:${chalk.gray(this.moduleName)} |`;
73
+ }
74
+ if(this.listenerName){
75
+ message += ` listener: ${chalk.red(this.listenerName)} |`;
76
+ }
77
+ if(this.methodName){
78
+ message += ` method: ${chalk.yellow(this.methodName)} |`;
79
+ }
80
+ if(message.endsWith(' |')){
81
+ message = message.slice(0, -1);
82
+ }
83
+
84
+ for(let i = 0; i < args.length; i++){
85
+ if(typeof args[i] === 'object'){
86
+ const constructorName = args[i].constructor.name;
87
+ message += ` ${chalk.yellow(`${constructorName}(`)}`
88
+ message += chalk.yellow(JSON.stringify(args[i], null, 2))
89
+ message += ` ${chalk.yellow(")")}`
90
+ }
91
+ if(typeof args[i] === 'string'){
92
+ message += chalk.green(args[i])
93
+ }
94
+ }
95
+
96
+
97
+ // // If message is an object, we put it on a new line
98
+ // if(typeof _message === 'object'){
99
+ // message += ' Object'
100
+ // message += '\n'+JSON.stringify(_message, null, 2)
101
+ // } else{
102
+ // message += ' : '+ _message
103
+ // }
104
+
105
+ this.history.push(message);
106
+ if(this.history.length > 100){
107
+ this.history.shift();
108
+ }
109
+
110
+ this._log(message);
111
+ }
112
+ info(message) {
113
+ this.log(message);
114
+ }
115
+ debug(message) {
116
+ this.log(message);
117
+ }
118
+ trace(message) {
119
+ if(this.level === 'trace')
120
+ {
121
+ this.log(message);
122
+ }
123
+ }
124
+ error(_message) {
125
+ const date = this._date || new Date();
126
+ let message = `[${chalk.gray(date.toISOString())}]`;
127
+
128
+ if(this.moduleName){
129
+ message += ` - m=${chalk.blue(this.moduleName)}`;
130
+ }
131
+ if(this.contextName){
132
+ message += ` - c=${chalk.green(this.contextName)}`;
133
+ }
134
+
135
+ message += ` : ${chalk.red(_message)}`
136
+
137
+ this.history.push(message);
138
+ if(this.history.length > 100){
139
+ this.history.shift();
140
+ }
141
+ this._log(message);
142
+ }
143
+ warn(message) {
144
+ this.log(message);
145
+ }
146
+ };
@@ -0,0 +1,40 @@
1
+ import {assert, describe, expect, it} from 'vitest'
2
+ import Logger from "./Logger.js";
3
+
4
+ describe('Logger', () => {
5
+ let silentLogger
6
+ let silentModuleLogger
7
+ it('should create an logger', () => {
8
+ silentLogger = new Logger(new Date('2023-07-29T01:38:00.482Z'));
9
+ silentLogger._log = () => {}
10
+ assert.equal(silentLogger.history.length, 0)
11
+ silentLogger.log('Hello');
12
+ assert.equal(silentLogger.history.length, 1)
13
+ assert.equal(silentLogger.history[0], '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m]\u001b[32mHello\u001b[39m')
14
+ })
15
+ it('should create an logger with module', () => {
16
+ silentModuleLogger = silentLogger.module('test-module')
17
+ silentModuleLogger.log('Hello');
18
+ assert.equal(silentModuleLogger.history.length, 1)
19
+ assert.equal(silentModuleLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m \u001b[32mHello\u001b[39m")
20
+ })
21
+ it('should create an logger with context', () => {
22
+ const silentContextLogger = silentModuleLogger.context('test-context')
23
+ silentContextLogger.log('Hello');
24
+ assert.equal(silentContextLogger.history.length, 1)
25
+ assert.equal(silentContextLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] context: \u001b[94mtest-context\u001b[39m | module:\u001b[90mtest-module\u001b[39m \u001b[32mHello\u001b[39m")
26
+ })
27
+ it('should create an logger with listener', () => {
28
+ const silentListenerLogger = silentModuleLogger.listener('onTest')
29
+ silentListenerLogger.log('Hello');
30
+ assert.equal(silentListenerLogger.history.length, 1)
31
+ assert.equal(silentListenerLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m | listener: \u001b[31monTest\u001b[39m \u001b[32mHello\u001b[39m")
32
+ })
33
+ it('should create an logger with method', () => {
34
+ const silentMethodLogger = silentModuleLogger.method('test-method')
35
+ silentMethodLogger.log('Hello');
36
+ assert.equal(silentMethodLogger.history.length, 1)
37
+
38
+ assert.equal(silentMethodLogger.history[0], '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m | method: \u001b[33mtest-method\u001b[39m \u001b[32mHello\u001b[39m')
39
+ })
40
+ })