001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.format;
056:
057: import java.util.Date;
058: import org.apache.log.LogEvent;
059:
060: /**
061: * Basic XML formatter that writes out a basic XML-ified log event.
062: *
063: * Note that this formatter assumes that the category and context
064: * values will produce strings that do not need to be escaped in XML.
065: *
066: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
067: */
068: public class XMLFormatter implements Formatter,
069: org.apache.log.Formatter {
070: private static final String EOL = System.getProperty(
071: "line.separator", "\n");
072:
073: //Booleans indicating whether or not we
074: //print out a particular field
075: private boolean m_printTime = true;
076: private boolean m_printRelativeTime = false;
077: private boolean m_printPriority = true;
078: private boolean m_printCategory = true;
079: private boolean m_printContext = true;
080: private boolean m_printMessage = true;
081: private boolean m_printException = true;
082:
083: private boolean m_printNumericTime = true;
084:
085: /**
086: * Print out time field to log.
087: *
088: * @param printTime true to print time, false otherwise
089: */
090: public void setPrintTime(final boolean printTime) {
091: m_printTime = printTime;
092: }
093:
094: /**
095: * Print out relativeTime field to log.
096: *
097: * @param printRelativeTime true to print relativeTime, false otherwise
098: */
099: public void setPrintRelativeTime(final boolean printRelativeTime) {
100: m_printRelativeTime = printRelativeTime;
101: }
102:
103: /**
104: * Print out priority field to log.
105: *
106: * @param printPriority true to print priority, false otherwise
107: */
108: public void setPrintPriority(final boolean printPriority) {
109: m_printPriority = printPriority;
110: }
111:
112: /**
113: * Print out category field to log.
114: *
115: * @param printCategory true to print category, false otherwise
116: */
117: public void setPrintCategory(final boolean printCategory) {
118: m_printCategory = printCategory;
119: }
120:
121: /**
122: * Print out context field to log.
123: *
124: * @param printContext true to print context, false otherwise
125: */
126: public void setPrintContext(final boolean printContext) {
127: m_printContext = printContext;
128: }
129:
130: /**
131: * Print out message field to log.
132: *
133: * @param printMessage true to print message, false otherwise
134: */
135: public void setPrintMessage(final boolean printMessage) {
136: m_printMessage = printMessage;
137: }
138:
139: /**
140: * Print out exception field to log.
141: *
142: * @param printException true to print exception, false otherwise
143: */
144: public void setPrintException(final boolean printException) {
145: m_printException = printException;
146: }
147:
148: /**
149: * Format log event into string.
150: *
151: * @param event the event
152: * @return the formatted string
153: */
154: public String format(final LogEvent event) {
155: final StringBuffer sb = new StringBuffer(400);
156:
157: sb.append("<log-entry>");
158: sb.append(EOL);
159:
160: if (m_printTime) {
161: sb.append(" <time>");
162:
163: if (m_printNumericTime) {
164: sb.append(event.getTime());
165: } else {
166: sb.append(new Date(event.getTime()));
167: }
168:
169: sb.append("</time>");
170: sb.append(EOL);
171: }
172:
173: if (m_printRelativeTime) {
174: sb.append(" <relative-time>");
175: sb.append(event.getRelativeTime());
176: sb.append("</relative-time>");
177: sb.append(EOL);
178: }
179:
180: if (m_printPriority) {
181: sb.append(" <priority>");
182: sb.append(event.getPriority().getName());
183: sb.append("</priority>");
184: sb.append(EOL);
185: }
186:
187: if (m_printCategory) {
188: sb.append(" <category>");
189: sb.append(event.getCategory());
190: sb.append("</category>");
191: sb.append(EOL);
192: }
193:
194: if (m_printContext && null != event.getContextStack()) {
195: sb.append(" <context-stack>");
196: sb.append(event.getContextStack());
197: sb.append("</context-stack>");
198: sb.append(EOL);
199: }
200:
201: if (m_printMessage && null != event.getMessage()) {
202: sb.append(" <message><![CDATA[");
203: sb.append(event.getMessage());
204: sb.append("]]></message>");
205: sb.append(EOL);
206: }
207:
208: if (m_printException && null != event.getThrowable()) {
209: sb.append(" <exception><![CDATA[");
210: //sb.append( event.getThrowable() );
211: sb.append("]]></exception>");
212: sb.append(EOL);
213: }
214:
215: sb.append("</log-entry>");
216: sb.append(EOL);
217:
218: return sb.toString();
219: }
220: }
|