From 4d60f6a0ac3c6a1b15e73055049bcef1e59af8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20J=C3=A4ckle?= Date: Mon, 11 Sep 2023 14:27:54 +0200 Subject: [PATCH] add IntLevelJsonProvider as logstash/logback provider, adding the log level as "intLevel" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Jäckle --- base/service/pom.xml | 5 ++ .../service/logging/IntLevelJsonProvider.java | 76 +++++++++++++++++++ .../ditto/logback-config/connectivity.xml | 1 + .../helm/ditto/logback-config/gateway.xml | 1 + .../helm/ditto/logback-config/policies.xml | 1 + .../helm/ditto/logback-config/things.xml | 1 + .../ditto/logback-config/thingssearch.xml | 1 + 7 files changed, 86 insertions(+) create mode 100644 base/service/src/main/java/org/eclipse/ditto/base/service/logging/IntLevelJsonProvider.java diff --git a/base/service/pom.xml b/base/service/pom.xml index 13254eebca..566bbb9f20 100644 --- a/base/service/pom.xml +++ b/base/service/pom.xml @@ -67,6 +67,11 @@ ch.qos.logback logback-classic + + net.logstash.logback + logstash-logback-encoder + provided + org.apache.pekko pekko-actor_${scala.version} diff --git a/base/service/src/main/java/org/eclipse/ditto/base/service/logging/IntLevelJsonProvider.java b/base/service/src/main/java/org/eclipse/ditto/base/service/logging/IntLevelJsonProvider.java new file mode 100644 index 0000000000..d88d7cb1e9 --- /dev/null +++ b/base/service/src/main/java/org/eclipse/ditto/base/service/logging/IntLevelJsonProvider.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.ditto.base.service.logging; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import net.logstash.logback.composite.AbstractFieldJsonProvider; +import net.logstash.logback.composite.JsonWritingUtils; + +/** + * Logstash logback provider for providing a field {@code intLevel} for each log entry with the values, depending on + * the log level, being: + *
    + *
  • TRACE: 1
  • + *
  • DEBUG: 2
  • + *
  • INFO: 3
  • + *
  • WARN: 4
  • + *
  • ERROR: 5
  • + *
+ * To be used in {@code logback.xml} as: + *
{@code
+ * 
+ *    
+ * 
+ * }
+ * 
+ * + * @since 3.4.0 + */ +public final class IntLevelJsonProvider extends AbstractFieldJsonProvider { + + @Override + public void writeTo(final JsonGenerator generator, final ILoggingEvent event) throws IOException { + JsonWritingUtils.writeNumberField( + generator, + "intLevel", + mapFromLevelToIntLevel(event.getLevel()) + ); + } + + private int mapFromLevelToIntLevel(final Level level) { + if (level.equals(Level.OFF)) { + return 0; + } else if (level.equals(Level.TRACE)) { + return 1; + } else if (level.equals(Level.DEBUG)) { + return 2; + } else if (level.equals(Level.INFO)) { + return 3; + } else if (level.equals(Level.WARN)) { + return 4; + } else if (level.equals(Level.ERROR)) { + return 5; + } else if (level.equals(Level.ALL)) { + // should not be able to happen for a single log entry: + return Integer.MAX_VALUE; + } else { + // should not be able to happen at all: + return Integer.MIN_VALUE; + } + } +} diff --git a/deployment/helm/ditto/logback-config/connectivity.xml b/deployment/helm/ditto/logback-config/connectivity.xml index 8ba84dfc3c..8382e42e0d 100644 --- a/deployment/helm/ditto/logback-config/connectivity.xml +++ b/deployment/helm/ditto/logback-config/connectivity.xml @@ -14,6 +14,7 @@ + sourceActorSystem pekkoUid pekkoTimestamp diff --git a/deployment/helm/ditto/logback-config/gateway.xml b/deployment/helm/ditto/logback-config/gateway.xml index 415117b2cf..8209710dc6 100755 --- a/deployment/helm/ditto/logback-config/gateway.xml +++ b/deployment/helm/ditto/logback-config/gateway.xml @@ -14,6 +14,7 @@ + sourceActorSystem pekkoUid pekkoTimestamp diff --git a/deployment/helm/ditto/logback-config/policies.xml b/deployment/helm/ditto/logback-config/policies.xml index 415117b2cf..8209710dc6 100755 --- a/deployment/helm/ditto/logback-config/policies.xml +++ b/deployment/helm/ditto/logback-config/policies.xml @@ -14,6 +14,7 @@ + sourceActorSystem pekkoUid pekkoTimestamp diff --git a/deployment/helm/ditto/logback-config/things.xml b/deployment/helm/ditto/logback-config/things.xml index 415117b2cf..8209710dc6 100755 --- a/deployment/helm/ditto/logback-config/things.xml +++ b/deployment/helm/ditto/logback-config/things.xml @@ -14,6 +14,7 @@ + sourceActorSystem pekkoUid pekkoTimestamp diff --git a/deployment/helm/ditto/logback-config/thingssearch.xml b/deployment/helm/ditto/logback-config/thingssearch.xml index 415117b2cf..8209710dc6 100755 --- a/deployment/helm/ditto/logback-config/thingssearch.xml +++ b/deployment/helm/ditto/logback-config/thingssearch.xml @@ -14,6 +14,7 @@ + sourceActorSystem pekkoUid pekkoTimestamp