Created
February 27, 2013 13:44
-
-
Save tzachz/5047987 to your computer and use it in GitHub Desktop.
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
import ch.qos.logback.classic.Logger; | |
import ch.qos.logback.classic.LoggerContext; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
import com.yammer.dropwizard.ConfiguredBundle; | |
import com.yammer.dropwizard.config.Bootstrap; | |
import com.yammer.dropwizard.config.Environment; | |
import com.yammer.dropwizard.logging.AsyncAppender; | |
import me.moocar.logbackgelf.GelfAppender; | |
import org.slf4j.LoggerFactory; | |
import java.util.HashMap; | |
public class LogbackConfigurer implements ConfiguredBundle<YourDropwizardConfiguration>{ | |
@Override | |
public void run(YourDropwizardConfiguration configuration, Environment environment) throws Exception { | |
final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); | |
LoggerContext loggerContext = root.getLoggerContext(); | |
GelfAppender<ILoggingEvent> appender = createAppender(loggerContext); | |
// configure logger for the relevant package, and wrap the appender using async decorator | |
loggerContext.getLogger("com.your.package").addAppender(AsyncAppender.wrap(appender)); | |
} | |
@Override | |
public void initialize(Bootstrap<?> bootstrap) { | |
//do nothing | |
} | |
private GelfAppender<ILoggingEvent> createAppender(LoggerContext loggerContext) { | |
GelfAppender<ILoggingEvent> appender = new GelfAppender<ILoggingEvent>(); | |
appender.setContext(loggerContext); | |
appender.setName("GELF"); | |
appender.setFacility("logback-gelf"); | |
appender.setGraylog2ServerHost("your-graylog-host-name"); | |
// GELF lets you add custom fields - you need to configure them here | |
final HashMap<String, String> additionalFields = new HashMap<String, String>(); | |
additionalFields.put("clientIdentifier", "_client_id"); | |
appender.setAdditionalFields(additionalFields); | |
appender.start(); | |
return appender; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment