title: Programmatically configure LogBack. Again
body: <p>in my android program I use slf4j-logback. with xml-configuration all work fine:</p>
  
  <pre><code>&lt;configuration&gt;
  &lt;property name="LOG_DIR" value="/sdcard/Results/.logs" /&gt;
  &lt;appender
          name="LOGCAT"
          class="ch.qos.logback.classic.android.LogcatAppender" &gt;
      &lt;tagEncoder&gt;
          &lt;pattern&gt;akrit&lt;/pattern&gt;
      &lt;/tagEncoder&gt;
      &lt;encoder&gt;
          &lt;pattern&gt;[%class{0}][%thread] %msg%n&lt;/pattern&gt;
      &lt;/encoder&gt;
  &lt;/appender&gt;
  
  &lt;appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
      &lt;file&gt;${LOG_DIR}/log.txt&lt;/file&gt;
      &lt;append&gt;true&lt;/append&gt;
      &lt;encoder&gt;
          &lt;pattern&gt;%date{ISO8601} [%thread] %-5level %logger{0} %msg%n&lt;/pattern&gt;
      &lt;/encoder&gt;
      &lt;rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"&gt;
          &lt;fileNamePattern&gt;${LOG_DIR}/log.%d.txt&lt;/fileNamePattern&gt;
          &lt;maxHistory&gt;14&lt;/maxHistory&gt;
      &lt;/rollingPolicy&gt;
  &lt;/appender&gt;
  
  &lt;root level="DEBUG" &gt;
      &lt;appender-ref ref="LOGCAT" /&gt;
      &lt;appender-ref ref="FILE" /&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p></p>
  
  <p>but the same code-logic, configured programmatically, dont work - logcat and logfile are empty:</p>
  
  <pre><code>package projects.common.logger;
  
  import org.slf4j.LoggerFactory;
  import ch.qos.logback.classic.Level;
  import ch.qos.logback.classic.Logger;
  import ch.qos.logback.classic.LoggerContext;
  import ch.qos.logback.classic.android.LogcatAppender;
  import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
  import ch.qos.logback.classic.spi.ILoggingEvent;
  import ch.qos.logback.core.rolling.RollingFileAppender;
  import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
  public class LogBack {
      private static boolean debugMode = true;
      private static String logPath = null;
      public static Logger getLogger(Class&lt;?&gt; _clazz) {
          //return getLoggerXML(_clazz);
          return configureLogbackDirectly(_clazz, debugMode, logPath);
      }
      public static Logger getLoggerXML(Class&lt;?&gt; _clazz) {
          return (Logger) LoggerFactory.getLogger(_clazz);
      }
      public static void setDebugMode(boolean _debugMode) {
          debugMode = _debugMode;
      }
      public static void setLogPath(String _logPath) {
          logPath = _logPath;
      }
      private static Logger configureLogbackDirectly(Class&lt;?&gt; _clazz, boolean _debug, String _logPath) {
          LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
          context.reset();
          Logger root = (Logger) LoggerFactory.getLogger(_clazz);
          root.setLevel(Level.ERROR);
          if (_debug)
              root.setLevel(Level.DEBUG);
          root.addAppender(logCat(context));
          if (_logPath != null)
              root.addAppender(logFile(context, _logPath));
          return root;
      }
      private static RollingFileAppender&lt;ILoggingEvent&gt; logFile(LoggerContext _context, String _filePath) {
          RollingFileAppender&lt;ILoggingEvent&gt; rollingFileAppender = new RollingFileAppender&lt;ILoggingEvent&gt;();
          rollingFileAppender.setAppend(true);
          rollingFileAppender.setContext(_context);
          rollingFileAppender.setFile(_filePath + "/log.txt");
          TimeBasedRollingPolicy&lt;ILoggingEvent&gt; rollingPolicy = new TimeBasedRollingPolicy&lt;ILoggingEvent&gt;();
          rollingPolicy.setFileNamePattern(_filePath + "/log.%d.txt");
          rollingPolicy.setMaxHistory(7);
          rollingPolicy.setParent(rollingFileAppender);
          rollingPolicy.setContext(_context);
          rollingPolicy.start();
          rollingFileAppender.setRollingPolicy(rollingPolicy);
          PatternLayoutEncoder encoder = new PatternLayoutEncoder();
          encoder.setPattern("%date{ISO8601} [%thread] %-5level %logger{0} %msg%n");
          encoder.setContext(_context);
          encoder.start();
          rollingFileAppender.setEncoder(encoder);
          rollingFileAppender.setName("rollingFileAppender");
          rollingFileAppender.start();
          return rollingFileAppender;
      }
      private static LogcatAppender logCat(LoggerContext _context) {
          PatternLayoutEncoder encoder = new PatternLayoutEncoder();
          encoder.setContext(_context);
          encoder.setPattern("[%thread] %msg%n");
          encoder.start();
          PatternLayoutEncoder tag_encoder = new PatternLayoutEncoder();
          tag_encoder.setContext(_context);
          tag_encoder.setPattern("%class{0}");
          tag_encoder.start();
          LogcatAppender logcatAppender = new LogcatAppender();
          logcatAppender.setContext(_context);
          logcatAppender.setEncoder(encoder);
          logcatAppender.setTagEncoder(tag_encoder);
          logcatAppender.setName("logcatAppender");
          logcatAppender.start();
          return logcatAppender;
      }
  }
  </code></pre>
  
  <p>i.e. if active line "return getLoggerXML(_clazz);" - all fine, 
  "return configureLogbackDirectly(_clazz, debugMode, logPath);" - nothing.
  whats wrong?</p>