Home > Software engineering >  Cannot create custom Appender for log4j2 in scala
Cannot create custom Appender for log4j2 in scala

Time:09-14

I try to create a custom Appender in Scala (to use it in UTs), to migrate log4j1 to log4j2, but i cannot.

in log4j1 it's like:

class TestAppender extends AppenderSkeleton {

}

now i'm doing somting like:

class TestAppender extends AbstractAppender {

}

but i get

Cannot resolve overloaded constructor `AbstractAppender`

Any thoughts on how to do so?

CodePudding user response:

In Java, you call the superclass constructor in your constructor with super(arguments). In Scala, however, because the body of the class (excluding defs) is the default constructor for the class, the superclass constructor is called by passing the arguments in the extends clause:

class TestAppender extends AbstractAppender(...) {

Since the only non-deprecated constructor for AbstractAppender appears to be this one, you almost certainly want something like:

class TestAppender extends AbstractAppender("test", someFilter, someLayout, whetherToIgnoreExceptions, Array()) {

If you want to allow the user of your class to specify a parameter for AbstractAppender, then you would do something like:

class TestAppender(ignoreExceptions: Boolean) extends AbstractAppender("test", someFilter, someLayout, ignoreExceptions, Array()) {
  • Related