From ae2cbbb5003237520cbb99660f4da5af13b6ee8e Mon Sep 17 00:00:00 2001 From: Andi Miller Date: Fri, 5 Jul 2024 17:10:16 +0100 Subject: [PATCH] Add Slf4j2 Module This implements the new SLF4JServiceProvider interface, and registers it with a ServiceLoader, as explained here: https://www.slf4j.org/faq.html#changesInVersion200 It depends on the old module and exposes the new interface, and will be picked up first when running on SLF4J versions 2 and above. --- README.md | 4 ++ build.sbt | 4 ++ .../main/java/org/slf4j/impl/OdinBridge.java | 53 +++++++++++++++++++ .../org.slf4j.spi.SLF4JServiceProvider | 1 + 4 files changed, 62 insertions(+) create mode 100644 modules/odin-slf4j2-bridge/src/main/java/org/slf4j/impl/OdinBridge.java create mode 100644 modules/odin-slf4j2-bridge/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider diff --git a/README.md b/README.md index f6636fe..554a80c 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,10 @@ In benchmarks, the bridge performs as well as [Log4Cats] with a [Logback] backen "com.permutive" %% "odin-slf4j-bridge" "" ``` +## [`odin-slf4j2-bridge`] + +A bridge between [Odin] and [Slf4j] versions 2 and above, allowing the dynamic [Odin] logger to be used by default for [Slf4j] log messages. + [`odin-testing`]: odin-testing [`log4cats-odin`]: log4cats-odin [`odin-dynamic`]: odin-dynamic diff --git a/build.sbt b/build.sbt index f55a304..8e996a8 100644 --- a/build.sbt +++ b/build.sbt @@ -37,6 +37,10 @@ lazy val `odin-slf4j-bridge` = module .settings(libraryDependencies += "com.github.valskalla" %% "odin-core" % "0.13.0") .settings(libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.36") +lazy val `odin-slf4j2-bridge` = module + .dependsOn(`odin-slf4j-bridge`) + .settings(libraryDependencies += "org.slf4j" % "slf4j-api" % "2.0.13") + lazy val `odin-slf4j-bridge-benchmarks` = module .enablePlugins(JmhPlugin) .dependsOn(`odin-slf4j-bridge`) diff --git a/modules/odin-slf4j2-bridge/src/main/java/org/slf4j/impl/OdinBridge.java b/modules/odin-slf4j2-bridge/src/main/java/org/slf4j/impl/OdinBridge.java new file mode 100644 index 0000000..56f8004 --- /dev/null +++ b/modules/odin-slf4j2-bridge/src/main/java/org/slf4j/impl/OdinBridge.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022-2024 Permutive Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.slf4j.impl; + +import com.permutive.logging.slf4j.odin.LoggerFactory; +import org.slf4j.ILoggerFactory; +import org.slf4j.IMarkerFactory; +import org.slf4j.helpers.BasicMDCAdapter; +import org.slf4j.spi.MDCAdapter; +import org.slf4j.spi.SLF4JServiceProvider; + + +public class OdinBridge implements SLF4JServiceProvider { + + @Override + public ILoggerFactory getLoggerFactory() { + return new LoggerFactory(); + } + + @Override + public IMarkerFactory getMarkerFactory() { + return null; + } + + @Override + public MDCAdapter getMDCAdapter() { + return new BasicMDCAdapter(); + } + + @Override + public String getRequestedApiVersion() { + return "2.0"; + } + + @Override + public void initialize() { + + } +} diff --git a/modules/odin-slf4j2-bridge/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider b/modules/odin-slf4j2-bridge/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider new file mode 100644 index 0000000..9fee796 --- /dev/null +++ b/modules/odin-slf4j2-bridge/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider @@ -0,0 +1 @@ +org.slf4j.impl.OdinBridge \ No newline at end of file