View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.nt;
19  
20  import org.apache.log4j.AppenderSkeleton;
21  import org.apache.log4j.Layout;
22  import org.apache.log4j.TTCCLayout;
23  import org.apache.log4j.helpers.LogLog;
24  import org.apache.log4j.spi.LoggingEvent;
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 source = null;
43    private String server = null;
44  
45  
46    public NTEventLogAppender() {
47      this(null, null, null);
48    }
49  
50    public NTEventLogAppender(String source) {
51      this(null, source, null);
52    }
53  
54    public NTEventLogAppender(String server, String source) {
55      this(server, source, null);
56    }
57  
58    public NTEventLogAppender(Layout layout) {
59      this(null, null, layout);
60    }
61  
62    public NTEventLogAppender(String source, Layout layout) {
63      this(null, source, layout);
64    }
65  
66    public NTEventLogAppender(String server, String source, Layout layout) {
67      if (source == null) {
68        source = "Log4j";
69      }
70      if (layout == null) {
71        this.layout = new TTCCLayout();
72      } else {
73        this.layout = layout;
74      }
75  
76      try {
77        _handle = registerEventSource(server, source);
78      } catch (Exception e) {
79        e.printStackTrace();
80        _handle = 0;
81      }
82    }
83  
84    public
85    void close() {
86      // unregister ...
87    }
88  
89    public
90    void activateOptions() {
91      if (source != null) {
92        try {
93     _handle = registerEventSource(server, source);
94        } catch (Exception e) {
95     LogLog.error("Could not register event source.", e);
96     _handle = 0;
97        }
98      }
99    }
100 
101 
102   public void append(LoggingEvent event) {
103 
104     StringBuffer sbuf = new StringBuffer();
105 
106     sbuf.append(layout.format(event));
107     if(layout.ignoresThrowable()) {
108       String[] s = event.getThrowableStrRep();
109       if (s != null) {
110    int len = s.length;
111    for(int i = 0; i < len; i++) {
112      sbuf.append(s[i]);
113    }
114       }
115     }
116     // Normalize the log message level into the supported categories
117     int nt_category = event.getLevel().toInt();
118 
119     // Anything above FATAL or below DEBUG is labeled as INFO.
120     //if (nt_category > FATAL || nt_category < DEBUG) {
121     //  nt_category = INFO;
122     //}
123     reportEvent(_handle, sbuf.toString(), nt_category);
124   }
125 
126 
127   public
128   void finalize() {
129     deregisterEventSource(_handle);
130     _handle = 0;
131   }
132 
133   /***
134      The <b>Source</b> option which names the source of the event. The
135      current value of this constant is <b>Source</b>.
136    */
137   public
138   void setSource(String source) {
139     this.source = source.trim();
140   }
141 
142   public
143   String getSource() {
144     return source;
145   }
146 
147 /***
148      The <code>NTEventLogAppender</code> requires a layout. Hence,
149      this method always returns <code>true</code>. */
150   public
151   boolean requiresLayout() {
152     return true;
153   }
154 
155   native private int registerEventSource(String server, String source);
156   native private void reportEvent(int handle, String message, int level);
157   native private void deregisterEventSource(int handle);
158 
159   static {
160     System.loadLibrary("NTEventLogAppender");
161   }
162 }