49327

Using getAppender() in Logback

Question:

I wrote a custom appender JTableAppender which implements ILoggingEvent. This appender have a public setter-method setModel(..) to assign a table model into 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-method 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 get a NullPointerException for exactly this line. The getAppender() didn't work as I wish it would.

Answer1:

<strong>SOLVED</strong>: Use Logger.getRootLogger() instead your local variable 'logger'.

(<em>example</em>)

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

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

Answer2:

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")

Recommend

  • Decoding a sealed trait in Argonaut based on JSON structure?
  • Unable to find the controller for path “/login_check”
  • Unexpected encoder behaviour when applying a flatMap operation on a Apache Spark Dataset
  • Serializing and Deserializing in Symfony
  • Two different SelectItems return single selected value
  • How to use AWS roles with Packer to create AMIs
  • Accessing Redshift from Lambda - Avoiding the 0.0.0.0/0 Security Group
  • c#.NET USB device persistent identifier
  • Testing Akka.NET's Context.Parent with TestKit
  • Raphael JS becomes really slow when using background on a lot of rectangles
  • SQL Count. How can I count how many distinct values are in a table when an other two columns are mat
  • Does Julia have something equivalent to Ans (Matlab) or Last.value (R)
  • logging static methods in a parent class
  • How can I update my Twitter status with Perl and only LWP::UserAgent?
  • reducers get Function not Object in redux, what's wrong with it?
  • cannot be assigned to — it is read only - C#
  • Gradle - How to detect --debug flag from command line?
  • Unexpected Error Google Apps Script Fetch
  • Quick Question About Get and Set
  • How to assign byte[] as a pointer in C#
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • GridView Sorting works once only
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal