9

I have created a custom appender in log4j2. While using the custom appender, I am getting the following error: "ERROR Attempted to append to non-started appender". Any help is appreciated.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
janeshs
  • 793
  • 2
  • 12
  • 26

1 Answers1

14

Log4j 2 checks for each log event that the appender is in a useable state. The error you are seeing is that Log4j detects that the appender is not ready to be used.

Some appenders need to do preparation before they can be used. The start() lifecycle method is the place where appenders can do initialization. Log4j will not route events to an appender that is not in a STARTED state.

If your appender is added by configuration, Log4j will call the lifecycle method. If your appender extends AbstractAppender, this will update the state and should be sufficient. Otherwise take a look at the lifecycle state management in AbstractAppender.

If you configure in code you may need to call start() explicitly.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Thanks @Remko. I have extended AbstractAppender and have configured the appender using XML. I will investigate the start() method invocation as per your advice. Thanks. – janeshs Sep 05 '16 at 11:06
  • @Remko Popma In case of multiple threads, explicitly calling start() method also leads to this issues. Should one put start() call in synchronized block ? – Gaurav Jul 17 '19 at 11:12
  • I don’t think that applications should call `start()` manually. Custom plugins implementing `LifeCycle` may _implement_ this method. The log4j framework will call the start method. – Remko Popma Jul 17 '19 at 11:15
  • i.e. you probably need to call `super.start()` if you have overridden it like I did – user1075613 May 28 '20 at 20:02