title: Java based Logback Configuration
body: <p>I got stuck with java based logback configuration via beans. As observed, the configuration is not even loading. Would be great if anybody can guide me to make it resolved. 
  Attaching the code below.</p>
  
  <p>public class JsonLogAppender{</p>
  
  <pre><code>@Bean("loggerContext")
  public LoggerContext loggerContext() {
      return (LoggerContext) LoggerFactory.getILoggerFactory();
  }
  
  @Bean
  @DependsOn("loggerContext")
  public static Logger rootLogger(LoggerContext loggerContext) {
      Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
      rootLogger.setLevel(Level.INFO);
      rootLogger.addAppender(consoleAppender(loggerContext, rootLogger));
      rootLogger.addAppender(tcpSocketAppender(loggerContext, rootLogger));
      return rootLogger;
  }
  
  @Bean
  protected static ConsoleAppender&lt;ILoggingEvent&gt; consoleAppender(LoggerContext loggerContext, Logger rootLogger) {
      ConsoleAppender&lt;ILoggingEvent&gt; consoleAppender = new ConsoleAppender&lt;&gt;();
      consoleAppender.setContext(loggerContext);
      consoleAppender.setName(CONSOLE_APPENDER_NAME);
      consoleAppender.setEncoder(patternLayoutEncoder(loggerContext));
      consoleAppender.start();
      return consoleAppender;
  }
  
  @DependsOn("rootLogger")
  public static PatternLayoutEncoder patternLayoutEncoder(LoggerContext loggerContext) {
      PatternLayoutEncoder encoder = new PatternLayoutEncoder();
      encoder.setContext(loggerContext);
      encoder.setPattern(PATTERN);
      encoder.setCharset(Charset.forName(CHARSET));
      encoder.start();
      return encoder;
  }
  
  @Bean
  @Primary
  protected static LogstashTcpSocketAppender tcpSocketAppender(LoggerContext loggerContext, Logger rootLogger) {
      LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender();
      appender.setName(TCP_APPENDER_NAME);
      appender.setContext(loggerContext);
      appender.addDestination(TCP_DESTINATION);
      appender.setEncoder(compositeJsonEncoder(loggerContext, rootLogger));
      return appender;
  }
  
  @Bean
  protected static LoggingEventCompositeJsonEncoder compositeJsonEncoder(LoggerContext loggerContext, Logger rootLogger){
      LoggingEventCompositeJsonEncoder encoder = new LoggingEventCompositeJsonEncoder();
      encoder.setContext(loggerContext);
      encoder.getProviders().addProvider(new MessageJsonProvider());
      encoder.getProviders().addProvider(new StackTraceJsonProvider());
      encoder.getProviders().addProvider(new LogstashMarkersJsonProvider());
      encoder.getProviders().addProvider(new ThreadNameJsonProvider());
      encoder.getProviders().addProvider(new LoggerNameJsonProvider());
      encoder.getProviders().addProvider(new LogLevelJsonProvider());
      encoder.getProviders().addProvider(new ContextJsonProvider&lt;&gt;());
      encoder.getProviders().addProvider(new MdcJsonProvider());
      encoder.getProviders().addProvider(withPattern(PATTERN, new LoggingEventPatternJsonProvider(), loggerContext));
      encoder.start();
      return encoder;
  
  }
  
  private static AbstractPatternJsonProvider&lt;ILoggingEvent&gt; withPattern(
          String pattern,
          AbstractPatternJsonProvider&lt;ILoggingEvent&gt; provider,
          LoggerContext context
          ) {
  
      provider.setContext(context);
      provider.setPattern(pattern);
      return provider;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am looking for only java based configuration not the XML based(I have the solution with XML which is working fine).</p>
  
  <p><strong>Update</strong>
  Resolved the issue, I have missed adding @Configuration, its working now as expected. </p>
  
  <blockquote>
    <p>Not deleteing the thread, as it may be helpul for people looking for java based configuration for the corresponding xml configuration.</p>
  </blockquote>