1 16 17 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 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 buf = new StringBuffer (DEFAULT_SIZE); 67 private boolean locationInfo = false; 68 69 80 public void setLocationInfo(boolean flag) { 81 locationInfo = flag; 82 } 83 84 87 public boolean getLocationInfo() { 88 return locationInfo; 89 } 90 91 92 public void activateOptions() { 93 } 94 95 96 99 public String format(LoggingEvent event) { 100 101 if(buf.capacity() > UPPER_LIMIT) { 104 buf = new StringBuffer (DEFAULT_SIZE); 105 } else { 106 buf.setLength(0); 107 } 108 109 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 Transform.appendEscapingCDATA(buf, event.getRenderedMessage()); 125 buf.append("]]></log4j:message>\r\n"); 126 127 String 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 [] 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 166 public boolean ignoresThrowable() { 167 return false; 168 } 169 } 170 | Popular Tags |