-
Notifications
You must be signed in to change notification settings - Fork 31
fix(rt): slf4j 1.x compatability #963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1b90cab
7fba07f
eb754d3
870630a
079348f
0e66d4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "id": "d7863388-cf31-44d7-b613-c2376e853b97", | ||
| "type": "bugfix", | ||
| "description": "Provide SLF4J 1.x compatible fallback implementation", | ||
| "issues": [ | ||
| "awslabs/aws-sdk-kotlin#993" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package aws.smithy.kotlin.runtime.telemetry.logging.slf4j | ||
|
|
||
| import aws.smithy.kotlin.runtime.telemetry.logging.LogLevel | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.Logger | ||
|
|
||
| /** | ||
| * Common functionality across SLF4J 1.x and 2.x | ||
| * @param delegate the underlying slf4j logger instance | ||
| */ | ||
| internal abstract class AbstractSlf4jLoggerAdapter(protected val delegate: org.slf4j.Logger) : Logger { | ||
| override fun trace(t: Throwable?, msg: () -> String) { | ||
| if (!isEnabledFor(LogLevel.Trace)) return | ||
| if (t != null) { | ||
| delegate.trace(msg(), t) | ||
| } else { | ||
| delegate.trace(msg()) | ||
| } | ||
| } | ||
| override fun debug(t: Throwable?, msg: () -> String) { | ||
| if (!isEnabledFor(LogLevel.Debug)) return | ||
| if (t != null) { | ||
| delegate.debug(msg(), t) | ||
| } else { | ||
| delegate.debug(msg()) | ||
| } | ||
| } | ||
| override fun info(t: Throwable?, msg: () -> String) { | ||
| if (!isEnabledFor(LogLevel.Info)) return | ||
| if (t != null) { | ||
| delegate.info(msg(), t) | ||
| } else { | ||
| delegate.info(msg()) | ||
| } | ||
| } | ||
| override fun warn(t: Throwable?, msg: () -> String) { | ||
| if (!isEnabledFor(LogLevel.Warning)) return | ||
| if (t != null) { | ||
| delegate.warn(msg(), t) | ||
| } else { | ||
| delegate.warn(msg()) | ||
| } | ||
| } | ||
| override fun error(t: Throwable?, msg: () -> String) { | ||
| if (!isEnabledFor(LogLevel.Error)) return | ||
| if (t != null) { | ||
| delegate.error(msg(), t) | ||
| } else { | ||
| delegate.error(msg()) | ||
| } | ||
| } | ||
| override fun isEnabledFor(level: LogLevel): Boolean = when (level) { | ||
| LogLevel.Trace -> delegate.isTraceEnabled | ||
| LogLevel.Debug -> delegate.isDebugEnabled | ||
| LogLevel.Info -> delegate.isInfoEnabled | ||
| LogLevel.Warning -> delegate.isWarnEnabled | ||
| LogLevel.Error -> delegate.isErrorEnabled | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package aws.smithy.kotlin.runtime.telemetry.logging.slf4j | ||
|
|
||
| import aws.smithy.kotlin.runtime.telemetry.context.Context | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.LogLevel | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.LogRecordBuilder | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.LoggerProvider | ||
| import org.slf4j.MDC | ||
|
|
||
| private val noOpLogRecordBuilder = LoggerProvider.None.getOrCreateLogger("NoOpLogger").atLevel(LogLevel.Error) | ||
|
|
||
| /** | ||
| * SLF4J 1.x based logger | ||
| */ | ||
| internal class Slf4j1xLoggerAdapter(logger: org.slf4j.Logger) : AbstractSlf4jLoggerAdapter(logger) { | ||
| override fun atLevel(level: LogLevel): LogRecordBuilder = if (isEnabledFor(level)) { | ||
| Slf4j1xLogRecordBuilderAdapter(this, level) | ||
| } else { | ||
| noOpLogRecordBuilder | ||
| } | ||
| } | ||
|
|
||
| private class Slf4j1xLogRecordBuilderAdapter( | ||
| private val delegate: Slf4j1xLoggerAdapter, | ||
| private val level: LogLevel, | ||
| ) : LogRecordBuilder { | ||
|
|
||
| private var msg: (() -> String)? = null | ||
| private var cause: Throwable? = null | ||
| private val kvp by lazy { mutableMapOf<String, Any>() } | ||
| override fun setCause(ex: Throwable) { | ||
| cause = ex | ||
| } | ||
|
|
||
| override fun setMessage(message: String) { | ||
| msg = { message } | ||
| } | ||
|
|
||
| override fun setMessage(message: () -> String) { | ||
| msg = message | ||
| } | ||
|
|
||
| override fun setKeyValuePair(key: String, value: Any) { | ||
| kvp[key] = value | ||
| } | ||
|
|
||
| override fun setContext(context: Context) { | ||
| // TODO - add a way to get the current trace context and set the key/value pair on it? | ||
| } | ||
|
Comment on lines
+50
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Could be moved down to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately it can't because this is on the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, sorry I was thinking this was still in |
||
|
|
||
| override fun emit() { | ||
| val message = requireNotNull(msg) { "no message provided to LogRecordBuilder" } | ||
| val logMethod = when (level) { | ||
| LogLevel.Error -> delegate::error | ||
| LogLevel.Warning -> delegate::warn | ||
| LogLevel.Info -> delegate::info | ||
| LogLevel.Debug -> delegate::debug | ||
| LogLevel.Trace -> delegate::trace | ||
| } | ||
|
|
||
| if (kvp.isNotEmpty()) { | ||
| val prevCtx = MDC.getCopyOfContextMap() | ||
| try { | ||
| kvp.forEach { (k, v) -> | ||
| MDC.put(k, v.toString()) | ||
| } | ||
| logMethod(cause, message) | ||
| } finally { | ||
| MDC.setContextMap(prevCtx) | ||
| } | ||
| } else { | ||
| logMethod(cause, message) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package aws.smithy.kotlin.runtime.telemetry.logging.slf4j | ||
|
|
||
| import aws.smithy.kotlin.runtime.telemetry.context.Context | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.LogLevel | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.LogRecordBuilder | ||
| import org.slf4j.event.Level | ||
| import org.slf4j.spi.LoggingEventBuilder | ||
|
|
||
| /** | ||
| * SLF4J 2.x based logger | ||
| */ | ||
| internal class Slf4j2xLoggerAdapter(logger: org.slf4j.Logger) : AbstractSlf4jLoggerAdapter(logger) { | ||
| override fun atLevel(level: LogLevel): LogRecordBuilder = | ||
| Slf4j2xLogRecordBuilderAdapter(delegate.atLevel(level.slf4jLevel)) | ||
| } | ||
|
|
||
| private class Slf4j2xLogRecordBuilderAdapter( | ||
| private val delegate: LoggingEventBuilder, | ||
| ) : LogRecordBuilder { | ||
| override fun setCause(ex: Throwable) { | ||
| delegate.setCause(ex) | ||
| } | ||
|
|
||
| override fun setMessage(message: String) { | ||
| delegate.setMessage(message) | ||
| } | ||
|
|
||
| override fun setMessage(message: () -> String) { | ||
| delegate.setMessage(message) | ||
| } | ||
|
|
||
| override fun setKeyValuePair(key: String, value: Any) { | ||
| delegate.addKeyValue(key, value) | ||
| } | ||
|
|
||
| override fun setContext(context: Context) { | ||
| // TODO - add a way to get the current trace context and set the key/value pair on it? | ||
| } | ||
|
|
||
| override fun emit() = delegate.log() | ||
| } | ||
|
|
||
| private val LogLevel.slf4jLevel: org.slf4j.event.Level | ||
| get() = when (this) { | ||
| LogLevel.Error -> Level.ERROR | ||
| LogLevel.Warning -> Level.WARN | ||
| LogLevel.Info -> Level.INFO | ||
| LogLevel.Debug -> Level.DEBUG | ||
| LogLevel.Trace -> Level.TRACE | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import aws.sdk.kotlin.gradle.dsl.skipPublishing | ||
|
|
||
| plugins { | ||
| kotlin("jvm") | ||
| } | ||
|
|
||
| skipPublishing() | ||
|
|
||
| dependencies { | ||
| implementation(project(":runtime:observability:logging-slf4j2")) | ||
| implementation(libs.slf4j.api) { | ||
| version { | ||
| strictly(libs.versions.slf4j.v1x.version.get()) | ||
| } | ||
| } | ||
| implementation(libs.slf4j.simple) { | ||
| version { | ||
| strictly(libs.versions.slf4j.v1x.version.get()) | ||
| } | ||
| } | ||
|
|
||
| testImplementation(libs.junit.jupiter) | ||
| testImplementation(libs.kotlin.test) | ||
| testImplementation(libs.kotlin.test.junit5) | ||
| } | ||
|
|
||
| tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { | ||
| kotlinOptions.jvmTarget = "1.8" | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| testLogging { | ||
| events("passed", "skipped", "failed") | ||
| showStandardStreams = false | ||
| showStackTraces = true | ||
| showExceptions = true | ||
| exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the directory
logging-slf4j2also be renamed to drop the2?