1 16 17 package org.apache.log4j.nt; 18 19 import org.apache.log4j.*; 20 import org.apache.log4j.spi.LoggingEvent; 21 import org.apache.log4j.Level; 22 import org.apache.log4j.helpers.LogLog; 23 24 import java.io.*; 25 26 27 39 public class NTEventLogAppender extends AppenderSkeleton { 40 private int _handle = 0; 41 42 private String source = null; 43 private String server = null; 44 45 private static final int FATAL = Level.FATAL.toInt(); 46 private static final int ERROR = Level.ERROR.toInt(); 47 private static final int WARN = Level.WARN.toInt(); 48 private static final int INFO = Level.INFO.toInt(); 49 private static final int DEBUG = Level.DEBUG.toInt(); 50 51 public NTEventLogAppender() { 52 this(null, null, null); 53 } 54 55 public NTEventLogAppender(String source) { 56 this(null, source, null); 57 } 58 59 public NTEventLogAppender(String server, String source) { 60 this(server, source, null); 61 } 62 63 public NTEventLogAppender(Layout layout) { 64 this(null, null, layout); 65 } 66 67 public NTEventLogAppender(String source, Layout layout) { 68 this(null, source, layout); 69 } 70 71 public NTEventLogAppender(String server, String source, Layout layout) { 72 if (source == null) { 73 source = "Log4j"; 74 } 75 if (layout == null) { 76 this.layout = new TTCCLayout(); 77 } else { 78 this.layout = layout; 79 } 80 81 try { 82 _handle = registerEventSource(server, source); 83 } catch (Exception e) { 84 e.printStackTrace(); 85 _handle = 0; 86 } 87 } 88 89 public 90 void close() { 91 } 93 94 public 95 void activateOptions() { 96 if (source != null) { 97 try { 98 _handle = registerEventSource(server, source); 99 } catch (Exception e) { 100 LogLog.error("Could not register event source.", e); 101 _handle = 0; 102 } 103 } 104 } 105 106 107 public void append(LoggingEvent event) { 108 109 StringBuffer sbuf = new StringBuffer (); 110 111 sbuf.append(layout.format(event)); 112 if(layout.ignoresThrowable()) { 113 String [] s = event.getThrowableStrRep(); 114 if (s != null) { 115 int len = s.length; 116 for(int i = 0; i < len; i++) { 117 sbuf.append(s[i]); 118 } 119 } 120 } 121 int nt_category = event.getLevel().toInt(); 123 124 reportEvent(_handle, sbuf.toString(), nt_category); 129 } 130 131 132 public 133 void finalize() { 134 deregisterEventSource(_handle); 135 _handle = 0; 136 } 137 138 142 public 143 void setSource(String source) { 144 this.source = source.trim(); 145 } 146 147 public 148 String getSource() { 149 return source; 150 } 151 152 155 public 156 boolean requiresLayout() { 157 return true; 158 } 159 160 native private int registerEventSource(String server, String source); 161 native private void reportEvent(int handle, String message, int level); 162 native private void deregisterEventSource(int handle); 163 164 static { 165 System.loadLibrary("NTEventLogAppender"); 166 } 167 } 168 | Popular Tags |