-
-
Save nicktelford/dd5e000c3327484540a8 to your computer and use it in GitHub Desktop.
This file contains 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 org.slf4j.LoggerFactory; | |
import ch.qos.logback.classic.Logger; | |
import ch.qos.logback.core.Appender; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
import me.moocar.logbackgelf.GelfAppender; | |
public abstract class GraylogBundle<T extends Configuration> | |
implements ConfiguredBundle<T> { | |
@Override | |
public void initialize(final Bootstrap<?> bootstrap) { | |
final GraylogConfiguration configuration = getConfiguration(); | |
if (configuration.isEnabled()) { | |
// todo: configure your appender here | |
final Appender<ILoggingEvent> appender = new GelfAppender(); | |
appender.setGraylog2ServerHost(configuration.getHost()); | |
appender.setGraylog2ServerPort(configuration.getPort()); | |
appender.setMessagePattern(configuration.getPattern()); | |
final Logger root = (Logger) LoggerFactory.getLogger( | |
org.slf4j.Logger.ROOT_LOGGER_NAME); | |
root.addAppender(appender); | |
} | |
public abstract GraylogConfiguration getConfiguration(T configuration); | |
} | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import org.hibernate.validator.constraints.NotEmpty; | |
import javax.validation.constraints.Max; | |
import javax.validation.constraints.Min; | |
public class GraylogConfiguration { | |
@JsonProperty | |
private boolean enabled = false; | |
@JsonProperty | |
@NotEmpty | |
private String host = "localhost"; | |
@JsonProperty | |
@Min(0) | |
@Max(65535) | |
private int port = 12201; | |
@JsonProperty | |
@NotEmpty | |
private String pattern = "%m%rEx"; | |
public boolean isEnabled() { | |
return enabled; | |
} | |
public String getHost() { | |
return host; | |
} | |
public int getPort() { | |
return port; | |
} | |
public String getPattern() { | |
return pattern; | |
} | |
} | |
// example for using the Bundle, omitting the imports | |
public class HelloWorldService extends Service<HelloWorldConfiguration> { | |
public static void main(String[] args) throws Exception { | |
new HelloWorldService().run(args); | |
} | |
@Override | |
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) { | |
bootstrap.addBundle(new GraylogBundle() { | |
@Override | |
public GraylogConfiguration getConfiguration(HelloWorldConfiguration configuration) { | |
return configuration.getGraylogConfiguration(); | |
} | |
}); | |
} | |
} | |
// service configuration, omitting imports | |
class HelloWorldConfiguration extends Configuration { | |
@JsonProperty | |
@Valid | |
@NotNull | |
private GraylogConfiguration graylog = new GraylogConfiguration(); | |
public GraylogConfiguration getGraylogConfiguration() { | |
return graylog; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment