Skip to content

Commit 96f0eb2

Browse files
buenaflorclaude
andauthored
feat(kmp): Add generic log APIs with explicit level parameter (#520)
* Add support for new log functions that take a level param * Update * Update CHANGELOG * Fix MaxLineLength detekt violations in log method signatures Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a80bee6 commit 96f0eb2

8 files changed

Lines changed: 410 additions & 118 deletions

File tree

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Add generic log APIs with explicit level parameter ([#520](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/520))
8+
9+
```kotlin
10+
// New APIs:
11+
// Simple message
12+
Sentry.logger.log(SentryLogLevel.INFO, "User logged in")
13+
14+
// Message with parameters
15+
Sentry.logger.log(SentryLogLevel.WARN, "Rate limit reached for %s", endpoint)
16+
17+
// Message with parameters and attributes
18+
Sentry.logger.log(SentryLogLevel.ERROR, "Failed to process %s", request) {
19+
this["error.code"] = 500
20+
this["retry"] = true
21+
}
22+
23+
// Full DSL
24+
Sentry.logger.log(SentryLogLevel.FATAL) {
25+
message("Database connection pool exhausted for %s", dbHost)
26+
attributes {
27+
this["database"] = "users"
28+
this["activeConnections"] = 100
29+
}
30+
}
31+
```
32+
533
### Dependencies
634

735
- Bump Java SDK from v8.31.0 to v8.36.0 ([#529](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/529), [#532](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/532))

sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ public abstract interface class io/sentry/kotlin/multiplatform/log/SentryLogger
412412
public abstract fun info (Ljava/lang/String;[Ljava/lang/Object;)V
413413
public abstract fun info (Ljava/lang/String;[Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
414414
public abstract fun info (Lkotlin/jvm/functions/Function1;)V
415+
public abstract fun log (Lio/sentry/kotlin/multiplatform/log/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
416+
public abstract fun log (Lio/sentry/kotlin/multiplatform/log/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
417+
public abstract fun log (Lio/sentry/kotlin/multiplatform/log/SentryLogLevel;Lkotlin/jvm/functions/Function1;)V
415418
public abstract fun trace (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
416419
public abstract fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
417420
public abstract fun trace (Ljava/lang/String;[Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V

sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ public abstract interface class io/sentry/kotlin/multiplatform/log/SentryLogger
409409
public abstract fun info (Ljava/lang/String;[Ljava/lang/Object;)V
410410
public abstract fun info (Ljava/lang/String;[Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
411411
public abstract fun info (Lkotlin/jvm/functions/Function1;)V
412+
public abstract fun log (Lio/sentry/kotlin/multiplatform/log/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
413+
public abstract fun log (Lio/sentry/kotlin/multiplatform/log/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
414+
public abstract fun log (Lio/sentry/kotlin/multiplatform/log/SentryLogLevel;Lkotlin/jvm/functions/Function1;)V
412415
public abstract fun trace (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
413416
public abstract fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
414417
public abstract fun trace (Ljava/lang/String;[Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V

sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/log/BaseSentryLogger.kt

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,83 @@ internal abstract class BaseSentryLogger(
2626
protected abstract fun sendLog(level: SentryLogLevel, formatted: FormattedLog)
2727

2828
override fun trace(message: String, vararg args: Any?) =
29-
log(SentryLogLevel.TRACE, message, args)
29+
logWithParams(SentryLogLevel.TRACE, message, args)
3030
override fun debug(message: String, vararg args: Any?) =
31-
log(SentryLogLevel.DEBUG, message, args)
31+
logWithParams(SentryLogLevel.DEBUG, message, args)
3232
override fun info(message: String, vararg args: Any?) =
33-
log(SentryLogLevel.INFO, message, args)
33+
logWithParams(SentryLogLevel.INFO, message, args)
3434
override fun warn(message: String, vararg args: Any?) =
35-
log(SentryLogLevel.WARN, message, args)
35+
logWithParams(SentryLogLevel.WARN, message, args)
3636
override fun error(message: String, vararg args: Any?) =
37-
log(SentryLogLevel.ERROR, message, args)
37+
logWithParams(SentryLogLevel.ERROR, message, args)
3838
override fun fatal(message: String, vararg args: Any?) =
39-
log(SentryLogLevel.FATAL, message, args)
39+
logWithParams(SentryLogLevel.FATAL, message, args)
4040

4141
override fun trace(message: String, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
42-
log(SentryLogLevel.TRACE, message, attributes = attributes)
42+
logWithParams(SentryLogLevel.TRACE, message, attributes = attributes)
4343
override fun debug(message: String, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
44-
log(SentryLogLevel.DEBUG, message, attributes = attributes)
44+
logWithParams(SentryLogLevel.DEBUG, message, attributes = attributes)
4545
override fun info(message: String, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
46-
log(SentryLogLevel.INFO, message, attributes = attributes)
46+
logWithParams(SentryLogLevel.INFO, message, attributes = attributes)
4747
override fun warn(message: String, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
48-
log(SentryLogLevel.WARN, message, attributes = attributes)
48+
logWithParams(SentryLogLevel.WARN, message, attributes = attributes)
4949
override fun error(message: String, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
50-
log(SentryLogLevel.ERROR, message, attributes = attributes)
50+
logWithParams(SentryLogLevel.ERROR, message, attributes = attributes)
5151
override fun fatal(message: String, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
52-
log(SentryLogLevel.FATAL, message, attributes = attributes)
52+
logWithParams(SentryLogLevel.FATAL, message, attributes = attributes)
5353

5454
override fun trace(message: String, vararg args: Any?, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
55-
log(SentryLogLevel.TRACE, message, args, attributes)
55+
logWithParams(SentryLogLevel.TRACE, message, args, attributes)
5656
override fun debug(message: String, vararg args: Any?, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
57-
log(SentryLogLevel.DEBUG, message, args, attributes)
57+
logWithParams(SentryLogLevel.DEBUG, message, args, attributes)
5858
override fun info(message: String, vararg args: Any?, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
59-
log(SentryLogLevel.INFO, message, args, attributes)
59+
logWithParams(SentryLogLevel.INFO, message, args, attributes)
6060
override fun warn(message: String, vararg args: Any?, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
61-
log(SentryLogLevel.WARN, message, args, attributes)
61+
logWithParams(SentryLogLevel.WARN, message, args, attributes)
6262
override fun error(message: String, vararg args: Any?, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
63-
log(SentryLogLevel.ERROR, message, args, attributes)
63+
logWithParams(SentryLogLevel.ERROR, message, args, attributes)
6464
override fun fatal(message: String, vararg args: Any?, attributes: @SentryLogDsl SentryAttributes.() -> Unit) =
65-
log(SentryLogLevel.FATAL, message, args, attributes)
65+
logWithParams(SentryLogLevel.FATAL, message, args, attributes)
66+
67+
override fun log(level: SentryLogLevel, message: String, vararg args: Any?) =
68+
logWithParams(level, message, args)
69+
70+
@Suppress("SpreadOperator")
71+
override fun log(
72+
level: SentryLogLevel,
73+
message: String,
74+
vararg args: Any?,
75+
attributes: @SentryLogDsl SentryAttributes.() -> Unit
76+
) = logWithParams(level, message, args, attributes)
77+
78+
override fun log(level: SentryLogLevel, block: SentryLogBuilder.() -> Unit) =
79+
logWithBuilder(block, level)
6680

6781
override fun trace(block: SentryLogBuilder.() -> Unit) =
68-
log(block, SentryLogLevel.TRACE)
82+
logWithBuilder(block, SentryLogLevel.TRACE)
6983
override fun debug(block: SentryLogBuilder.() -> Unit) =
70-
log(block, SentryLogLevel.DEBUG)
84+
logWithBuilder(block, SentryLogLevel.DEBUG)
7185
override fun info(block: SentryLogBuilder.() -> Unit) =
72-
log(block, SentryLogLevel.INFO)
86+
logWithBuilder(block, SentryLogLevel.INFO)
7387
override fun warn(block: SentryLogBuilder.() -> Unit) =
74-
log(block, SentryLogLevel.WARN)
88+
logWithBuilder(block, SentryLogLevel.WARN)
7589
override fun error(block: SentryLogBuilder.() -> Unit) =
76-
log(block, SentryLogLevel.ERROR)
90+
logWithBuilder(block, SentryLogLevel.ERROR)
7791
override fun fatal(block: SentryLogBuilder.() -> Unit) =
78-
log(block, SentryLogLevel.FATAL)
92+
logWithBuilder(block, SentryLogLevel.FATAL)
7993

8094
@Suppress("SpreadOperator")
81-
private fun log(
95+
private fun logWithParams(
8296
level: SentryLogLevel,
8397
message: String,
8498
args: Array<out Any?> = emptyArray(),
8599
attributes: (@SentryLogDsl SentryAttributes.() -> Unit)? = null
86-
) = log({
100+
) = logWithBuilder({
87101
if (args.isEmpty()) message(message) else message(message, *args)
88102
attributes?.let { attributes(it) }
89103
}, level)
90104

91-
private inline fun log(block: SentryLogBuilder.() -> Unit, level: SentryLogLevel) {
105+
private inline fun logWithBuilder(block: SentryLogBuilder.() -> Unit, level: SentryLogLevel) {
92106
val formatted = logBuilderFactory().apply(block).buildFormatted() ?: return
93107
sendLog(level, formatted)
94108
}

sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/log/SentryLogger.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,49 @@ import io.sentry.kotlin.multiplatform.SentryAttributes
88
* This interface provides methods to log messages at different severity levels.
99
*/
1010
public interface SentryLogger {
11+
/**
12+
* Logs a message at a specific level.
13+
*
14+
* @param level The log level
15+
* @param message The message template (use %s for substitution, %% for literal %)
16+
* @param args Arguments to substitute into the message template via toString()
17+
*/
18+
public fun log(level: SentryLogLevel, message: String, vararg args: Any?)
19+
20+
/**
21+
* Logs a message at a specific level with inline attributes.
22+
*
23+
* @param level The log level
24+
* @param message The message template (use %s for substitution, %% for literal %)
25+
* @param args Arguments to substitute into the message template via toString()
26+
* @param attributes A lambda with [SentryAttributes] receiver to set key-value attributes
27+
*/
28+
public fun log(
29+
level: SentryLogLevel,
30+
message: String,
31+
vararg args: Any?,
32+
attributes: @SentryLogDsl SentryAttributes.() -> Unit
33+
)
34+
35+
/**
36+
* Logs a message at a specific level using a DSL builder.
37+
*
38+
* Example:
39+
* ```
40+
* Sentry.logger.log(SentryLogLevel.INFO) {
41+
* message("User %s authenticated via %s", userId, method)
42+
* attributes {
43+
* this["auth.method"] = "oauth2"
44+
* this["user.id"] = userId
45+
* }
46+
* }
47+
* ```
48+
*
49+
* @param level The log level
50+
* @param block A lambda with [SentryLogBuilder] receiver to configure the log entry
51+
*/
52+
public fun log(level: SentryLogLevel, block: SentryLogBuilder.() -> Unit)
53+
1154
/**
1255
* Logs a message at [SentryLogLevel.TRACE] level.
1356
*

sentry-kotlin-multiplatform/src/commonStub/kotlin/io/sentry/kotlin/multiplatform/NoOpSentryLogger.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform
22

33
import io.sentry.kotlin.multiplatform.log.SentryLogBuilder
44
import io.sentry.kotlin.multiplatform.log.SentryLogDsl
5+
import io.sentry.kotlin.multiplatform.log.SentryLogLevel
56
import io.sentry.kotlin.multiplatform.log.SentryLogger
67

78
/**
@@ -80,6 +81,23 @@ internal class NoOpSentryLogger : SentryLogger {
8081
// No-op
8182
}
8283

84+
override fun log(level: SentryLogLevel, message: String, vararg args: Any?) {
85+
// No-op
86+
}
87+
88+
override fun log(
89+
level: SentryLogLevel,
90+
message: String,
91+
vararg args: Any?,
92+
attributes: @SentryLogDsl SentryAttributes.() -> Unit
93+
) {
94+
// No-op
95+
}
96+
97+
override fun log(level: SentryLogLevel, block: SentryLogBuilder.() -> Unit) {
98+
// No-op
99+
}
100+
83101
override fun trace(block: SentryLogBuilder.() -> Unit) {
84102
// No-op
85103
}

0 commit comments

Comments
 (0)