hermodlog 1.0.0 → 1.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/README.md CHANGED
@@ -4,6 +4,8 @@ Stupid simple logging for JS but with context for heavy log environments.
4
4
 
5
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
6
 
7
+ <img width="1365" alt="image" src="https://github.com/Alex-Werner/hermodlog/assets/5849920/347d8d00-8cb1-4838-bc5d-e7434fdd019b">
8
+
7
9
 
8
10
  ## Install
9
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermodlog",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Simple contextual logger to simplify log display in heavy load context and provide easy parsing",
5
5
  "main": "src/Logger.js",
6
6
  "type": "module",
@@ -22,6 +22,6 @@
22
22
  "chalk": "^5.3.0"
23
23
  },
24
24
  "devDependencies": {
25
- "vitest": "^0.34.4"
25
+ "vitest": "^0.34.5"
26
26
  }
27
27
  }
@@ -0,0 +1,9 @@
1
+ const LOG_LEVELS = {
2
+ trace: 0,
3
+ debug: 1,
4
+ info: 2,
5
+ warn: 3,
6
+ error: 4,
7
+ }
8
+
9
+ export default LOG_LEVELS
package/src/Logger.js CHANGED
@@ -1,8 +1,22 @@
1
- import chalk from 'chalk';
2
- export default class Logger {
3
- constructor(_date, level = 'info') {
4
-
5
- this.level = level;
1
+ import LOG_LEVELS from "./LOG_LEVELS.js";
2
+
3
+ import context from "./methods/context.js";
4
+ import debug from "./methods/debug.js";
5
+ import error from "./methods/error.js";
6
+ import info from "./methods/info.js";
7
+ import listener from "./methods/listener.js";
8
+ import log from "./methods/log.js";
9
+ import method from "./methods/method.js";
10
+ import module from "./methods/module.js";
11
+ import trace from "./methods/trace.js";
12
+ import warn from "./methods/warn.js";
13
+
14
+ class Logger {
15
+ constructor(props = {}) {
16
+ this.level = props.level ?? 'info';
17
+ if (!LOG_LEVELS.hasOwnProperty(this.level)) {
18
+ throw new Error(`Unknown log level ${this.level}`)
19
+ }
6
20
 
7
21
  this.history = [];
8
22
 
@@ -10,137 +24,27 @@ export default class Logger {
10
24
  this.methodName = null
11
25
  this.moduleName = null
12
26
  this.listenerName = null
13
- if(_date){
14
- this._date = _date;
27
+ if (props.date) {
28
+ this.date = props.date;
15
29
  }
16
30
  }
17
31
 
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
32
  _log(message) {
56
33
  console.log(message)
57
34
  }
58
35
 
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
36
 
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
37
  };
38
+
39
+ Logger.prototype.context = context;
40
+ Logger.prototype.debug = debug;
41
+ Logger.prototype.error = error;
42
+ Logger.prototype.info = info;
43
+ Logger.prototype.listener = listener;
44
+ Logger.prototype.log = log;
45
+ Logger.prototype.method = method;
46
+ Logger.prototype.module = module;
47
+ Logger.prototype.trace = trace;
48
+ Logger.prototype.warn = warn;
49
+
50
+ export default Logger;
@@ -0,0 +1,12 @@
1
+ import Logger from "../Logger.js";
2
+
3
+ export default function context(_context) {
4
+ const logger = new Logger(this.date);
5
+ logger.contextName = _context;
6
+ logger.methodName = this.methodName;
7
+ logger.moduleName = this.moduleName;
8
+ logger.listenerName = this.listenerName;
9
+
10
+ logger._log = this._log.bind(logger);
11
+ return logger;
12
+ }
@@ -0,0 +1,50 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import chalk from "chalk";
3
+
4
+ export default function debug(...args) {
5
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['debug']){
6
+ return;
7
+ }
8
+ let _message = args[0];
9
+ if(args.length > 1){
10
+ _message = args.slice(0).join(' | ');
11
+ }
12
+ const date = this.date || new Date();
13
+ let message = `[${chalk.gray(date.toISOString())}][${chalk.blue('D')}]`;
14
+
15
+
16
+ if(this.contextName){
17
+ message += ` context: ${chalk.blueBright(this.contextName)} |`;
18
+ }
19
+ if(this.moduleName){
20
+ message += ` module:${chalk.gray(this.moduleName)} |`;
21
+ }
22
+ if(this.listenerName){
23
+ message += ` listener: ${chalk.cyan(this.listenerName)} |`;
24
+ }
25
+ if(this.methodName){
26
+ message += ` method: ${chalk.yellow(this.methodName)} |`;
27
+ }
28
+ if(message.endsWith(' |')){
29
+ message = message.slice(0, -1);
30
+ }
31
+
32
+ for(let i = 0; i < args.length; i++){
33
+ if(typeof args[i] === 'object'){
34
+ const constructorName = args[i].constructor.name;
35
+ message += ` ${chalk.blue(`${constructorName}(`)}`
36
+ message += chalk.blue(JSON.stringify(args[i], null, 2))
37
+ message += ` ${chalk.blue(")")}`
38
+ }
39
+ if(typeof args[i] === 'string'){
40
+ message += chalk.blue(args[i])
41
+ }
42
+ }
43
+
44
+ this.history.push(message);
45
+ if(this.history.length > 100){
46
+ this.history.shift();
47
+ }
48
+
49
+ this._log(message);
50
+ }
@@ -0,0 +1,49 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import chalk from "chalk";
3
+
4
+ export default function error(...args) {
5
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['error']){
6
+ return;
7
+ }
8
+ let _message = args[0];
9
+ if(args.length > 1){
10
+ _message = args.slice(0).join(' | ');
11
+ }
12
+ const date = this.date || new Date();
13
+ let message = `[${chalk.gray(date.toISOString())}][${chalk.red('E')}]`;
14
+
15
+ if(this.contextName){
16
+ message += ` context: ${chalk.blueBright(this.contextName)} |`;
17
+ }
18
+ if(this.moduleName){
19
+ message += ` module:${chalk.gray(this.moduleName)} |`;
20
+ }
21
+ if(this.listenerName){
22
+ message += ` listener: ${chalk.cyan(this.listenerName)} |`;
23
+ }
24
+ if(this.methodName){
25
+ message += ` method: ${chalk.yellow(this.methodName)} |`;
26
+ }
27
+ if(message.endsWith(' |')){
28
+ message = message.slice(0, -1);
29
+ }
30
+
31
+ for(let i = 0; i < args.length; i++){
32
+ if(typeof args[i] === 'object'){
33
+ const constructorName = args[i].constructor.name;
34
+ message += ` ${chalk.red(`${constructorName}(`)}`
35
+ message += chalk.red(JSON.stringify(args[i], null, 2))
36
+ message += ` ${chalk.red(")")}`
37
+ }
38
+ if(typeof args[i] === 'string'){
39
+ message += chalk.red(args[i])
40
+ }
41
+ }
42
+
43
+ this.history.push(message);
44
+ if(this.history.length > 100){
45
+ this.history.shift();
46
+ }
47
+
48
+ this._log(message);
49
+ }
@@ -0,0 +1,50 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import chalk from "chalk";
3
+
4
+ export default function info(...args) {
5
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['info']){
6
+ return;
7
+ }
8
+ let _message = args[0];
9
+ if(args.length > 1){
10
+ _message = args.slice(0).join(' | ');
11
+ }
12
+ const date = this.date || new Date();
13
+ let message = `[${chalk.gray(date.toISOString())}][${chalk.green('I')}]`;
14
+
15
+
16
+ if(this.contextName){
17
+ message += ` context: ${chalk.blueBright(this.contextName)} |`;
18
+ }
19
+ if(this.moduleName){
20
+ message += ` module:${chalk.gray(this.moduleName)} |`;
21
+ }
22
+ if(this.listenerName){
23
+ message += ` listener: ${chalk.cyan(this.listenerName)} |`;
24
+ }
25
+ if(this.methodName){
26
+ message += ` method: ${chalk.yellow(this.methodName)} |`;
27
+ }
28
+ if(message.endsWith(' |')){
29
+ message = message.slice(0, -1);
30
+ }
31
+
32
+ for(let i = 0; i < args.length; i++){
33
+ if(typeof args[i] === 'object'){
34
+ const constructorName = args[i].constructor.name;
35
+ message += ` ${chalk.yellow(`${constructorName}(`)}`
36
+ message += chalk.yellow(JSON.stringify(args[i], null, 2))
37
+ message += ` ${chalk.yellow(")")}`
38
+ }
39
+ if(typeof args[i] === 'string'){
40
+ message += chalk.green(args[i])
41
+ }
42
+ }
43
+
44
+ this.history.push(message);
45
+ if(this.history.length > 100){
46
+ this.history.shift();
47
+ }
48
+
49
+ this._log(message);
50
+ }
@@ -0,0 +1,10 @@
1
+ import Logger from "../Logger.js";
2
+
3
+ export default function listener(_listener) {
4
+ const logger = new Logger(this.date);
5
+ logger.moduleName = this.moduleName;
6
+ logger.contextName = this.contextName;
7
+ logger.listenerName = _listener;
8
+ logger._log = this._log.bind(logger);
9
+ return logger;
10
+ }
@@ -0,0 +1,6 @@
1
+ import chalk from "chalk";
2
+ import LOG_LEVELS from "../LOG_LEVELS.js";
3
+
4
+ export default function log(...args) {
5
+ return this.info(...args);
6
+ }
@@ -0,0 +1,11 @@
1
+ import Logger from "../Logger.js";
2
+
3
+ export default function method(_method) {
4
+ const logger = new Logger(this.date);
5
+ logger.moduleName = this.moduleName;
6
+ logger.contextName = this.contextName;
7
+ logger.listenerName = this.listenerName;
8
+ logger.methodName = _method;
9
+ logger._log = this._log.bind(logger);
10
+ return logger;
11
+ }
@@ -0,0 +1,12 @@
1
+ import Logger from "../Logger.js";
2
+
3
+ export default function module(_module) {
4
+ const logger = new Logger(this.date);
5
+ logger.methodName = this.methodName;
6
+ logger.moduleName = _module;
7
+ logger.contextName = this.contextName;
8
+ logger.listenerName = this.listenerName;
9
+
10
+ logger._log = this._log.bind(logger);
11
+ return logger;
12
+ }
@@ -0,0 +1,50 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import chalk from "chalk";
3
+
4
+ export default function trace(...args) {
5
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['trace']){
6
+ return;
7
+ }
8
+ let _message = args[0];
9
+ if(args.length > 1){
10
+ _message = args.slice(0).join(' | ');
11
+ }
12
+ const date = this.date || new Date();
13
+ let message = `[${chalk.gray(date.toISOString())}][${chalk.grey('T')}]`;
14
+
15
+
16
+ if(this.contextName){
17
+ message += ` context: ${chalk.blueBright(this.contextName)} |`;
18
+ }
19
+ if(this.moduleName){
20
+ message += ` module:${chalk.gray(this.moduleName)} |`;
21
+ }
22
+ if(this.listenerName){
23
+ message += ` listener: ${chalk.cyan(this.listenerName)} |`;
24
+ }
25
+ if(this.methodName){
26
+ message += ` method: ${chalk.yellow(this.methodName)} |`;
27
+ }
28
+ if(message.endsWith(' |')){
29
+ message = message.slice(0, -1);
30
+ }
31
+
32
+ for(let i = 0; i < args.length; i++){
33
+ if(typeof args[i] === 'object'){
34
+ const constructorName = args[i].constructor.name;
35
+ message += ` ${chalk.grey(`${constructorName}(`)}`
36
+ message += chalk.grey(JSON.stringify(args[i], null, 2))
37
+ message += ` ${chalk.grey(")")}`
38
+ }
39
+ if(typeof args[i] === 'string'){
40
+ message += chalk.grey(args[i])
41
+ }
42
+ }
43
+
44
+ this.history.push(message);
45
+ if(this.history.length > 100){
46
+ this.history.shift();
47
+ }
48
+
49
+ this._log(message);
50
+ }
@@ -0,0 +1,50 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import chalk from "chalk";
3
+
4
+ export default function warn(...args) {
5
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['warn']){
6
+ return;
7
+ }
8
+ let _message = args[0];
9
+ if(args.length > 1){
10
+ _message = args.slice(0).join(' | ');
11
+ }
12
+ const date = this.date || new Date();
13
+ let message = `[${chalk.gray(date.toISOString())}][${chalk.yellow('W')}]`;
14
+
15
+
16
+ if(this.contextName){
17
+ message += ` context: ${chalk.blueBright(this.contextName)} |`;
18
+ }
19
+ if(this.moduleName){
20
+ message += ` module:${chalk.gray(this.moduleName)} |`;
21
+ }
22
+ if(this.listenerName){
23
+ message += ` listener: ${chalk.cyan(this.listenerName)} |`;
24
+ }
25
+ if(this.methodName){
26
+ message += ` method: ${chalk.yellow(this.methodName)} |`;
27
+ }
28
+ if(message.endsWith(' |')){
29
+ message = message.slice(0, -1);
30
+ }
31
+
32
+ for(let i = 0; i < args.length; i++){
33
+ if(typeof args[i] === 'object'){
34
+ const constructorName = args[i].constructor.name;
35
+ message += ` ${chalk.yellow(`${constructorName}(`)}`
36
+ message += chalk.yellow(JSON.stringify(args[i], null, 2))
37
+ message += ` ${chalk.yellow(")")}`
38
+ }
39
+ if(typeof args[i] === 'string'){
40
+ message += chalk.yellow(args[i])
41
+ }
42
+ }
43
+
44
+ this.history.push(message);
45
+ if(this.history.length > 100){
46
+ this.history.shift();
47
+ }
48
+
49
+ this._log(message);
50
+ }
@@ -0,0 +1,43 @@
1
+ import Logger from './src/Logger.js';
2
+
3
+
4
+ const logger = new Logger({
5
+ level: 'trace',
6
+ });
7
+ const contextLogger = logger.context('APIContext')
8
+ contextLogger.log('Started API');
9
+ const moduleLogger = contextLogger.module('Websocket Server')
10
+ moduleLogger.log('Started WSS');
11
+ const listenerLogger = moduleLogger.listener('onConnection')
12
+ listenerLogger.log('New connection');
13
+ const object = {
14
+ x:42,
15
+ y: {
16
+ b:[1,2,3]
17
+ },
18
+ req:'getCar'
19
+ }
20
+ listenerLogger.method('processIncomingMessage').log('Received from ws client',object)
21
+
22
+ class Car {
23
+ constructor(props) {
24
+ this.name = props.name;
25
+ }
26
+ };
27
+
28
+ const car = new Car({name: 'RSQ'});
29
+
30
+ listenerLogger.method('processIncomingMessage').log('Responding to client object:',car)
31
+ listenerLogger.error('Something happened while sending:',car)
32
+
33
+ listenerLogger.trace('Display me');
34
+ listenerLogger.level = 'error'
35
+ listenerLogger.trace('Do not display me trace on error');
36
+ listenerLogger.log('Do not display me log on error')
37
+ listenerLogger.error('Do display me')
38
+ listenerLogger.level = 'trace'
39
+ listenerLogger.error('Error')
40
+ listenerLogger.warn('Warn')
41
+ listenerLogger.info('Info')
42
+ listenerLogger.debug('debug')
43
+ listenerLogger.trace('trace')