Skip to content

Commit

Permalink
feat: added function handling + Error's message helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Werner committed Dec 20, 2023
1 parent 72fc937 commit b07fa95
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 45 deletions.
11 changes: 11 additions & 0 deletions src/Logger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ describe('Logger', () => {

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')
})
it('should handle level', function () {
const levelLogger = new Logger({level: 'error'})
levelLogger.error('Hello');
assert.equal(levelLogger.history.length, 1)
levelLogger.warn('Hello');
assert.equal(levelLogger.history.length, 1)

levelLogger.level = 'info'
levelLogger.trace('Hello');
assert.equal(levelLogger.history.length, 1)
});
})
32 changes: 24 additions & 8 deletions src/methods/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,31 @@ export default function debug(...args) {
}

for(let i = 0; i < args.length; i++){
if(typeof args[i] === 'object'){
const constructorName = args[i].constructor.name;
message += ` ${chalk.blue(`${constructorName}(`)}`
message += chalk.blue(JSON.stringify(args[i], null, 2))
message += ` ${chalk.blue(")")}`
}
if(typeof args[i] === 'string'){
message += chalk.blue(args[i])

const typeOfArg = typeof args[i];

switch (typeOfArg){
case 'object':
const constructorName = args[i].constructor.name;
message += ` ${chalk.blue(`${constructorName}(`)}`
if(constructorName === 'Error'){
message += chalk.blue(JSON.stringify(args[i].message, null, 2))
}
message += chalk.blue(JSON.stringify(args[i], null, 2))
message += ` ${chalk.blue(")")}`
break;
case 'string':
message += chalk.blue(args[i])
break;
case "function":
message += chalk.blue(args[i].toString())
break;
case "number":
default:
message += chalk.blue(args[i])
break;
}

}

this.history.push(message);
Expand Down
31 changes: 23 additions & 8 deletions src/methods/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@ export default function error(...args) {
}

for(let i = 0; i < args.length; i++){
if(typeof args[i] === 'object'){
const constructorName = args[i].constructor.name;
message += ` ${chalk.red(`${constructorName}(`)}`
message += chalk.red(JSON.stringify(args[i], null, 2))
message += ` ${chalk.red(")")}`
}
if(typeof args[i] === 'string'){
message += chalk.red(args[i])

const typeOfArg = typeof args[i];

switch (typeOfArg){
case 'object':
const constructorName = args[i].constructor.name;
message += ` ${chalk.red(`${constructorName}(`)}`
if(constructorName === 'Error'){
message += chalk.red(JSON.stringify(args[i].message, null, 2))
}
message += chalk.red(JSON.stringify(args[i], null, 2))
message += ` ${chalk.red(")")}`
break;
case 'string':
message += chalk.red(args[i])
break;
case "function":
message += chalk.red(args[i].toString())
break;
case "number":
default:
message += chalk.red(args[i])
break;
}
}

Expand Down
32 changes: 24 additions & 8 deletions src/methods/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,31 @@ export default function info(...args) {
}

for(let i = 0; i < args.length; i++){
if(typeof args[i] === 'object'){
const constructorName = args[i].constructor.name;
message += ` ${chalk.yellow(`${constructorName}(`)}`
message += chalk.yellow(JSON.stringify(args[i], null, 2))
message += ` ${chalk.yellow(")")}`
}
if(typeof args[i] === 'string'){
message += chalk.green(args[i])

const typeOfArg = typeof args[i];

switch (typeOfArg){
case 'object':
const constructorName = args[i].constructor.name;
message += ` ${chalk.green(`${constructorName}(`)}`
if(constructorName === 'Error'){
message += chalk.green(JSON.stringify(args[i].message, null, 2))
}
message += chalk.green(JSON.stringify(args[i], null, 2))
message += ` ${chalk.green(")")}`
break;
case 'string':
message += chalk.green(args[i])
break;
case "function":
message += chalk.green(args[i].toString())
break;
case "number":
default:
message += chalk.green(args[i])
break;
}

}

this.history.push(message);
Expand Down
30 changes: 22 additions & 8 deletions src/methods/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,28 @@ export default function trace(...args) {
}

for(let i = 0; i < args.length; i++){
if(typeof args[i] === 'object'){
const constructorName = args[i].constructor.name;
message += ` ${chalk.grey(`${constructorName}(`)}`
message += chalk.grey(JSON.stringify(args[i], null, 2))
message += ` ${chalk.grey(")")}`
}
if(typeof args[i] === 'string'){
message += chalk.grey(args[i])
const typeOfArg = typeof args[i];

switch (typeOfArg){
case 'object':
const constructorName = args[i].constructor.name;
message += ` ${chalk.grey(`${constructorName}(`)}`
if(constructorName === 'Error'){
message += chalk.grey(JSON.stringify(args[i].message, null, 2))
}
message += chalk.grey(JSON.stringify(args[i], null, 2))
message += ` ${chalk.grey(")")}`
break;
case 'string':
message += chalk.grey(args[i])
break;
case "function":
message += chalk.grey(args[i].toString())
break;
case "number":
default:
message += chalk.grey(args[i])
break;
}
}

Expand Down
31 changes: 23 additions & 8 deletions src/methods/warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,29 @@ export default function warn(...args) {
}

for(let i = 0; i < args.length; i++){
if(typeof args[i] === 'object'){
const constructorName = args[i].constructor.name;
message += ` ${chalk.yellow(`${constructorName}(`)}`
message += chalk.yellow(JSON.stringify(args[i], null, 2))
message += ` ${chalk.yellow(")")}`
}
if(typeof args[i] === 'string'){
message += chalk.yellow(args[i])

const typeOfArg = typeof args[i];

switch (typeOfArg){
case 'object':
const constructorName = args[i].constructor.name;
message += ` ${chalk.yellow(`${constructorName}(`)}`
if(constructorName === 'Error'){
message += chalk.yellow(JSON.stringify(args[i].message, null, 2))
}
message += chalk.yellow(JSON.stringify(args[i], null, 2))
message += ` ${chalk.yellow(")")}`
break;
case 'string':
message += chalk.yellow(args[i])
break;
case "function":
message += chalk.yellow(args[i].toString())
break;
case "number":
default:
message += chalk.yellow(args[i])
break;
}
}

Expand Down
21 changes: 16 additions & 5 deletions usage.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const object = {
}
listenerLogger.method('processIncomingMessage').log('Received from ws client',object)

listenerLogger.log(()=>'This is a function that returns a string')
listenerLogger.log(1,2,3,4,5,6,7,8,9,10)

class Car {
constructor(props) {
this.name = props.name;
Expand All @@ -36,8 +39,16 @@ listenerLogger.trace('Do not display me trace on error');
listenerLogger.log('Do not display me log on error')
listenerLogger.error('Do display me')
listenerLogger.level = 'trace'
listenerLogger.error('Error')
listenerLogger.warn('Warn')
listenerLogger.info('Info')
listenerLogger.debug('debug')
listenerLogger.trace('trace')
listenerLogger.error('Error',object)
listenerLogger.warn('Warn',object)
listenerLogger.info('Info',object)
listenerLogger.debug('debug',object)
listenerLogger.trace('trace',object)

const error = new Error('Something happened');
listenerLogger.error('Error',error)
listenerLogger.warn('Warn',error)
listenerLogger.info('Info',error)
listenerLogger.debug('debug',error)
listenerLogger.trace('trace',error)

0 comments on commit b07fa95

Please sign in to comment.