Question

I am trying to get an logger and add a specific appender to it. My code is very simple but I am not sure how to get this working.

val loggerInstance = LoggerFactory.getLogger("FOO.class")
var fileAppender =  new FileAppender()
// fileAppender .setFile , sietPattern etc..the parameters i want
loggerInstance.addAppender(fileAppender) 

I get an error here

Multiple markers at this line
- type mismatch; found : ch.qos.logback.core.FileAppender[Nothing] required: 
     ch.qos.logback.core.Appender[ch.qos.logback.classic.spi.ILoggingEvent] Note: Nothing <: 
     ch.qos.logback.classic.spi.ILoggingEvent, but Java-defined trait Appender is invariant in type E. You may wish to investigate a 
     wildcard type such as `_ <: ch.qos.logback.classic.spi.ILoggingEvent`. (SLS 3.2.10)
    - Line breakpoint:loggerchange [line: 76] - addAppender

I have no clue what this error means and how to solve it. Can someone help me?

EDIT :

I tried to do what was told by drexin. I was not able to extend the interface and define the functions . There were only three functions , setName,getName, and doAppend. i am not sure as how to define these functions. Meanwhile i tried something and removed the errors. Please look into the code and let me know if what i am doing makes any sense .

val encoder = new PatternLayoutEncoder()
encoder2.setContext(context)
encoder2.setPattern("%msg%")    
fileAppender.setAppend(true)
fileAppender.setContext(context)
fileAppender.setEncoder(encoder2.asInstanceOf[Encoder[Nothing]])

loggerInstance.asInstanceOf[Logger].addAppender(fileAppender
.asInstanceOf[Appender[ILoggingEvent]])

I know using asInstanceOf is not a smart way of coding but for now i want to make this work . When i execute this code i am getting the file in which i want to log but there is no logs inside it. I have checked for level errors , but that's not the case. i believe there is something wrong with encoder/layout. I am not sure how to fix it. Can someone show me how to either extend the class and apply the functions or what is wrong in this new code .

Was it helpful?

Solution

The FileAppender has a type parameter class FileAppender[E], but you instantiate it without providing such a parameter, so scala decides to put Nothing in as type. addAppender expects an appender of type Appender[ILoggingEvent], that is what the error says. What you have to do now, is to either use an existing, or create your own subclass of ILoggingEvent and provide it as type param:

class MyLoggingEvent extends ILoggingEvent {
  // implement ALL the methods
}

val fileAppender = new FileAppender[MyLoggingEvent]()

OTHER TIPS

My second approach worked . I am able to see the logs now. I was making the mistake in the pattern. It just a small tweak .

  val encoder = new PatternLayoutEncoder()
  encoder.setContext(context)
  encoder.setPattern("%msg%n")    
  fileAppender.setAppend(true)
  fileAppender.setContext(context)
  fileAppender.setEncoder(encoder.asInstanceOf[Encoder[Nothing]])

  loggerInstance.asInstanceOf[Logger].addAppender(fileAppender
  .asInstanceOf[Appender[ILoggingEvent]])

I will summarize all my efforts towards logging/appending automatically and put it up soon. Hope that will help others too .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top