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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.nt;
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;
28 Append to the NT event log system.
30 <p><b>WARNING</b> This appender can only be installed and used on a
33 <p>Do not forget to place NTEventLogAppender.dll,
34 NTEventLogAppender.amd64.dll, NTEventLogAppender.ia64.dll
35 or NTEventLogAppender.x86.dll as appropriate in a
36 directory that is on the PATH of the Windows system. Otherwise, you
37 will get a java.lang.UnsatisfiedLinkError.
39 @author <a href="mailto:cstaylor@pacbell.net">Chris Taylor</a>
40 @author <a href="mailto:jim_cakalic@na.biomerieux.com">Jim Cakalic</a> */
41 public class NTEventLogAppender extends AppenderSkeleton {
42 private int _handle = 0;
44 private String source = null;
45 private String server = null;
48 public NTEventLogAppender() {
49 this(null, null, null);
52 public NTEventLogAppender(String source) {
53 this(null, source, null);
56 public NTEventLogAppender(String server, String source) {
57 this(server, source, null);
60 public NTEventLogAppender(Layout layout) {
61 this(null, null, layout);
64 public NTEventLogAppender(String source, Layout layout) {
65 this(null, source, layout);
68 public NTEventLogAppender(String server, String source, Layout layout) {
73 this.layout = new TTCCLayout();
79 _handle = registerEventSource(server, source);
80 } catch (Exception e) {
92 void activateOptions() {
95 _handle = registerEventSource(server, source);
96 } catch (Exception e) {
97 LogLog.error("Could not register event source.", e);
104 public void append(LoggingEvent event) {
106 StringBuffer sbuf = new StringBuffer();
108 sbuf.append(layout.format(event));
109 if(layout.ignoresThrowable()) {
110 String[] s = event.getThrowableStrRep();
113 for(int i = 0; i < len; i++) {
118 // Normalize the log message level into the supported categories
119 int nt_category = event.getLevel().toInt();
121 // Anything above FATAL or below DEBUG is labeled as INFO.
122 //if (nt_category > FATAL || nt_category < DEBUG) {
123 // nt_category = INFO;
125 reportEvent(_handle, sbuf.toString(), nt_category);
131 deregisterEventSource(_handle);
136 The <b>Source</b> option which names the source of the event. The
137 current value of this constant is <b>Source</b>.
140 void setSource(String source) {
141 this.source = source.trim();
150 The <code>NTEventLogAppender</code> requires a layout. Hence,
151 this method always returns <code>true</code>. */
153 boolean requiresLayout() {
157 native private int registerEventSource(String server, String source);
158 native private void reportEvent(int handle, String message, int level);
159 native private void deregisterEventSource(int handle);
164 archs = new String[] { System.getProperty("os.arch")};
165 } catch(SecurityException e) {
166 archs = new String[] { "amd64", "ia64", "x86"};
168 boolean loaded = false;
169 for(int i = 0; i < archs.length; i++) {
171 System.loadLibrary("NTEventLogAppender." + archs[i]);
174 } catch(java.lang.UnsatisfiedLinkError e) {
179 System.loadLibrary("NTEventLogAppender");