title: SLF4J - Logging to different files based on a tag
body: <p>I have a requirement wherein I need to create log files such that all the logs based on a tag (Preferably text) need to be logged to corresponding log files.</p>
  
  <p>For example,</p>
  
  <p>I have logs regarding apples, oranges, and Mangoes.</p>
  
  <pre><code>logger.info("Apples: They are red in color");
  logger.info("Oranges: They are orange in color");
  logger.info("Mangoes: They are yellowish in color");
  </code></pre>
  
  <p>As per my requirement, the first log should be logged to <strong>Apples.log</strong>, the second to <strong>Oranges.log</strong> and the third to <strong>Mangoes.log</strong></p>
  
  <p>The log files should be created dynamically.</p>
  
  <p>Below shown is my <strong>logback.xml</strong> file</p>
  
  <pre><code>    &lt;statusListener class="ch.qos.logback.core.status.NopStatusListener"/&gt;
      &lt;appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"&gt;
          &lt;encoder&gt;
              &lt;Pattern&gt;
                  %d %-5p - %marker%m%n
              &lt;/Pattern&gt;
          &lt;/encoder&gt;
      &lt;/appender&gt;
    &lt;appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"&gt;
      &lt;discriminator&gt;
        &lt;key&gt;fruitName&lt;/key&gt;
        &lt;defaultValue&gt;Common_logs&lt;/defaultValue&gt;
      &lt;/discriminator&gt;
      &lt;sift&gt;
        &lt;appender name="FILE-${instanceName}" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
          &lt;rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"&gt;  
              &lt;fileNamePattern&gt;./Logs/${fruitName}/${instanceName}.log&lt;/fileNamePattern&gt;
              &lt;maxHistory&gt;50&lt;/maxHistory&gt;
              &lt;cleanHistoryOnStart&gt;false&lt;/cleanHistoryOnStart&gt;
              &lt;timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"&gt;
              &lt;maxFileSize&gt;10MB&lt;/maxFileSize&gt;
              &lt;/timeBasedFileNamingAndTriggeringPolicy&gt;
          &lt;/rollingPolicy&gt;
          &lt;encoder&gt;
              &lt;pattern&gt;%d %-5p - %m%n&lt;/pattern&gt;
          &lt;/encoder&gt;
          &lt;/appender&gt;
      &lt;/sift&gt;
    &lt;/appender&gt;
  
    &lt;logger name="AssetInstanceService" level="info" additivity="false"&gt;
      &lt;appender-ref ref="CONSOLE"/&gt;
      &lt;appender-ref ref="FILE"/&gt;
      &lt;appender-ref ref="SIFT" /&gt;
    &lt;/logger&gt;
  
    &lt;root level="info"&gt;
      &lt;appender-ref ref="SIFT" /&gt;
      &lt;appender-ref ref="CONSOLE"/&gt;
      &lt;appender-ref ref="FILE"/&gt;
    &lt;/root&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Given below is my <strong>LogManager.java</strong> file </p>
  
  <pre><code>package Data;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.slf4j.MDC;
  
  import ch.qos.logback.classic.LoggerContext;
  import ch.qos.logback.classic.joran.JoranConfigurator;
  import ch.qos.logback.core.joran.spi.JoranException;
  
  public class LogManager {
  
      static String configFile = "./logback.xml";
  
      private static Logger _logger = LoggerFactory.getLogger(LogManager.class);
      private static boolean _isInitialized = false;
  
      private LogManager() {
  
      }
  
      public static Logger Instance() {
          if (_logger == null) {
              _logger = LoggerFactory.getLogger(LogManager.class);
          }
          return _logger;
      }
  
      public static Logger Instance(String instanceName) {
          if (!_isInitialized) {
              if (!CommonMethods.CheckFileExist(configFile)) {
                  configFile = "../logback.xml";
                  if (!CommonMethods.CheckFileExist(configFile)) {
                      return null;
                  }
              }
              LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
              JoranConfigurator configurator = new JoranConfigurator();
              lc.reset();
              configurator.setContext(lc);
              try {
                  configurator.doConfigure(configFile);
                  MDC.put("fruitName", instanceName);
              } catch (JoranException e) {
                  System.out.println(e);
              }
              _isInitialized = true;
          }
          if (_logger == null) {
              _logger = LoggerFactory.getLogger(LogManager.class);
          }
          return _logger;
      }
  }
  </code></pre>
  
  <p>Here my logs are created when the jar file is run as I initialize the logger in my main method.</p>
  
  <p>But my need is to have different log files created with different names based on the tag or id in the log .</p>