Skip to content

Commit

Permalink
add IntLevelJsonProvider as logstash/logback provider, adding the log…
Browse files Browse the repository at this point in the history
… level as "intLevel"

Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Sep 18, 2023
1 parent 88d7fa8 commit 4d60f6a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<scope>provided</scope> <!-- provided by actual service -->
</dependency>
<dependency>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-actor_${scala.version}</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
* <ul>
* <li>TRACE: 1</li>
* <li>DEBUG: 2</li>
* <li>INFO: 3</li>
* <li>WARN: 4</li>
* <li>ERROR: 5</li>
* </ul>
* To be used in {@code logback.xml} as:
* <pre>{@code
* <encoder class="net.logstash.logback.encoder.LogstashEncoder">
* <provider class="org.eclipse.ditto.base.service.logging.IntLevelJsonProvider"/>
* </encoder>
* }
* </pre>
*
* @since 3.4.0
*/
public final class IntLevelJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> {

@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;
}
}
}
1 change: 1 addition & 0 deletions deployment/helm/ditto/logback-config/connectivity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<provider class="org.eclipse.ditto.base.service.logging.IntLevelJsonProvider"/>
<excludeMdcKeyName>sourceActorSystem</excludeMdcKeyName>
<excludeMdcKeyName>pekkoUid</excludeMdcKeyName>
<excludeMdcKeyName>pekkoTimestamp</excludeMdcKeyName>
Expand Down
1 change: 1 addition & 0 deletions deployment/helm/ditto/logback-config/gateway.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<provider class="org.eclipse.ditto.base.service.logging.IntLevelJsonProvider"/>
<excludeMdcKeyName>sourceActorSystem</excludeMdcKeyName>
<excludeMdcKeyName>pekkoUid</excludeMdcKeyName>
<excludeMdcKeyName>pekkoTimestamp</excludeMdcKeyName>
Expand Down
1 change: 1 addition & 0 deletions deployment/helm/ditto/logback-config/policies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<provider class="org.eclipse.ditto.base.service.logging.IntLevelJsonProvider"/>
<excludeMdcKeyName>sourceActorSystem</excludeMdcKeyName>
<excludeMdcKeyName>pekkoUid</excludeMdcKeyName>
<excludeMdcKeyName>pekkoTimestamp</excludeMdcKeyName>
Expand Down
1 change: 1 addition & 0 deletions deployment/helm/ditto/logback-config/things.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<provider class="org.eclipse.ditto.base.service.logging.IntLevelJsonProvider"/>
<excludeMdcKeyName>sourceActorSystem</excludeMdcKeyName>
<excludeMdcKeyName>pekkoUid</excludeMdcKeyName>
<excludeMdcKeyName>pekkoTimestamp</excludeMdcKeyName>
Expand Down
1 change: 1 addition & 0 deletions deployment/helm/ditto/logback-config/thingssearch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<provider class="org.eclipse.ditto.base.service.logging.IntLevelJsonProvider"/>
<excludeMdcKeyName>sourceActorSystem</excludeMdcKeyName>
<excludeMdcKeyName>pekkoUid</excludeMdcKeyName>
<excludeMdcKeyName>pekkoTimestamp</excludeMdcKeyName>
Expand Down

0 comments on commit 4d60f6a

Please sign in to comment.