Skip to content

Commit

Permalink
slf4j2-bridge - annotations for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Oct 24, 2024
1 parent f58de59 commit 869db6f
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 179 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ lazy val slf4j2Bridge = project
.settings(enableZIO())
.settings(mimaSettings(failOnProblem = true))
.settings(
compileOrder := CompileOrder.JavaThenScala,
compileOrder := CompileOrder.ScalaThenJava,
javacOptions := jpmsOverwriteModulePath((Compile / dependencyClasspath).value.map(_.data))(javacOptions.value),
javaOptions := jpmsOverwriteModulePath((Compile / dependencyClasspath).value.map(_.data))(javaOptions.value),
Compile / doc / sources := Seq.empty // module-info.java compilation issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import zio.logging.slf4j.bridge.LoggerData
import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters._

class ZioLoggerFactory extends ILoggerFactory {
final class ZioLoggerFactory extends ILoggerFactory {
private var runtime: LoggerRuntime = null
private val loggers = new ConcurrentHashMap[String, Logger]().asScala

Expand Down
101 changes: 0 additions & 101 deletions slf4j2-bridge/src/main/java/zio/logging/slf4j/bridge/Logger.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getRequestedApiVersion() {
@Override
public void initialize() {
markerFactory = new BasicMarkerFactory();
loggerFactory = new LoggerFactory();
loggerFactory = new ZioLoggerFactory();
mdcAdapter = new BasicMDCAdapter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zio.logging.slf4j.bridge;
package zio.logging.slf4j.bridge

import org.slf4j.event.KeyValuePair;
import org.slf4j.event.Level;
final case class LoggerData(name: String) {

import java.util.List;
lazy val annotations: Map[String, String] = Map(zio.logging.loggerNameAnnotationKey -> name)

interface LoggerRuntime {
void log(String name, Level level, String messagePattern, Object[] arguments, Throwable throwable, List<KeyValuePair> keyValues);

boolean isEnabled(String name, Level level);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio.logging.slf4j.bridge

import org.slf4j.event.{ KeyValuePair, Level }

trait LoggerRuntime {

def log(
logger: LoggerData,
level: Level,
messagePattern: String,
arguments: Array[AnyRef],
throwable: Throwable,
keyValues: java.util.List[KeyValuePair]
): Unit

def isEnabled(logger: LoggerData, level: Level): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ object Slf4jBridge {
for {
runtime <- ZIO.runtime[Any]
_ <- initLock.withPermit {
ZIO.succeed(
org.slf4j.LoggerFactory
.getILoggerFactory()
.asInstanceOf[LoggerFactory]
.attachRuntime(new ZioLoggerRuntime(runtime, filter))
)
ZIO.succeed(ZioLoggerFactory.initialize(new ZioLoggerRuntime(runtime, filter)))
}
} yield ()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package zio.logging.slf4j.bridge

import org.slf4j.Marker
import org.slf4j.event.{ KeyValuePair, Level, LoggingEvent }
import org.slf4j.helpers.AbstractLogger
import org.slf4j.spi.LoggingEventAware

import java.util.Collections;

final class ZioLogger private[bridge] (name: String, factory: ZioLoggerFactory)
extends AbstractLogger
with LoggingEventAware {

private val data: LoggerData = LoggerData(name)

override def getName: String = name

override protected def getFullyQualifiedCallerName: String = null

override protected def handleNormalizedLoggingCall(
level: Level,
marker: Marker,
messagePattern: String,
arguments: Array[AnyRef],
throwable: Throwable
): Unit =
factory.log(data, level, messagePattern, arguments, throwable, Collections.emptyList[KeyValuePair]())

override def isTraceEnabled: Boolean = factory.isEnabled(data, Level.TRACE)

override def isTraceEnabled(marker: Marker): Boolean = isTraceEnabled

override def isDebugEnabled: Boolean = factory.isEnabled(data, Level.DEBUG)

override def isDebugEnabled(marker: Marker): Boolean = isDebugEnabled

override def isInfoEnabled: Boolean = factory.isEnabled(data, Level.INFO)

override def isInfoEnabled(marker: Marker): Boolean = isInfoEnabled

override def isWarnEnabled: Boolean = factory.isEnabled(data, Level.WARN)

override def isWarnEnabled(marker: Marker): Boolean = isWarnEnabled

override def isErrorEnabled: Boolean = factory.isEnabled(data, Level.ERROR)

override def isErrorEnabled(marker: Marker): Boolean = isErrorEnabled

override def log(event: LoggingEvent): Unit =
if (factory.isEnabled(data, event.getLevel))
factory.log(
data,
event.getLevel,
event.getMessage,
event.getArgumentArray,
event.getThrowable,
event.getKeyValuePairs
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio.logging.slf4j.bridge

import org.slf4j.event.{ KeyValuePair, Level }
import org.slf4j.{ ILoggerFactory, Logger }

import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters._

final class ZioLoggerFactory extends ILoggerFactory {
private var runtime: LoggerRuntime = null
private val loggers = new ConcurrentHashMap[String, Logger]().asScala

private[bridge] def attachRuntime(runtime: LoggerRuntime): Unit =
this.runtime = runtime

private[bridge] def log(
logger: LoggerData,
level: Level,
messagePattern: String,
arguments: Array[AnyRef],
throwable: Throwable,
keyValues: java.util.List[KeyValuePair]
): Unit =
if (runtime != null) runtime.log(logger, level, messagePattern, arguments, throwable, keyValues)

private[bridge] def isEnabled(logger: LoggerData, level: Level): Boolean =
if (runtime != null) runtime.isEnabled(logger, level) else false

override def getLogger(name: String): Logger =
loggers.getOrElseUpdate(name, new ZioLogger(name, this))
}

object ZioLoggerFactory {

def initialize(runtime: LoggerRuntime): Unit = {
val factory = org.slf4j.LoggerFactory
.getILoggerFactory()
.asInstanceOf[ZioLoggerFactory]

factory.attachRuntime(runtime)
}

}
Loading

0 comments on commit 869db6f

Please sign in to comment.