KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > xml > XMLLayout


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 // Contributors: Mathias Bogaert
18

19 package org.apache.log4j.xml;
20
21 import org.apache.log4j.Layout;
22 import org.apache.log4j.spi.LoggingEvent;
23 import org.apache.log4j.spi.LocationInfo;
24 import org.apache.log4j.helpers.OptionConverter;
25 import org.apache.log4j.helpers.DateLayout;
26 import org.apache.log4j.helpers.Transform;
27
28 /**
29  * The output of the XMLLayout consists of a series of log4j:event
30  * elements as defined in the <a
31  * HREF="doc-files/log4j.dtd">log4j.dtd</a>. It does not output a
32  * complete well-formed XML file. The output is designed to be
33  * included as an <em>external entity</em> in a separate file to form
34  * a correct XML file.
35  *
36  * <p>For example, if <code>abc</code> is the name of the file where
37  * the XMLLayout ouput goes, then a well-formed XML file would be:
38  *
39   <pre>
40    &lt;?xml version="1.0" ?&gt;
41  
42   &lt;!DOCTYPE log4j:eventSet SYSTEM "log4j.dtd" [&lt;!ENTITY data SYSTEM "abc"&gt;]&gt;
43  
44   &lt;log4j:eventSet version="1.2" xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
45     &nbsp;&nbsp;&data;
46   &lt;/log4j:eventSet&gt;
47   </pre>
48  
49  * <p>This approach enforces the independence of the XMLLayout and the
50  * appender where it is embedded.
51  *
52  * <p>The <code>version</code> attribute helps components to correctly
53  * intrepret output generated by XMLLayout. The value of this
54  * attribute should be "1.1" for output generated by log4j versions
55  * prior to log4j 1.2 (final release) and "1.2" for relase 1.2 and
56  * later.
57  *
58  * @author Ceki G&uuml;lc&uuml;
59  * @since 0.9.0
60  * */

61 public class XMLLayout extends Layout {
62
63   private final int DEFAULT_SIZE = 256;
64   private final int UPPER_LIMIT = 2048;
65
66   private StringBuffer JavaDoc buf = new StringBuffer JavaDoc(DEFAULT_SIZE);
67   private boolean locationInfo = false;
68  
69   /**
70    * The <b>LocationInfo</b> option takes a boolean value. By default,
71    * it is set to false which means there will be no location
72    * information output by this layout. If the the option is set to
73    * true, then the file name and line number of the statement at the
74    * origin of the log statement will be output.
75    *
76    * <p>If you are embedding this layout within an {@link
77    * org.apache.log4j.net.SMTPAppender} then make sure to set the
78    * <b>LocationInfo</b> option of that appender as well.
79    * */

80   public void setLocationInfo(boolean flag) {
81     locationInfo = flag;
82   }
83   
84   /**
85      Returns the current value of the <b>LocationInfo</b> option.
86    */

87   public boolean getLocationInfo() {
88     return locationInfo;
89   }
90   
91   /** No options to activate. */
92   public void activateOptions() {
93   }
94
95
96   /**
97    * Formats a {@link LoggingEvent} in conformance with the log4j.dtd.
98    * */

99   public String JavaDoc format(LoggingEvent event) {
100
101     // Reset working buffer. If the buffer is too large, then we need a new
102
// one in order to avoid the penalty of creating a large array.
103
if(buf.capacity() > UPPER_LIMIT) {
104       buf = new StringBuffer JavaDoc(DEFAULT_SIZE);
105     } else {
106       buf.setLength(0);
107     }
108     
109     // We yield to the \r\n heresy.
110

111     buf.append("<log4j:event logger=\"");
112     buf.append(event.getLoggerName());
113     buf.append("\" timestamp=\"");
114     buf.append(event.timeStamp);
115     buf.append("\" level=\"");
116     buf.append(event.getLevel());
117     buf.append("\" thread=\"");
118     buf.append(event.getThreadName());
119     buf.append("\">\r\n");
120
121     buf.append("<log4j:message><![CDATA[");
122     // Append the rendered message. Also make sure to escape any
123
// existing CDATA sections.
124
Transform.appendEscapingCDATA(buf, event.getRenderedMessage());
125     buf.append("]]></log4j:message>\r\n");
126     
127     String JavaDoc ndc = event.getNDC();
128     if(ndc != null) {
129       buf.append("<log4j:NDC><![CDATA[");
130       buf.append(ndc);
131       buf.append("]]></log4j:NDC>\r\n");
132     }
133     
134     String JavaDoc[] s = event.getThrowableStrRep();
135     if(s != null) {
136       buf.append("<log4j:throwable><![CDATA[");
137       for(int i = 0; i < s.length; i++) {
138     buf.append(s[i]);
139     buf.append("\r\n");
140       }
141       buf.append("]]></log4j:throwable>\r\n");
142     }
143     
144     if(locationInfo) {
145       LocationInfo locationInfo = event.getLocationInformation();
146       buf.append("<log4j:locationInfo class=\"");
147       buf.append(locationInfo.getClassName());
148       buf.append("\" method=\"");
149       buf.append(Transform.escapeTags(locationInfo.getMethodName()));
150       buf.append("\" file=\"");
151       buf.append(locationInfo.getFileName());
152       buf.append("\" line=\"");
153       buf.append(locationInfo.getLineNumber());
154       buf.append("\"/>\r\n");
155     }
156     
157     buf.append("</log4j:event>\r\n\r\n");
158     
159     return buf.toString();
160   }
161   
162   /**
163      The XMLLayout prints and does not ignore exceptions. Hence the
164      return value <code>false</code>.
165   */

166   public boolean ignoresThrowable() {
167     return false;
168   }
169 }
170
Popular Tags