<font face="'courier new', monospace">I'm trying to port my log4j appender and layout over to logback but am running into some issues. My log4j appender colorizes the timestamp printed in the logs and the layout colorizes the log messages based on level - at least, in theory.<br>
<br></font><div><font face="'courier new', monospace">I think I've correctly configured everything in the XML but my classes are not quite correct. I'm getting the colored timestamp and I'm partially getting the colorized log messages, but am not getting the pattern I've specified in my XML and the log messages are all smushed together back to back. I suspect I'm not overriding the right methods or not calling them in the right order or place.<br>
<br></font></div><div><font face="'courier new', monospace">Here's my appender XML:<br><br></font></div><div><font face="'courier new', monospace"> <appender name="foouilog" class="com.foo.logging.FooRollingFileAppender"><br>
<file>/usr/local/tomcat/logs/fooui.log</file><br> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><br> <!-- rollover daily --><br> <fileNamePattern>/usr/local/tomcat/logs/fooui-%d{yyyy-MM-dd}.%i.gz</fileNamePattern><br>
<maxHistory>10</maxHistory><br> <timeBasedFileNamingAndTriggeringPolicy<br> class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><br> <maxFileSize>25MB</maxFileSize><br>
</timeBasedFileNamingAndTriggeringPolicy><br> </rollingPolicy><br> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"><br> <layout class="com.foo.logging.AnsiColorLayout"/><br>
<pattern>%date{ISO8601} %p [%logger{5}.%method:%line] - %msg%n%rEx</pattern><br> </encoder><br> </appender><br> <br>And here's the relevant Java code:<br><br></font></div><div><font face="'courier new', monospace">Appender:<br>
<br></font></div><div><font face="'courier new', monospace"> public class FooRollingFileAppender extends RollingFileAppender<ILoggingEvent> {<br> *** snip ***<br> public void append(ILoggingEvent eventObject) {<br>
OutputStream os = this.getOutputStream();<br> // code to write out color formatted timestamp<br> os.flush();<br> <br> super.append(eventObject);<br> }<br> *** snip ***<br> }<br> <br>Layout:<br>
<br></font></div><div><font face="'courier new', monospace"> public class AnsiColorLayout extends LayoutBase<ILoggingEvent> {<br> <br> *** snip ***<br> <br> public String doLayout(ILoggingEvent event) {<br>
return this.format(event);<br> }</font></div><div><font face="'courier new', monospace"><br> private String format(ILoggingEvent event) {<br> StringBuilder sb = new StringBuilder();<br> switch (event.getLevel().toInt()) {</font></div>
<div><font face="'courier new', monospace"><br> *** snip ***<br> <br> }<br> sb.append(event.getFormattedMessage());<br> sb.append(COLOR_DEFAULT);<br> return sb.toString();<br> }<br>
<br> *** snip ***<br> }<br><br>Thanks,<br><br>--adam<br><br><a href="http://gordonizer.com">http://gordonizer.com</a><br></font><br></div>