[logback-user] Custom appender and layout issue

Adam Gordon adam.n.gordon at gmail.com
Wed Nov 9 21:29:45 CET 2011


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.

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.

Here's my appender XML:

  <appender name="foouilog" class="com.foo.logging.FooRollingFileAppender">
     <file>/usr/local/tomcat/logs/fooui.log</file>
     <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->

<fileNamePattern>/usr/local/tomcat/logs/fooui-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
        <maxHistory>10</maxHistory>
        <timeBasedFileNamingAndTriggeringPolicy
           class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
           <maxFileSize>25MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
     </rollingPolicy>
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <layout class="com.foo.logging.AnsiColorLayout"/>
      <pattern>%date{ISO8601} %p [%logger{5}.%method:%line] -
%msg%n%rEx</pattern>
    </encoder>
  </appender>

And here's the relevant Java code:

Appender:

  public class FooRollingFileAppender extends
RollingFileAppender<ILoggingEvent> {
    *** snip ***
    public void append(ILoggingEvent eventObject) {
      OutputStream os = this.getOutputStream();
      // code to write out color formatted timestamp
      os.flush();

      super.append(eventObject);
    }
    *** snip ***
  }

Layout:

  public class AnsiColorLayout extends LayoutBase<ILoggingEvent> {

    *** snip ***

    public String doLayout(ILoggingEvent event) {
      return this.format(event);
    }

    private String format(ILoggingEvent event) {
      StringBuilder sb = new StringBuilder();
      switch (event.getLevel().toInt()) {

        *** snip ***

      }
      sb.append(event.getFormattedMessage());
      sb.append(COLOR_DEFAULT);
      return sb.toString();
    }

    *** snip ***
  }

Thanks,

--adam

http://gordonizer.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20111109/748ad96c/attachment.html>


More information about the Logback-user mailing list