Skip to content

Instantly share code, notes, and snippets.

@bijukunjummen
Created May 14, 2022 13:07
Show Gist options
  • Save bijukunjummen/450ce16542c257a9b60a58839dbee2fc to your computer and use it in GitHub Desktop.
Save bijukunjummen/450ce16542c257a9b60a58839dbee2fc to your computer and use it in GitHub Desktop.
package org.bk.logback.custom;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.contrib.json.classic.JsonLayout;
import com.google.cloud.logging.Severity;
import java.util.Map;
public class GcpJsonLayout extends JsonLayout {
private static final String SEVERITY_FIELD = "severity";
@Override
protected void addCustomDataToJsonMap(Map<String, Object> map, ILoggingEvent event) {
map.put(SEVERITY_FIELD, severityFor(event.getLevel()));
}
private static Severity severityFor(Level level) {
return switch (level.toInt()) {
// TRACE
case 5000 -> Severity.DEBUG;
// DEBUG
case 10000 -> Severity.DEBUG;
// INFO
case 20000 -> Severity.INFO;
// WARNING
case 30000 -> Severity.WARNING;
// ERROR
case 40000 -> Severity.ERROR;
default -> Severity.DEFAULT;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment