KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > format > XMLFormatter


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

17 package org.apache.log.format;
18
19 import java.util.Date JavaDoc;
20 import org.apache.log.LogEvent;
21
22 /**
23  * Basic XML formatter that writes out a basic XML-ified log event.
24  *
25  * Note that this formatter assumes that the category and context
26  * values will produce strings that do not need to be escaped in XML.
27  *
28  * @author Peter Donald
29  */

30 public class XMLFormatter
31     implements Formatter
32 {
33     private static final String JavaDoc EOL = System.getProperty( "line.separator", "\n" );
34
35     //Booleans indicating whether or not we
36
//print out a particular field
37
private boolean m_printTime = true;
38     private boolean m_printRelativeTime = false;
39     private boolean m_printPriority = true;
40     private boolean m_printCategory = true;
41     
42     // This can't be changed to 'true' until the Testcases have been fixed
43
//
44
private boolean m_printContext = false;
45     
46     
47     private boolean m_printMessage = true;
48     private boolean m_printException = true;
49
50     private boolean m_printNumericTime = true;
51
52     /**
53      * Print out time field to log.
54      *
55      * @param printTime true to print time, false otherwise
56      */

57     public void setPrintTime( final boolean printTime )
58     {
59         m_printTime = printTime;
60     }
61
62     /**
63      * Print out relativeTime field to log.
64      *
65      * @param printRelativeTime true to print relativeTime, false otherwise
66      */

67     public void setPrintRelativeTime( final boolean printRelativeTime )
68     {
69         m_printRelativeTime = printRelativeTime;
70     }
71
72     /**
73      * Print out priority field to log.
74      *
75      * @param printPriority true to print priority, false otherwise
76      */

77     public void setPrintPriority( final boolean printPriority )
78     {
79         m_printPriority = printPriority;
80     }
81
82     /**
83      * Print out category field to log.
84      *
85      * @param printCategory true to print category, false otherwise
86      */

87     public void setPrintCategory( final boolean printCategory )
88     {
89         m_printCategory = printCategory;
90     }
91
92     /**
93      * Print out context field to log.
94      *
95      * @param printContext true to print context, false otherwise
96      */

97     public void setPrintContext( final boolean printContext )
98     {
99         m_printContext = printContext;
100     }
101
102     /**
103      * Print out message field to log.
104      *
105      * @param printMessage true to print message, false otherwise
106      */

107     public void setPrintMessage( final boolean printMessage )
108     {
109         m_printMessage = printMessage;
110     }
111
112     /**
113      * Print out exception field to log.
114      *
115      * @param printException true to print exception, false otherwise
116      */

117     public void setPrintException( final boolean printException )
118     {
119         m_printException = printException;
120     }
121
122     /**
123      * Format log event into string.
124      *
125      * @param event the event
126      * @return the formatted string
127      */

128     public String JavaDoc format( final LogEvent event )
129     {
130         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 400 );
131
132         sb.append( "<log-entry>" );
133         sb.append( EOL );
134
135         if( m_printTime )
136         {
137             sb.append( " <time>" );
138
139             if( m_printNumericTime )
140             {
141                 sb.append( event.getTime() );
142             }
143             else
144             {
145                 sb.append( new Date JavaDoc( event.getTime() ) );
146             }
147
148             sb.append( "</time>" );
149             sb.append( EOL );
150         }
151
152         if( m_printRelativeTime )
153         {
154             sb.append( " <relative-time>" );
155             sb.append( event.getRelativeTime() );
156             sb.append( "</relative-time>" );
157             sb.append( EOL );
158         }
159
160         if( m_printPriority )
161         {
162             sb.append( " <priority>" );
163             sb.append( event.getPriority().getName() );
164             sb.append( "</priority>" );
165             sb.append( EOL );
166         }
167
168         if( m_printCategory )
169         {
170             sb.append( " <category>" );
171             sb.append( event.getCategory() );
172             sb.append( "</category>" );
173             sb.append( EOL );
174         }
175
176         if( m_printContext && null != event.getContextMap() )
177         {
178             sb.append( " <context-map>" );
179             sb.append( event.getContextMap() );
180             sb.append( "</context-map>" );
181             sb.append( EOL );
182         }
183
184         if( m_printMessage && null != event.getMessage() )
185         {
186             sb.append( " <message><![CDATA[" );
187             sb.append( event.getMessage() );
188             sb.append( "]]></message>" );
189             sb.append( EOL );
190         }
191
192         if( m_printException && null != event.getThrowable() )
193         {
194             sb.append( " <exception><![CDATA[" );
195             //sb.append( event.getThrowable() );
196
sb.append( "]]></exception>" );
197             sb.append( EOL );
198         }
199
200         sb.append( "</log-entry>" );
201         sb.append( EOL );
202
203         return sb.toString();
204     }
205 }
206
Popular Tags