How to use LoggingEvent class with log4j 2.2
Asked Answered
L

2

14

I want to migrate from Log4j 1.x to Log4j 2.x. So I'm trying to use log4j-1.2-api.jar as mentioned here. My application has an implementation of org.apache.log4j.spi.LoggingEvent, but I cannot find a way to use LoggingEvent with the log4j 2.x api bridge. Is there anyway that I can use LoggingEvent with log4j 2.2 ?

Thanks.

Lunn answered 9/4, 2015 at 14:59 Comment(0)
F
2

Internally, log4j uses a LogEvent to carry all the data pieces (the log message, thread name, timestamp, markers, logger name, etc) to the appenders where they may appear in the output.

Log4j log events are designed for internal use only and are not designed to be extended by applications. For example, Async Loggers use a specialized implementation of the LogEvent interface to meet the requirements of the underlying LMAX Disruptor library. It will not be easy to extend this.

Your use case is not clear to me, but if for example you want to develop a custom appender which needs custom data not present in the LogEvent interface, you can consider using the ThreadContext map to pass data from your app to the custom appender, rather than trying to use a custom log event.

Fredrick answered 13/4, 2015 at 4:4 Comment(2)
Thank you for the answer. I'm upgrading log4j 1.2 custom appender to Log4j 2. Here the LoggingEvent is used to get the log event from the java.util.logging.LogRecord and then to doAppend that log event. Eg: LoggingEvent loggingEvent = LoggingUtils.getLogEvent(record); doAppend(loggingEvent);Lunn
If you are looking for a bridge between Java util logging and log4j, please be aware that log4j2 provides a JUL adapter: logging.apache.org/log4j/2.x/log4j-jul/index.html All you need to do is put the log4j-jul-2.2.jar in the classpath and set system property -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager to redirect all JUL logging to log4j2.Fredrick
B
3

When upgrading Log4j, I simply used LogEvent instead of LoggingEvent.

I had removed AppenderSkeleton and replaced it with AbstractAppender. AppenderSkeleton's append(LoggingEvent event) method appears in AbstractAppender as append(LogEvent event), which was my cue to use LogEvent instead of LoggingEvent.

Bontebok answered 15/1, 2021 at 18:52 Comment(3)
Did this work? Is that compatible?Cain
Can you please guide me here: #72556697?Dimidiate
@PaulErlenmeyer In my case it works. I replaced a custom Appender implementing the old log4j 1.x Appender interface with one which extends AbstractAppender and after some code clean up it works well.Madore
F
2

Internally, log4j uses a LogEvent to carry all the data pieces (the log message, thread name, timestamp, markers, logger name, etc) to the appenders where they may appear in the output.

Log4j log events are designed for internal use only and are not designed to be extended by applications. For example, Async Loggers use a specialized implementation of the LogEvent interface to meet the requirements of the underlying LMAX Disruptor library. It will not be easy to extend this.

Your use case is not clear to me, but if for example you want to develop a custom appender which needs custom data not present in the LogEvent interface, you can consider using the ThreadContext map to pass data from your app to the custom appender, rather than trying to use a custom log event.

Fredrick answered 13/4, 2015 at 4:4 Comment(2)
Thank you for the answer. I'm upgrading log4j 1.2 custom appender to Log4j 2. Here the LoggingEvent is used to get the log event from the java.util.logging.LogRecord and then to doAppend that log event. Eg: LoggingEvent loggingEvent = LoggingUtils.getLogEvent(record); doAppend(loggingEvent);Lunn
If you are looking for a bridge between Java util logging and log4j, please be aware that log4j2 provides a JUL adapter: logging.apache.org/log4j/2.x/log4j-jul/index.html All you need to do is put the log4j-jul-2.2.jar in the classpath and set system property -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager to redirect all JUL logging to log4j2.Fredrick

© 2022 - 2024 — McMap. All rights reserved.