annotate src/goodjava/logger/ThreadLocalAppender.java @ 1450:28b1ddacfad3

logger work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 01 Mar 2020 13:47:02 -0700
parents
children 85d7c1777357
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1450
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.logger;
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 public class ThreadLocalAppender implements Appender {
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 private final Appender defaultAppender;
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 public final ThreadLocal<Appender> threadLocal = new ThreadLocal<Appender>();
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 public ThreadLocalAppender(Appender defaultAppender) {
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 this.defaultAppender = defaultAppender;
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 }
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public void append(LoggingEvent event) {
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 Appender appender = threadLocal.get();
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 if( appender == null )
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 appender = defaultAppender;
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 appender.append(event);
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public void close() {
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 defaultAppender.close();
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 }
28b1ddacfad3 logger work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }