Skip to content

Instantly share code, notes, and snippets.

@Mistic92
Created September 14, 2017 22:03
Show Gist options
  • Save Mistic92/492bd85202e9d4bf44c1d9801244e751 to your computer and use it in GitHub Desktop.
Save Mistic92/492bd85202e9d4bf44c1d9801244e751 to your computer and use it in GitHub Desktop.
Log4j2 Google Stackdriver appender (using github.com/GoogleCloudPlatform/google-cloud-java)
@Plugin(name = "CloudAppender", category = "Core", printObject = false)
public class CloudAppender extends AbstractAppender {
private final LoggingOptions loggingOptions;
private final List<LoggingEnhancer> enhancers;
private volatile Logging logging;
protected CloudAppender(String name,
Filter filter,
Layout<? extends Serializable> layout,
LoggingOptions options) {
super(name, filter, layout);
loggingOptions = options != null ? options : LoggingOptions.getDefaultInstance();
final List<LoggingEnhancer> enhancers = MonitoredResourceUtil.getResourceEnhancers();
this.enhancers = new LinkedList<>();
this.enhancers.addAll(enhancers);
}
protected CloudAppender(String name,
Filter filter,
Layout<? extends Serializable> layout,
boolean ignoreExceptions,
LoggingOptions options) {
super(name, filter, layout, ignoreExceptions);
loggingOptions = options != null ? options : LoggingOptions.getDefaultInstance();
final List<LoggingEnhancer> enhancers = MonitoredResourceUtil.getResourceEnhancers();
this.enhancers = new LinkedList<>();
this.enhancers.addAll(enhancers);
}
private static Severity severityFor(Level level) {
if (level.intLevel() == DEBUG.intLevel()) {
return Severity.DEBUG;
} else if (level.intLevel() == INFO.intLevel()) {
return Severity.INFO;
} else if (level.intLevel() == WARN.intLevel()) {
return Severity.WARNING;
} else if (level.intLevel() == ERROR.intLevel()) {
return Severity.ERROR;
} else {
return Severity.DEFAULT;
}
}
@PluginFactory
public static CloudAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter,
@PluginAttribute("otherAttribute") String otherAttribute) {
if (name == null) {
LOGGER.error("No name provided for CloudAppender");
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
return new CloudAppender(name, filter, layout, false, null);
}
@Override
public void append(LogEvent event) {
final LogEntry logEntry = logEntryFor(event);
getLogging().write(ImmutableList.of(logEntry));
}
private LogEntry logEntryFor(LogEvent event) {
final String payload = event.getMessage().getFormattedMessage();
final Level level = event.getLevel();
LogEntry.Builder builder =
LogEntry.newBuilder(Payload.StringPayload.of(payload))
.setTimestamp(event.getTimeMillis())
.setResource(MonitoredResource.newBuilder("global")
.addLabel("project_id", loggingOptions.getProjectId())
.build())
.setSeverity(severityFor(level))
.setLogName("blenge-vision")
.addLabel("levelName", level.name())
.addLabel("levelValue", String.valueOf(level.intLevel()));
for (LoggingEnhancer enhancer : enhancers) {
enhancer.enhanceLogEntry(builder);
}
return builder.build();
}
/**
* Returns an instance of the logging service.
*/
private Logging getLogging() {
if (logging == null) {
synchronized (this) {
if (logging == null) {
logging = loggingOptions.getService();
}
}
}
return logging;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment