title: Add Spring Boot Profile to Sleuth/Zipkin logs
body: <p>I'm using these dependencies:</p>
  
  <pre><code>compile 'org.springframework.cloud:spring-cloud-starter-zipkin'
  compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
  compile 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
  </code></pre>
  
  <p>Is there a possibility to add the current active profile(s) to each log line? This would make it possible to filter logs based on the profiles in Splunk/ELK/... </p>
  
  <p>So instead of </p>
  
  <pre><code>2017-03-13 13:38:30.465  INFO [app,,,] 19220 --- [           main] com.company.app.Application    : Started Application in 20.682 seconds (JVM running for 22.166)
  </code></pre>
  
  <p>it should log </p>
  
  <pre><code>2017-03-13 13:38:30.465  INFO [app,,,] [dev] 19220 --- [           main] com.company.app.Application    : Started Application in 20.682 seconds (JVM running for 22.166)
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Based on Marcin's answer, I implemented it as follows:</p>
  
  <p>application.yml</p>
  
  <pre><code>logging:
    pattern:
      level: "%X{profiles} %5p"
  </code></pre>
  
  <p>ProfileLogger.java</p>
  
  <pre><code>public class ProfileLogger implements SpanLogger {
  
      private final Environment environment;
      private final Logger log;
      private final Pattern nameSkipPattern;
  
      @Autowired
      public ProfileLogger(String nameSkipPattern, final Environment environment) {
          this.nameSkipPattern = Pattern.compile(nameSkipPattern);
          this.environment = environment;
          this.log = org.slf4j.LoggerFactory.getLogger(this.getClass());
      }
  
      private void setProfiles() {
          MDC.put("profiles", Arrays.toString(environment.getActiveProfiles()));
      }
  
      @Override
      public void logStartedSpan(Span parent, Span span) {
          setProfiles();
          ...
      }
      ... // (as https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jSpanLogger.java)
  }
  </code></pre>
  
  <p>LogConfig.java</p>
  
  <pre><code>@Configuration
  public class LogConfig {
  
      private final Environment environment;
  
      @Autowired
      public LogConfig(final Environment environment) {
          this.environment = environment;
      }
  
      @Bean
      SpanLogger getLogger() {
          return new ProfileLogger("", environment);
      }
  }
  </code></pre>
  
  <p>This prints logs like the following:</p>
  
  <pre><code>2017-03-13 14:47:02.796   INFO 22481 --- [           main] com.company.app.Application    : Started Application in 16.115 seconds (JVM running for 16.792)
  2017-03-13 14:47:32.684 [localhost, swagger] TRACE 22481 --- [pool-2-thread-1] c.c.app.config.ProfileLogger    : Starting span: [Trace: bfcdd2ce866efbff, Span: bfcdd2ce866efbff, Parent: null, exportable:true]
  </code></pre>
  
  <p>This is already good but not completely what I'm looking for yet.
  I'd like to add the profile from the beginning -> even the "Started Application" should contain the profile - if possible. Secondly, I'd like to move the <code>profiles</code> between <code>INFO</code> and <code>22481</code>. </p>
  
  <p>One more question came up during implementation: In the linked implementation there is this statement:</p>
  
  <pre><code>if (this.log.isTraceEnabled()) {
      this.log.trace(text, span);
  }
  </code></pre>
  
  <p>does that mean you only send traces if log-level is set to TRACE? If so, how could I improve logging to stdout with that approach (given a log-level of debug/info/warn)? I think the log-pattern is overriden by Sleuth/Zipkin upon importing the dependencies and thus, local logging looks the same as tracing. Eventually I'm interested in having the profile displayed in local stdout as well as in Zipkin. </p>
  
  <p><strong>EDIT 2:</strong> With the help of Marcin, I have changed the pattern by introducing a <code>resources/logback-spring.xml</code> file containing these lines:</p>
  
  <pre><code>&lt;springProperty scope="context" name="activeSpringProfiles" source="spring.profiles.active"/&gt;
  &lt;!-- Example for logging into the build folder of your project --&gt;
  &lt;property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${activeSpringProfiles:-}"/&gt;​
  
  &lt;!-- You can override this to have a custom pattern --&gt;
  &lt;property name="CONSOLE_LOG_PATTERN"
  value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) [${activeSpringProfiles:-}] %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/&gt;
  </code></pre>
  
  <p>Note that you have to add a <code>bootstrap.yml</code> file too in order to have the application name correctly displayed. Without a <code>bootstrap.yml</code> file, the above log-pattern just prints "bootstrap" as application name. </p>
  
  <p>The <code>bootstrap.yml</code> just contains </p>
  
  <pre><code>spring:
    application:
      name: app
  </code></pre>
  
  <p>in my case. Everything else is configured in application-[profile].yml</p>
  
  <p>Now everything works as desired:</p>
  
  <pre><code>2017-03-13 15:58:21.291  INFO [app,,,] [localhost,swagger] 27519 --- [           main] com.company.app.keyserver.Application    : Started Application in 17.565 seconds (JVM running for 18.232)
  </code></pre>