0

Currently to add standard prefix to log in our Play application, I override LoggerLike trait with additional functions

trait MyLogger extends LoggerLike {

    ...

    def error(message: => String, error: => Throwable)(implicit myObject: MyObject)  {
        if (super.isErrorEnabled) logWithPrefix(encounterAware, super.error, message, error)
    }

    private def logWithPrefix(myObject: MyObject, log: (=> String) => Unit, message: => String) = {
        log(s"A:${myObject.A} ${message}")
    }   

    ...
}

And use it like this:

new Logger(LoggerFactory.getLogger(this.getClass.getSimpleName)) with MyLogger

I expect this to affect only log entries which have implicit myObject in context, and leave all old references valid, but all my logs start referencing new method.

I don't want to change the name of the method or provide default value for implicit.

How do I need to declare error method, to get desired behavior?

mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • 3
    You cannot overload with only implicits. The difference between which overload gets called cannot depend on whether or not an implicit is in scope. – Michael Zajac Mar 15 '15 at 14:02
  • And there is no way arround this? – mavarazy Mar 15 '15 at 14:15
  • 1
    See https://stackoverflow.com/questions/7179229/scala-currying-and-overloading ; a solution is to use the "magnet pattern", e.g. here https://stackoverflow.com/questions/18646866/scalas-overloading-and-default-parameter-limitations-how-to-avoid-writing-down?lq=1 – 0__ Mar 15 '15 at 23:39

0 Answers0