1 17 package org.apache.log.format; 18 19 import java.util.Date ; 20 import org.apache.log.LogEvent; 21 22 30 public class XMLFormatter 31 implements Formatter 32 { 33 private static final String EOL = System.getProperty( "line.separator", "\n" ); 34 35 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 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 57 public void setPrintTime( final boolean printTime ) 58 { 59 m_printTime = printTime; 60 } 61 62 67 public void setPrintRelativeTime( final boolean printRelativeTime ) 68 { 69 m_printRelativeTime = printRelativeTime; 70 } 71 72 77 public void setPrintPriority( final boolean printPriority ) 78 { 79 m_printPriority = printPriority; 80 } 81 82 87 public void setPrintCategory( final boolean printCategory ) 88 { 89 m_printCategory = printCategory; 90 } 91 92 97 public void setPrintContext( final boolean printContext ) 98 { 99 m_printContext = printContext; 100 } 101 102 107 public void setPrintMessage( final boolean printMessage ) 108 { 109 m_printMessage = printMessage; 110 } 111 112 117 public void setPrintException( final boolean printException ) 118 { 119 m_printException = printException; 120 } 121 122 128 public String format( final LogEvent event ) 129 { 130 final StringBuffer sb = new StringBuffer ( 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 ( 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( "]]></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 |