KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > nt > NTEventLogAppender


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
28    Append to the NT event log system.
29
30    <p><b>WARNING</b> This appender can only be installed and used on a
31    Windows system.
32
33    <p>Do not forget to place the file NTEventLogAppender.dll in a
34    directory that is on the PATH of the Windows system. Otherwise, you
35    will get a java.lang.UnsatisfiedLinkError.
36
37    @author <a HREF="mailto:cstaylor@pacbell.net">Chris Taylor</a>
38    @author <a HREF="mailto:jim_cakalic@na.biomerieux.com">Jim Cakalic</a> */

39 public class NTEventLogAppender extends AppenderSkeleton {
40   private int _handle = 0;
41
42   private String JavaDoc source = null;
43   private String JavaDoc 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 JavaDoc source) {
56     this(null, source, null);
57   }
58
59   public NTEventLogAppender(String JavaDoc server, String JavaDoc source) {
60     this(server, source, null);
61   }
62
63   public NTEventLogAppender(Layout layout) {
64     this(null, null, layout);
65   }
66
67   public NTEventLogAppender(String JavaDoc source, Layout layout) {
68     this(null, source, layout);
69   }
70
71   public NTEventLogAppender(String JavaDoc server, String JavaDoc 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 JavaDoc e) {
84       e.printStackTrace();
85       _handle = 0;
86     }
87   }
88
89   public
90   void close() {
91     // unregister ...
92
}
93
94   public
95   void activateOptions() {
96     if (source != null) {
97       try {
98     _handle = registerEventSource(server, source);
99       } catch (Exception JavaDoc 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 JavaDoc sbuf = new StringBuffer JavaDoc();
110
111     sbuf.append(layout.format(event));
112     if(layout.ignoresThrowable()) {
113       String JavaDoc[] 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     // Normalize the log message level into the supported categories
122
int nt_category = event.getLevel().toInt();
123
124     // Anything above FATAL or below DEBUG is labeled as INFO.
125
//if (nt_category > FATAL || nt_category < DEBUG) {
126
// nt_category = INFO;
127
//}
128
reportEvent(_handle, sbuf.toString(), nt_category);
129   }
130
131
132   public
133   void finalize() {
134     deregisterEventSource(_handle);
135     _handle = 0;
136   }
137
138   /**
139      The <b>Source</b> option which names the source of the event. The
140      current value of this constant is <b>Source</b>.
141    */

142   public
143   void setSource(String JavaDoc source) {
144     this.source = source.trim();
145   }
146
147   public
148   String JavaDoc getSource() {
149     return source;
150   }
151
152 /**
153      The <code>NTEventLogAppender</code> requires a layout. Hence,
154      this method always returns <code>true</code>. */

155   public
156   boolean requiresLayout() {
157     return true;
158   }
159
160   native private int registerEventSource(String JavaDoc server, String JavaDoc source);
161   native private void reportEvent(int handle, String JavaDoc message, int level);
162   native private void deregisterEventSource(int handle);
163
164   static {
165     System.loadLibrary("NTEventLogAppender");
166   }
167 }
168
Popular Tags