KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > logging > ArchiveAppender


1 package org.oddjob.logging;
2
3 import org.apache.log4j.AppenderSkeleton;
4 import org.apache.log4j.Layout;
5 import org.apache.log4j.spi.LoggingEvent;
6
7 /**
8  * A Log4j appender which logs to LogArchiver.
9  */

10
11 public class ArchiveAppender extends AppenderSkeleton {
12
13     private final LogArchiverCache logArchiver;
14     
15     /**
16      * Create an ArchiveAppender using the given logArchiver and layout.
17      *
18      * @param logArchiver The LogArchiver to archive to.
19      * @param layout The layout to use.
20      */

21     public ArchiveAppender(LogArchiverCache logArchiver, Layout layout) {
22         this.logArchiver = logArchiver;
23         this.layout = layout;
24     }
25
26     public void close() {
27     }
28
29     public void append(LoggingEvent event) {
30         String JavaDoc archive = event.getLoggerName();
31         if (!logArchiver.hasArchive(archive)) {
32             archive = (String JavaDoc) event.getMDC(Log4jArchiver.MDC);
33         }
34         if (!logArchiver.hasArchive(archive)) {
35             return;
36         }
37         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
38         text.append(this.layout.format(event));
39
40         if (layout.ignoresThrowable()) {
41             String JavaDoc[] s = event.getThrowableStrRep();
42             if (s != null) {
43                 int len = s.length;
44                 for (int i = 0; i < len; i++) {
45                     text.append(s[i]);
46                     text.append(Layout.LINE_SEP);
47                 }
48             }
49         }
50
51         logArchiver.addEvent(archive, Log4jArchiver.convertLevel(event.getLevel()), text.toString());
52     }
53     
54     public boolean requiresLayout() {
55         return false;
56     }
57 }
58
Popular Tags