Dropwizard: Documentation to create custom logging appenders

Created on 16 Jul 2019  路  6Comments  路  Source: dropwizard/dropwizard

There are some place that contains the documentation for create my own logging appender? I saw some examples in the internet, but it is a few difficult to understand.

stale

Most helpful comment

Here is an example of creating a custom console appender to add a header and footer:

@JsonTypeName("my-console")
public class MyConsoleAppender<E extends DeferredProcessingAware> extends ConsoleAppenderFactory<E> {
    @Override
    protected OutputStreamAppender<E> appender(LoggerContext context) {
        final OutputStreamAppender<E> appender = super.appender(context);
        appender.setEncoder(new EncoderBase<E>() {
            @Override
            public byte[] headerBytes() {
                return "this is a header\n".getBytes(StandardCharsets.UTF_8);
            }

            @Override
            public byte[] encode(E event) {
                return event.toString().getBytes(StandardCharsets.UTF_8);
            }

            @Override
            public byte[] footerBytes() {
                return "this is a footer\n".getBytes(StandardCharsets.UTF_8);
            }
        });
        return appender;
    }
}

Then create file: resources/META-INF/services/io.dropwizard.logging.AppenderFactory and add the appender to it:

com.example.MyConsoleAppender

You'll be able to reference it in yaml

level: INFO
appenders:
  - type: my-console

All 6 comments

@danielborges93 What do you want to achieve?

@joschi I whant create new logging appenders to use in my project. For example, an Slack appender, a Github/GitLab/Bitbucket issue creator for error logs, a custom DBAppernder.

What files I should create? What classes I should extend? What interface I should implement? What means the parameters of build method of AbstractAppenderFactory?

Like a setp-by-step tutorial to create new appenders.

Here is an example of creating a custom console appender to add a header and footer:

@JsonTypeName("my-console")
public class MyConsoleAppender<E extends DeferredProcessingAware> extends ConsoleAppenderFactory<E> {
    @Override
    protected OutputStreamAppender<E> appender(LoggerContext context) {
        final OutputStreamAppender<E> appender = super.appender(context);
        appender.setEncoder(new EncoderBase<E>() {
            @Override
            public byte[] headerBytes() {
                return "this is a header\n".getBytes(StandardCharsets.UTF_8);
            }

            @Override
            public byte[] encode(E event) {
                return event.toString().getBytes(StandardCharsets.UTF_8);
            }

            @Override
            public byte[] footerBytes() {
                return "this is a footer\n".getBytes(StandardCharsets.UTF_8);
            }
        });
        return appender;
    }
}

Then create file: resources/META-INF/services/io.dropwizard.logging.AppenderFactory and add the appender to it:

com.example.MyConsoleAppender

You'll be able to reference it in yaml

level: INFO
appenders:
  - type: my-console

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days

@nickbabcock do you have any idea why this wouldn't work? I'm trying to create a stack driver appender that will basically append a traceId to all the logs. This is done via com.google.cloud.logging.TraceLoggingEnhancer

I'm not seeing the trace ID appended to any of the logs

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.Appender;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.cloud.logging.logback.LoggingAppender;
import io.dropwizard.logging.AbstractAppenderFactory;
import io.dropwizard.logging.async.AsyncAppenderFactory;
import io.dropwizard.logging.filter.LevelFilterFactory;
import io.dropwizard.logging.layout.LayoutFactory;

@JsonTypeName("stack-driver-console")
public class StackDriverAppenderFactory extends AbstractAppenderFactory {

  private String appenderName = "stack-driver-console-appender";
  private boolean includeContextName = true;

  @JsonProperty
  public String getName() {
    return this.appenderName;
  }

  @JsonProperty
  public void setName(String name) {
    this.appenderName = name;
  }

  @Override
  public Appender build(
    LoggerContext loggerContext,
    String s,
    LayoutFactory layoutFactory,
    LevelFilterFactory levelFilterFactory,
    AsyncAppenderFactory asyncAppenderFactory) {

    setNeverBlock(true);
    setTimeZone("utc");
    setLogFormat("%-6level [%d{HH:mm:ss.SSS}] [%t] %logger{5} - %X{code} %msg %n");

    LoggingAppender cloudAppender = new LoggingAppender();
    cloudAppender.addEnhancer("com.google.cloud.logging.TraceLoggingEnhancer");
    cloudAppender.addFilter(levelFilterFactory.build(Level.toLevel(getThreshold())));

    cloudAppender.setFlushLevel(Level.WARN);
    cloudAppender.setLog("application.log");

    cloudAppender.setName(appenderName);
    cloudAppender.setContext(loggerContext);
    cloudAppender.start();

    return wrapAsync(cloudAppender, asyncAppenderFactory);
  }
}

This issue is stale because it has been open 90 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.

Was this page helpful?
0 / 5 - 0 ratings