Source:Java program malfunction
class SessionLogAppender implements Appender<ILogEvent> {
private static final TheadLocal<Object> sessionHolder = new ThreadLocal<Object>();
private Map<Object, SessionLog> sessionLogs = new ConcurrentHashMap<>();
/** must be invoked when a new session begins */
public static void begin(Object session) {
sessionHolder.set(session);
}
/** must be invoked when a session ends */
public static void end() {
Object session = sessionHolder.get();
writeIfNecessary(sessionLogs.get(session));
sessionLogs.remove(session);
sessionHolder.clear();
}
@Override
public void doAppend(ILogEvent e) {
Object session = sessionHolder.get();
SessionLog l = sessionLogs.get(session);
if (l == null) {
l = new SessionLog();
sessionLogs.put(session, l);
}
l.append(e);
}
}