I'm trying to implement a custom AppenderFactory for Splunk HTTP Event Collector. I wrote a simple class as follows,
package com.example.app;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AppenderBase;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.logging.AbstractAppenderFactory;
import io.dropwizard.logging.async.AsyncAppenderFactory;
import io.dropwizard.logging.filter.LevelFilterFactory;
import io.dropwizard.logging.layout.LayoutFactory;
@JsonTypeName("splunk")
public class SplunkAppenderFactory extends AbstractAppenderFactory{
@Override
public Appender build(LoggerContext context, String applicationName, LayoutFactory layoutFactory, LevelFilterFactory levelFilterFactory, AsyncAppenderFactory asyncAppenderFactory) {
System.out.println("Setting up SplunkAppenderFactory!");
final SplunkAppender appender = new SplunkAppender();
appender.setName("splunk-appender");
appender.setContext(context);
appender.start();
return wrapAsync(appender, asyncAppenderFactory);
}
}
class SplunkAppender extends AppenderBase<ILoggingEvent> {
@Override
protected void append(ILoggingEvent eventObject) {
System.out.println("Splunk: "+ eventObject.toString());
}
}
It is said that we don't have to wire anything, as Dropwizard would scan and wire the stuffs automatically. But when I run the app, I get this errors,
./infrastructure/config/config.yml has an error: * Failed to parse configuration at: logging.appenders.[2]; Could not resolve type id 'splunk' into a subtype of [simple type, class io.dropwizard.logging.AppenderFactory]: known type ids = [AppenderFactory, console, file, syslog] at [Source: N/A; line: -1, column: -1] (through reference chain: com.example.app.AppConfiguration["logging"]->io.dropwizard.logging.DefaultLoggingFactory["appenders"]->java.util.ArrayList[2])
My app.config is as follows,
logging:
appenders:
# log format: <Level> - <Time> - <Revision> - <Environment> - <Thread> - <Log Content>
- type: console
logFormat: "%level %d{HH:mm:ss.SSS} %mdc{revision} %mdc{environment} '%mdc{user}' %t %logger{5} - %X{code} %msg %n"
threshold: ${CONSOLE_LOG_LEVEL:-ERROR}
- type: file
threshold: INFO
logFormat: "%level %d{HH:mm:ss.SSS} %mdc{revision} %mdc{environment} '%mdc{user}' %t %logger{5} - %X{code} %msg %n"
# The file to which current statements will be logged.
currentLogFilename: ./logs/app.log
# When the log file rotates, the archived log will be renamed to this and gzipped. The
# %d is replaced with the previous day (yyyy-MM-dd). Custom rolling windows can be created
# by passing a SimpleDateFormat-compatible format as an argument: "%d{yyyy-MM-dd-hh}".
archivedLogFilenamePattern: ./logs/app-%d.log.gz
# The number of archived files to keep.
archivedFileCount: 10
# The timezone used to format dates. HINT: USE THE DEFAULT, UTC.
timeZone: UTC
- type: splunk
logFormat: "%level %d{HH:mm:ss.SSS} %mdc{revision} %mdc{environment} '%mdc{user}' %t %logger{5} - %X{code} %msg %n"
threshold: INFO
How can i get this work?
You might have to create a file named:
META-INF/services/io.dropwizard.logging.AppenderFactory
In the resources folder of the project where the content of this file is the full qualified name of the Appender class (or classes) available:
com.example.app.SplunkAppenderFactory
The core DW project also includes this file with the default appenders: