hermodlog 1.1.1 → 1.2.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
@@ -1,5 +1,7 @@
1
1
  # hermodlog
2
2
 
3
+ [![npm version](https://badge.fury.io/js/hermodlog.svg)](https://badge.fury.io/js/hermodlog)
4
+
3
5
  Stupid simple logging for JS but with context for heavy log environments.
4
6
 
5
7
  This is just to only pass along a single logger instance between modules and have a hierarchical way to sort and display such modules.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermodlog",
3
- "version": "1.1.1",
3
+ "version": "1.2.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",
@@ -37,4 +37,15 @@ describe('Logger', () => {
37
37
 
38
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
39
  })
40
+ it('should handle level', function () {
41
+ const levelLogger = new Logger({level: 'error'})
42
+ levelLogger.error('Hello');
43
+ assert.equal(levelLogger.history.length, 1)
44
+ levelLogger.warn('Hello');
45
+ assert.equal(levelLogger.history.length, 1)
46
+
47
+ levelLogger.level = 'info'
48
+ levelLogger.trace('Hello');
49
+ assert.equal(levelLogger.history.length, 1)
50
+ });
40
51
  })
@@ -30,15 +30,31 @@ export default function debug(...args) {
30
30
  }
31
31
 
32
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])
33
+
34
+ const typeOfArg = typeof args[i];
35
+
36
+ switch (typeOfArg){
37
+ case 'object':
38
+ const constructorName = args[i].constructor.name;
39
+ message += ` ${chalk.blue(`${constructorName}(`)}`
40
+ if(constructorName === 'Error'){
41
+ message += chalk.blue(JSON.stringify(args[i].message, null, 2))
42
+ }
43
+ message += chalk.blue(JSON.stringify(args[i], null, 2))
44
+ message += ` ${chalk.blue(")")}`
45
+ break;
46
+ case 'string':
47
+ message += chalk.blue(args[i])
48
+ break;
49
+ case "function":
50
+ message += chalk.blue(args[i].toString())
51
+ break;
52
+ case "number":
53
+ default:
54
+ message += chalk.blue(args[i])
55
+ break;
41
56
  }
57
+
42
58
  }
43
59
 
44
60
  this.history.push(message);
@@ -29,14 +29,29 @@ export default function error(...args) {
29
29
  }
30
30
 
31
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])
32
+
33
+ const typeOfArg = typeof args[i];
34
+
35
+ switch (typeOfArg){
36
+ case 'object':
37
+ const constructorName = args[i].constructor.name;
38
+ message += ` ${chalk.red(`${constructorName}(`)}`
39
+ if(constructorName === 'Error'){
40
+ message += chalk.red(JSON.stringify(args[i].message, null, 2))
41
+ }
42
+ message += chalk.red(JSON.stringify(args[i], null, 2))
43
+ message += ` ${chalk.red(")")}`
44
+ break;
45
+ case 'string':
46
+ message += chalk.red(args[i])
47
+ break;
48
+ case "function":
49
+ message += chalk.red(args[i].toString())
50
+ break;
51
+ case "number":
52
+ default:
53
+ message += chalk.red(args[i])
54
+ break;
40
55
  }
41
56
  }
42
57
 
@@ -30,15 +30,31 @@ export default function info(...args) {
30
30
  }
31
31
 
32
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])
33
+
34
+ const typeOfArg = typeof args[i];
35
+
36
+ switch (typeOfArg){
37
+ case 'object':
38
+ const constructorName = args[i].constructor.name;
39
+ message += ` ${chalk.green(`${constructorName}(`)}`
40
+ if(constructorName === 'Error'){
41
+ message += chalk.green(JSON.stringify(args[i].message, null, 2))
42
+ }
43
+ message += chalk.green(JSON.stringify(args[i], null, 2))
44
+ message += ` ${chalk.green(")")}`
45
+ break;
46
+ case 'string':
47
+ message += chalk.green(args[i])
48
+ break;
49
+ case "function":
50
+ message += chalk.green(args[i].toString())
51
+ break;
52
+ case "number":
53
+ default:
54
+ message += chalk.green(args[i])
55
+ break;
41
56
  }
57
+
42
58
  }
43
59
 
44
60
  this.history.push(message);
@@ -30,14 +30,28 @@ export default function trace(...args) {
30
30
  }
31
31
 
32
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])
33
+ const typeOfArg = typeof args[i];
34
+
35
+ switch (typeOfArg){
36
+ case 'object':
37
+ const constructorName = args[i].constructor.name;
38
+ message += ` ${chalk.grey(`${constructorName}(`)}`
39
+ if(constructorName === 'Error'){
40
+ message += chalk.grey(JSON.stringify(args[i].message, null, 2))
41
+ }
42
+ message += chalk.grey(JSON.stringify(args[i], null, 2))
43
+ message += ` ${chalk.grey(")")}`
44
+ break;
45
+ case 'string':
46
+ message += chalk.grey(args[i])
47
+ break;
48
+ case "function":
49
+ message += chalk.grey(args[i].toString())
50
+ break;
51
+ case "number":
52
+ default:
53
+ message += chalk.grey(args[i])
54
+ break;
41
55
  }
42
56
  }
43
57
 
@@ -30,14 +30,29 @@ export default function warn(...args) {
30
30
  }
31
31
 
32
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])
33
+
34
+ const typeOfArg = typeof args[i];
35
+
36
+ switch (typeOfArg){
37
+ case 'object':
38
+ const constructorName = args[i].constructor.name;
39
+ message += ` ${chalk.yellow(`${constructorName}(`)}`
40
+ if(constructorName === 'Error'){
41
+ message += chalk.yellow(JSON.stringify(args[i].message, null, 2))
42
+ }
43
+ message += chalk.yellow(JSON.stringify(args[i], null, 2))
44
+ message += ` ${chalk.yellow(")")}`
45
+ break;
46
+ case 'string':
47
+ message += chalk.yellow(args[i])
48
+ break;
49
+ case "function":
50
+ message += chalk.yellow(args[i].toString())
51
+ break;
52
+ case "number":
53
+ default:
54
+ message += chalk.yellow(args[i])
55
+ break;
41
56
  }
42
57
  }
43
58
 
package/usage.example.js CHANGED
@@ -19,6 +19,9 @@ const object = {
19
19
  }
20
20
  listenerLogger.method('processIncomingMessage').log('Received from ws client',object)
21
21
 
22
+ listenerLogger.log(()=>'This is a function that returns a string')
23
+ listenerLogger.log(1,2,3,4,5,6,7,8,9,10)
24
+
22
25
  class Car {
23
26
  constructor(props) {
24
27
  this.name = props.name;
@@ -36,8 +39,16 @@ listenerLogger.trace('Do not display me trace on error');
36
39
  listenerLogger.log('Do not display me log on error')
37
40
  listenerLogger.error('Do display me')
38
41
  listenerLogger.level = 'trace'
39
- listenerLogger.error('Error')
40
- listenerLogger.warn('Warn')
41
- listenerLogger.info('Info')
42
- listenerLogger.debug('debug')
43
- listenerLogger.trace('trace')
42
+ listenerLogger.error('Error',object)
43
+ listenerLogger.warn('Warn',object)
44
+ listenerLogger.info('Info',object)
45
+ listenerLogger.debug('debug',object)
46
+ listenerLogger.trace('trace',object)
47
+
48
+ const error = new Error('Something happened');
49
+ listenerLogger.error('Error',error)
50
+ listenerLogger.warn('Warn',error)
51
+ listenerLogger.info('Info',error)
52
+ listenerLogger.debug('debug',error)
53
+ listenerLogger.trace('trace',error)
54
+