Last active
October 16, 2017 17:57
-
-
Save laur89/bef122a936996d83554353e0601916ae to your computer and use it in GitHub Desktop.
Increase logging level upon kafka consumers initialisation to decrease log chatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.your.project.config; | |
import org.apache.kafka.clients.consumer.ConsumerConfig; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder; | |
import org.springframework.cloud.stream.binding.InputBindingLifecycle; | |
import org.springframework.context.SmartLifecycle; | |
import org.springframework.context.annotation.Configuration; | |
import ch.qos.logback.classic.Level; | |
import ch.qos.logback.classic.Logger; | |
/** | |
* Increases kafka {@link ConsumerConfig} logging level *after* | |
* the consumers are started by {@link KafkaMessageChannelBinder}, so we | |
* get the consumerConfig in logs only on application startup, not on | |
* each call to /health. | |
*/ | |
@Configuration | |
public class KafkaConsumerConfig implements SmartLifecycle { | |
private volatile boolean running = false; | |
@Override | |
public boolean isAutoStartup() { | |
return true; | |
} | |
@Override | |
public void stop( final Runnable callback ) { | |
stop(); | |
if ( callback != null ) { | |
callback.run(); | |
} | |
} | |
@Override | |
public void start() { | |
Logger rootLogger = (Logger) LoggerFactory.getLogger( ConsumerConfig.class.getName() ); | |
rootLogger.setLevel( Level.WARN ); | |
this.running = true; | |
} | |
@Override | |
public void stop() { | |
this.running = false; | |
} | |
@Override | |
public boolean isRunning() { | |
return running; | |
} | |
/** | |
* Phase needs to be higher than that of {@link InputBindingLifecycle}'s | |
* to ensure this class starts after {@link KafkaMessageChannelBinder}. | |
*/ | |
@Override | |
public int getPhase() { | |
return Integer.MAX_VALUE - 999; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment