Does anyone see any major problems with this workaround? If not, I 
recommend it as a patch to the mainline.

package akka.event.slf4j

import akka.actor._
import akka.event.Logging._

/**
 * Stock Slf4jLogger actually logs everything as "{}" with a
 * parameter, which is incompatible with much of the logback
 * machinary. See
 * https://groups.google.com/d/msg/akka-user/YVri58taWsM/X6-XR0_i1nwJ
 * for a discussion.
 */
class FixedSlf4jLogger extends Slf4jLogger {
  override def receive = {
    case event @ Error(cause, logSource, logClass, message) =>
      withMdc(logSource, event) {
        val logger = Logger(logClass, logSource)
        if (logger.isErrorEnabled()) {
          cause match {
            case Error.NoCause | null => logger.error(if (message != null) 
message.toString else null)
            case _ => logger.error(if (message != null) message.toString 
else cause.getLocalizedMessage, cause)
          }
        }
      }

    case event @ Warning(logSource, logClass, message) =>
      withMdc(logSource, event) {
        val logger = Logger(logClass, logSource)
        if (logger.isWarnEnabled()) {
          logger.warn(message.toString)
        }
      }

    case event @ Info(logSource, logClass, message) =>
      withMdc(logSource, event) {
        val logger = Logger(logClass, logSource)
        if (logger.isInfoEnabled()) {
          logger.info(message.toString)
        }
      }

    case event @ Debug(logSource, logClass, message) =>
      withMdc(logSource, event) {
        val logger = Logger(logClass, logSource)
        if (logger.isDebugEnabled()) {
          logger.debug(message.toString)
        }
      }

    case InitializeLogger(_) =>
      sender() ! LoggerInitialized
  }
}



On Monday, 22 June 2015 10:33:20 UTC+1, Sam Halliday wrote:
>
> On Monday, 22 June 2015 10:02:06 UTC+1, √ wrote:
>>
>> Doing the filtering pre-substitution seems like a bug.
>>
>
> If you're suggesting that there is a bug in the filter, then that is not 
> true: it's a well documented, incredibly useful, feature: 
> http://logback.qos.ch/manual/filters.html#DuplicateMessageFilter
>
>   "Note that in case of parameterized logging, only the raw message is 
> taken into consideration."
>
> The problem is in Akka's SLF4J because *everything* the user logs is being 
> converted into `{}` with parameters, even if the user actually logged a 
> fully generated String. This means it's impossible to use this (very useful 
> filter) in a meaningful way with Akka and de-duplication of logging 
> messages must be performed in the application tier (yuck!).
>
> The only practical workaround I can see to fix this today in my codebase 
> is to reimplement Slf4jLogger.scala... but suggestions welcome.
>
> Incidentally, I don't see why you don't just create the log message in 
> Slf4jLogger because at this point we're running in the log actor and not in 
> the thread / actor that initiated the log message. If you really want to 
> delay logging, can't you just ask SLF4J "is debugging enabled for this 
> source/class?"?
>
> Best regards,
> Sam
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to