0
\$\begingroup\$

I am trying to make logger class with debug, info, warn and error methods that "extends" basic log method.

However it is not written using DRY principle, how can I make it DRY, not WET?

Code snippet

const logLevels = {
  0: 'debug',
  1: 'info',
  2: 'warn',
  3: 'error',
  'debug': 0,
  'info': 1,
  'warn': 2,
  'error': 3,
}

class Logger {
  logLevel = logLevels.debug

  log(logLevel, ...messages) {
    if (logLevel >= this.logLevel) {
      const logType = logLevels[logLevel]
      messages.unshift(logType)

      console.log(...messages)
    }
  }

  debug(...messages) {
    this.log(logLevels.debug, ...messages)
  }

  info(...messages) {
    this.log(logLevels.info, ...messages)
  }

  warn(...messages) {
    this.log(logLevels.warn, ...messages)
  }

  error(...messages) {
    this.log(logLevels.error, ...messages)
  }
}

const myLogger = new Logger()
const dateNowHex = () => Date.now().toString(16)

myLogger.debug('this is a test', dateNowHex())
myLogger.info('this is a test', dateNowHex())
myLogger.warn('this is a test', dateNowHex())
myLogger.error('this is a test', dateNowHex())

Should I make better class composition? Can you provide examples?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Interesting question;

  • getting the logType and then unshifting it seems complicated, why not

    console.log(logLevels[logLevel], ...messages)

  • The mapping from an integer to a string and back from string to integer must be addressed to make this DRY

  • This code seems a good fit for monads, which can also help with making this DRY

Other than that there is not much to say.

function Logger() {
  
  const logLevels = ['debug', 'info', 'warn', 'error'];
  this.logLevel = 0;

  this.log = function log(logLevel, ...messages) {
    if (logLevel >= this.logLevel) {
      console.log(logLevels[logLevel], ...messages)
    }
  }
  
  for(let i = 0; i< logLevels.length; i++){
    this[logLevels[i]] = this.log.bind(this, i);
  }

} 

const myLogger = new Logger()
const dateNowHex = () => Date.now().toString(16) 
//debugger; 
myLogger.debug('this is a test', dateNowHex())
myLogger.info('this is a test', dateNowHex())
myLogger.warn('this is a test', dateNowHex())
myLogger.error('this is a test', dateNowHex())

\$\endgroup\$
2
  • \$\begingroup\$ Yeah, thank you very much! The logLevels is actually enum in TypeScript, just ignore it, probably important to be mentioned before. The reason behind unshift is that I am using additional coloring, so it is simplier to make it bold and then make all the messages of some color. Is there a way to make a extend based class composition? Could you please add it to your anwser as second variant? Thanks! \$\endgroup\$
    – RamoFX
    Commented Feb 18, 2021 at 19:01
  • 1
    \$\begingroup\$ @RamoFX I could not get it to work as a class in jsbin :/ \$\endgroup\$
    – konijn
    Commented Feb 18, 2021 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.