Yannick Wald
Yannick Wald

Reputation: 195

Using getAppender() in Logback

I wrote a custom appender JTableAppender which implements ILoggingEvent. This appender has a public setter setModel(..) to assign a table model to the appender, so I can manipulate the model in doAppend().

The JTableAppender is configurated in an xml file, where I named it TABLE:

<appender name="TABLE" class="blabla.jgwf.test.logger.JTableAppender">
    <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder> 
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

To be able to use the setter from the outside, I tried to get the appender using

Logger logger = (Logger) LoggerFactory.getLogger("blabal");
JTableAppender<ILoggingEvent> appender = (JTableAppender<ILoggingEvent>)logger.getAppender("TABLE");

When I tried to set a model using

appender.setModel(...);

I got a NullPointerException for exactly this line. The getAppender() didn't work as I wish it would.

Upvotes: 2

Views: 10230

Answers (3)

raoulsson
raoulsson

Reputation: 16375

To add the Java version to Todor Kolev's Scala answer:

    ch.qos.logback.classic.Logger root = (Logger) org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    // appender name, as configured in logback.xml: <appender name="CLI" class="...
    Appender<ILoggingEvent> appender = root.getAppender("CLI");
    CliLogAppender cliLogAppender = (CliLogAppender) appender;
    System.out.println("> " + cliLogAppender.someCustomMethod());

The example contains the fully qualified package names to avoid confusion between slf4j and logback.

I defined the adapter named CLI in logback.xml. Logback then creates the instance and sends the logs to it. As shown above, this way you can get a handle of the instance and work with the logging data.

Be careful with this, as adding it to a library will break logging, if the client code doesn't use logback! Think: use just for your own code (or with proper guarding code around it).

(And make sure your appenders logging datastructure discards old logs automatically, in case you "forget" to handle and pruge the data properly).

Upvotes: 0

Todor Kolev
Todor Kolev

Reputation: 1482

using logback 1.1.3 I had to:

import ch.qos.logback.classic.Logger
import org.slf4j.LoggerFactory    
...
val root: Logger = val root: Logger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME).asInstanceOf[Logger]
val myAppender = root.getAppender("MyAppenderName")

Upvotes: 1

AlejandroHernandez.Inc
AlejandroHernandez.Inc

Reputation: 153

SOLVED: Use Logger.getRootLogger() instead your local variable 'logger'.

(example)

SMTPAppender emailAppender = 
(SMTPAppender)Logger.getRootLogger().getAppender("SMTPAppender");

doing this, you wont get NullException, and to APPLY changes on your appender then you have to write this: emailAppender.activateOptions();

Upvotes: 3

Related Questions