Forked from kengelke/CachingSingletonAppender .java
Last active
September 3, 2015 07:38
-
-
Save phamthaithinh/63d7cd7ff6f431d5e1ed to your computer and use it in GitHub Desktop.
A custom log appender for log4j that allows one to catch log messages generated by log4j from within the application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.List; | |
| import org.apache.log4j.spi.LoggingEvent; | |
| public class CachingSingletonAppender { | |
| private static final CachingSingletonAppender theInstance = new CachingSingletonAppender(); | |
| private List<LogEventListener> listeners; | |
| private List<LogEvent> eventCache; | |
| private CachingSingletonAppender() { | |
| listeners = new ArrayList<>(); | |
| eventCache = new ArrayList<>(); | |
| } | |
| public static CachingSingletonAppender getInstance() { | |
| return theInstance; | |
| } | |
| public void append(LoggingEvent le) { | |
| LogEvent event = new LogEvent(le.getLevel().toString(), le.getMessage().toString(), new Date(le.getTimeStamp())); | |
| if (!listeners.isEmpty()) { | |
| for (LogEventListener listener : listeners) { | |
| listener.handle(event); | |
| } | |
| } else { | |
| eventCache.add(event); | |
| } | |
| } | |
| public void addLoggingEventListener(LogEventListener listener) { | |
| listeners.add(listener); | |
| if (listeners.size() == 1 && !eventCache.isEmpty()) { | |
| sendAndClearCache(); | |
| } | |
| } | |
| public void removeLoggingEventListener(LogEventListener listener) { | |
| listeners.remove(listener); | |
| } | |
| synchronized private void sendAndClearCache() { | |
| for (LogEventListener listener : listeners) { | |
| for (LogEvent event : eventCache) { | |
| listener.handle(event); | |
| } | |
| } | |
| eventCache.clear(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.log4j.AppenderSkeleton; | |
| import org.apache.log4j.spi.LoggingEvent; | |
| public class InternalAppenderWrapper extends AppenderSkeleton { | |
| private CachingSingletonAppender appender = CachingSingletonAppender.getInstance(); | |
| @Override | |
| protected void append(LoggingEvent le) { | |
| appender.append(le); | |
| } | |
| @Override | |
| public void close() { | |
| } | |
| @Override | |
| public boolean requiresLayout() { | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| log4j.rootLogger=INFO, internal | |
| log4j.appender.internal=InternalAppenderWrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Date; | |
| public class LogEvent { | |
| private String level; | |
| private String message; | |
| private Date timestamp; | |
| public LogEvent() { | |
| } | |
| public LogEvent(String level, String message, Date timestamp) { | |
| this.level = level; | |
| this.message = message; | |
| this.timestamp = timestamp; | |
| } | |
| public String getLevel() { | |
| return level; | |
| } | |
| public void setLevel(String level) { | |
| this.level = level; | |
| } | |
| public String getMessage() { | |
| return message; | |
| } | |
| public void setMessage(String message) { | |
| this.message = message; | |
| } | |
| public Date getTimestamp() { | |
| return timestamp; | |
| } | |
| public void setTimestamp(Date timestamp) { | |
| this.timestamp = timestamp; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface LogEventListener { | |
| void handle(LogEvent event); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment