Home > Software engineering >  How to override a function in a class being returned from module in javascript
How to override a function in a class being returned from module in javascript

Time:10-07

I have a logging class in a module which contains a static function to return an instance of itself, so a simplified version looks like this:

Class Logger {

    static createFromConfig(key) {   
      return new Logger(key);
    }

    info(message) {
      // do logging stuff
    }

    debug(message) {
      // do logging stuff
    }

    error(message) {
      // do logging stuff
    }
}

This is used in many places throughout our codebase like:

    import logging from 'logging';
    const log = logging.Logger.createFromConfig('keyname');
    log.info('App starting');

In one project I wish to add some extra functionality to the error function, without having to change code in any of the places calling it. So something like

log.error = () => {
    // do my extra stuff here
    log.error() // call the original error function
}

I've tried extending the class like below but I think the problem is the original base class just gets returned from the static function so my overridden function isn't called:

class dblogger extends logging.Logger {
  error(...args) {
   // do my extra stuff here 
    super.error();
  }
}

const dblogging = {
  Logger: dblogger
};

export default dblogging;

CodePudding user response:

You need to call the original method from the Logger prototype.

function getdblogger(keyname) {
    const log = logging.Logger.createFromConfig(keyname);
    log.error = function(message) {
        // do my extra stuff here
        Logger.prototype.error.call(this, message) // call the original error function
    }
    return log;
}

Note that you have to use an ordinary function rather than an arrow function so it gets this.

If you can get changes made to the original Logger class, you could chage createFromConfig() to allow the class to be specified, then you could use your subclass.

Class Logger {

    static createFromConfig(key, loggerClass = Logger) {   
      return new loggerClass(key);
    }

    info(message) {
      // do logging stuff
    }

    debug(message) {
      // do logging stuff
    }

    error(message) {
      // do logging stuff
    }
}

Then in your files:

const log = logging.Logger.createFromConfig('keyname', dblogger);

CodePudding user response:

I believe you really want to use this.constructor.prototype.error within your method:

class Logger{
  static createFromConfig(key){   
    return new Logger(key);
  }
  info(message){
    // do logging stuff
  }
  debug(message){
    // do logging stuff
  }
  error(message){
    return message;
    // do logging stuff
  }
}
class logging extends Logger{
}
const log = logging.createFromConfig('test');
log.error = function(message){
  console.log(this.constructor.prototype.error('Logger message here'));
  console.log(message);
}
log.error('within log');

  • Related