[logback-user] One Log file per Log Level

Ceki Gulcu ceki at qos.ch
Mon Aug 17 16:49:52 CEST 2009


SiftingAppender could do that pretty elegantly. However, you would need to write 
a discriminator such as:


// I have not tested (not even compiled) this code
public class LevelBasedDiscriminator extends ContextAwareBase implements
     Discriminator<LoggingEvent> {

   private boolean started = false;

   public ContextBasedDiscriminator() {
   }

   public String getDiscriminatingValue(ILoggingEvent event) {
     return event.getLevel().toString();
   }

   public boolean isStarted() {
     return started;
   }

   public void start() {
     started = true;
   }

   public void stop() {
     started = false;
   }

   public String getKey() {
     return "level";
   }

   public void setKey(String key) {
     throw new UnsupportedOperationException("Key cannot be set");
   }

   public String getDefaultValue() {
     return "DEBUG";
   }

   public void setDefaultValue(String defaultValue) {
     throw new UnsupportedOperationException("default value cannot be set.");
   }
}

Here is a sample configuration file:

<configuration>
   <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
     <discriminator  class="some.package.name.LevelBasedDiscriminator"/>
     <sift>
       <appender name="FILE-${level}"
                class="ch.qos.logback.core.FileAppender">
         <File>$level}.log</File>
         <layout>
           <Pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</Pattern>
         </layout>
       </appender>
     </sift>
   </appender>
   <root level="DEBUG">
     <appender-ref ref="SIFT" />
   </root>
</configuration>

If you'd like the LevelBasedDiscriminator to be added to logback please enter a 
jira issue.

HTH

James Apfel wrote:
> Hey,
> 
> I'm wondering if it's possible to configure Logback so that it writes
> individual log files for the various log levels. For example ERROR is
> written to error.txt, WARN to warn.txt and so on...
> 
> Thanks,
>  James

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch


More information about the Logback-user mailing list