Skip to content

Commit

Permalink
Add Tagging and Attributes functionality, Generic .log() function (#40)
Browse files Browse the repository at this point in the history
* Add Tagging and Attributes functionality, Generic .log() function on loggers
  • Loading branch information
sdonn3 authored Feb 9, 2024
1 parent 34ef795 commit 73ef2c5
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 20 deletions.
10 changes: 9 additions & 1 deletion bundled/api/bundled.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ public final class com/juul/datadog/DatadogInitializer : com/juul/datadog/Initia
public fun setTrackingConsent (Lcom/juul/datadog/TrackingConsent;)V
}

public final class com/juul/datadog/DatadogLogger : com/juul/datadog/JvmLogger {
public final class com/juul/datadog/DatadogLogger : com/juul/datadog/JvmLogger, com/juul/datadog/TagHandler {
public fun <init> (Ljava/lang/String;Lcom/juul/datadog/Logger$Level;Lcom/juul/datadog/LoggerConfiguration;)V
public synthetic fun <init> (Ljava/lang/String;Lcom/juul/datadog/Logger$Level;Lcom/juul/datadog/LoggerConfiguration;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun addAttribute (Ljava/lang/String;Ljava/lang/String;)V
public fun addTag (Ljava/lang/String;)V
public fun addTagWithKey (Ljava/lang/String;Ljava/lang/String;)V
public fun assert (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun debug (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun error (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun info (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun log (Lcom/juul/datadog/Logger$Level;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun removeAttribute (Ljava/lang/String;)V
public fun removeTag (Ljava/lang/String;)V
public fun removeTagsWithKey (Ljava/lang/String;)V
public fun verbose (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun warn (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
}
Expand Down
60 changes: 53 additions & 7 deletions bundled/src/androidMain/kotlin/DatadogLogger.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.juul.datadog

import android.util.Log
import com.juul.datadog.Logger.Level.Assert
import com.juul.datadog.Logger.Level.Debug
import com.juul.datadog.Logger.Level.Error
import com.juul.datadog.Logger.Level.Info
import com.juul.datadog.Logger.Level.Notice
import com.juul.datadog.Logger.Level.Verbose
import com.juul.datadog.Logger.Level.Warn
import com.datadog.android.log.Logger as DatadogLogger

private const val ALL = -1
Expand All @@ -9,7 +16,7 @@ public actual class DatadogLogger actual constructor(
name: String,
level: Logger.Level?,
configuration: LoggerConfiguration?,
) : JvmLogger {
) : JvmLogger, TagHandler {

private val logger: DatadogLogger = DatadogLogger.Builder()
.setName(name)
Expand All @@ -26,6 +33,21 @@ public actual class DatadogLogger actual constructor(
}
.build()

override fun log(level: Logger.Level, message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
when (level) {
Verbose -> verbose(message, attributes, throwable)
Debug -> debug(message, attributes, throwable)
Info -> info(message, attributes, throwable)
Notice, Warn -> warn(message, attributes, throwable)
Error -> error(message, attributes, throwable)
Assert -> assert(message, attributes, throwable)
}
}

override fun assert(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
logger.wtf(message, throwable, attributes.orEmpty())
}

override fun verbose(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
logger.v(message, throwable, attributes.orEmpty())
}
Expand All @@ -45,13 +67,37 @@ public actual class DatadogLogger actual constructor(
override fun error(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
logger.e(message, throwable, attributes.orEmpty())
}

override fun addTag(tag: String) {
logger.addTag(tag)
}

override fun removeTag(tag: String) {
logger.removeTag(tag)
}

override fun addTagWithKey(key: String, value: String) {
logger.addTag(key, value)
}

override fun removeTagsWithKey(key: String) {
logger.removeTagsWithKey(key)
}

override fun addAttribute(key: String, value: String) {
logger.addAttribute(key, value)
}

override fun removeAttribute(key: String) {
logger.removeAttribute(key)
}
}

private fun Logger.Level.toDatadogType(): Int = when (this) {
Logger.Level.Verbose -> Log.VERBOSE
Logger.Level.Debug -> Log.DEBUG
Logger.Level.Info -> Log.INFO
Logger.Level.Notice, Logger.Level.Warn -> Log.WARN
Logger.Level.Error -> Log.ERROR
Logger.Level.Assert -> Log.ASSERT
Verbose -> Log.VERBOSE
Debug -> Log.DEBUG
Info -> Log.INFO
Notice, Warn -> Log.WARN
Error -> Log.ERROR
Assert -> Log.ASSERT
}
67 changes: 59 additions & 8 deletions bundled/src/iosMain/kotlin/DatadogLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import cocoapods.DatadogObjc.DDLogLevelNotice
import cocoapods.DatadogObjc.DDLogLevelWarn
import cocoapods.DatadogObjc.DDLogger
import cocoapods.DatadogObjc.DDLoggerConfiguration
import com.juul.datadog.Logger.Level.Assert
import com.juul.datadog.Logger.Level.Debug
import com.juul.datadog.Logger.Level.Error
import com.juul.datadog.Logger.Level.Info
import com.juul.datadog.Logger.Level.Notice
import com.juul.datadog.Logger.Level.Verbose
import com.juul.datadog.Logger.Level.Warn
import com.rickclephas.kmp.nserrorkt.asNSError

/**
Expand All @@ -21,10 +28,10 @@ public actual class DatadogLogger actual constructor(
name: String,
level: Logger.Level?,
configuration: LoggerConfiguration?,
) : IosLogger {
) : IosLogger, TagHandler {

private val logger = DDLogger.createWith(
DDLoggerConfiguration(name, level ?: Logger.Level.Debug, configuration),
DDLoggerConfiguration(name, level ?: Debug, configuration),
)

override fun notice(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
Expand All @@ -36,6 +43,26 @@ public actual class DatadogLogger actual constructor(
}
}

override fun log(level: Logger.Level, message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
when (level) {
Verbose, Debug -> debug(message, attributes, throwable)
Info -> info(message, attributes, throwable)
Notice -> notice(message, attributes, throwable)
Warn -> warn(message, attributes, throwable)
Error -> error(message, attributes, throwable)
Assert -> critical(message, attributes, throwable)
}
}

override fun critical(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
val attrs = attributes.orEmpty() as Map<Any?, *>
if (throwable == null) {
logger.critical(message, attrs)
} else {
logger.critical(message, throwable.asNSError(), attrs)
}
}

override fun debug(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
val attrs = attributes.orEmpty() as Map<Any?, *>
if (throwable == null) {
Expand Down Expand Up @@ -71,6 +98,30 @@ public actual class DatadogLogger actual constructor(
logger.error(message, throwable.asNSError(), attrs)
}
}

override fun addTag(tag: String) {
logger.addWithTag(tag)
}

override fun removeTag(tag: String) {
logger.removeWithTag(tag)
}

override fun addTagWithKey(key: String, value: String) {
logger.addTagWithKey(key, value)
}

override fun removeTagsWithKey(key: String) {
logger.removeTagWithKey(key)
}

override fun addAttribute(key: String, value: String) {
logger.addAttributeForKey(key, value)
}

override fun removeAttribute(key: String) {
logger.removeAttributeForKey(key)
}
}

@Suppress("ktlint:standard:function-naming")
Expand All @@ -97,10 +148,10 @@ private fun DDLoggerConfiguration(
}

private fun Logger.Level.toDatadogType(): DDLogLevel = when (this) {
Logger.Level.Verbose, Logger.Level.Debug -> DDLogLevelDebug
Logger.Level.Info -> DDLogLevelInfo
Logger.Level.Notice -> DDLogLevelNotice
Logger.Level.Warn -> DDLogLevelWarn
Logger.Level.Error -> DDLogLevelError
Logger.Level.Assert -> DDLogLevelCritical
Verbose, Debug -> DDLogLevelDebug
Info -> DDLogLevelInfo
Notice -> DDLogLevelNotice
Warn -> DDLogLevelWarn
Error -> DDLogLevelError
Assert -> DDLogLevelCritical
}
31 changes: 27 additions & 4 deletions bundled/src/jsMain/kotlin/DatadogLogger.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.juul.datadog

import com.juul.datadog.Logger.Level.Assert
import com.juul.datadog.Logger.Level.Debug
import com.juul.datadog.Logger.Level.Error
import com.juul.datadog.Logger.Level.Info
import com.juul.datadog.Logger.Level.Notice
import com.juul.datadog.Logger.Level.Verbose
import com.juul.datadog.Logger.Level.Warn
import com.juul.datadog.external.JsLoggerConfiguration
import com.juul.datadog.external.datadogLogs

Expand All @@ -10,6 +17,14 @@ public actual class DatadogLogger actual constructor(
) : BrowserLogger {

private val logger = datadogLogs.createLogger(name, conf(level, configuration))
override fun log(level: Logger.Level, message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
when (level) {
Verbose, Debug -> debug(message, attributes, throwable)
Info -> info(message, attributes, throwable)
Notice, Warn -> warn(message, attributes, throwable)
Error, Assert -> error(message, attributes, throwable)
}
}

override fun debug(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
logger.debug(message, attributes?.toJsObject(), throwable)
Expand All @@ -26,6 +41,14 @@ public actual class DatadogLogger actual constructor(
override fun error(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
logger.error(message, attributes?.toJsObject(), throwable)
}

override fun addAttribute(key: String, value: String) {
datadogLogs.setGlobalContextProperty(key, value)
}

override fun removeAttribute(key: String) {
datadogLogs.removeGlobalContextProperty(key)
}
}

private fun conf(
Expand All @@ -40,10 +63,10 @@ private fun conf(
}

private fun Logger.Level.toDatadogType() = when (this) {
Logger.Level.Verbose, Logger.Level.Debug -> "debug"
Logger.Level.Info -> "info"
Logger.Level.Notice, Logger.Level.Warn -> "warn"
Logger.Level.Error, Logger.Level.Assert -> "error"
Verbose, Debug -> "debug"
Info -> "info"
Notice, Warn -> "warn"
Error, Assert -> "error"
}

private fun LoggerConfiguration.Handler.toDatadogType(): String = when (this) {
Expand Down
4 changes: 4 additions & 0 deletions bundled/src/jsMain/kotlin/external/datadogLogs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public external interface DatadogLogs {
public fun getLogger(name: String): JsLogger

public val logger: JsLogger

public fun setGlobalContextProperty(key: String, value: String)

public fun removeGlobalContextProperty(key: String)
}

public external val datadogLogs: DatadogLogs
16 changes: 16 additions & 0 deletions datadog/api/datadog.api
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,30 @@ public final class com/juul/datadog/Initializer$DefaultImpls {
}

public abstract interface class com/juul/datadog/JvmLogger : com/juul/datadog/Logger {
public abstract fun assert (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public abstract fun verbose (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
}

public final class com/juul/datadog/JvmLogger$DefaultImpls {
public static synthetic fun assert$default (Lcom/juul/datadog/JvmLogger;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
public static synthetic fun verbose$default (Lcom/juul/datadog/JvmLogger;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
}

public abstract interface class com/juul/datadog/Logger {
public abstract fun addAttribute (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun debug (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public abstract fun error (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public abstract fun info (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public abstract fun log (Lcom/juul/datadog/Logger$Level;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public abstract fun removeAttribute (Ljava/lang/String;)V
public abstract fun warn (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
}

public final class com/juul/datadog/Logger$DefaultImpls {
public static synthetic fun debug$default (Lcom/juul/datadog/Logger;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
public static synthetic fun error$default (Lcom/juul/datadog/Logger;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
public static synthetic fun info$default (Lcom/juul/datadog/Logger;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
public static synthetic fun log$default (Lcom/juul/datadog/Logger;Lcom/juul/datadog/Logger$Level;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
public static synthetic fun warn$default (Lcom/juul/datadog/Logger;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;ILjava/lang/Object;)V
}

Expand Down Expand Up @@ -116,9 +122,12 @@ public final class com/juul/datadog/NoopInitializer : com/juul/datadog/Initializ

public final class com/juul/datadog/NoopLogger : com/juul/datadog/Logger {
public fun <init> ()V
public fun addAttribute (Ljava/lang/String;Ljava/lang/String;)V
public fun debug (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun error (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun info (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun log (Lcom/juul/datadog/Logger$Level;Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
public fun removeAttribute (Ljava/lang/String;)V
public fun warn (Ljava/lang/String;Ljava/util/Map;Ljava/lang/Throwable;)V
}

Expand All @@ -133,6 +142,13 @@ public final class com/juul/datadog/Site : java/lang/Enum {
public static fun values ()[Lcom/juul/datadog/Site;
}

public abstract interface class com/juul/datadog/TagHandler {
public abstract fun addTag (Ljava/lang/String;)V
public abstract fun addTagWithKey (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun removeTag (Ljava/lang/String;)V
public abstract fun removeTagsWithKey (Ljava/lang/String;)V
}

public final class com/juul/datadog/TrackingConsent : java/lang/Enum {
public static final field Granted Lcom/juul/datadog/TrackingConsent;
public static final field NotGranted Lcom/juul/datadog/TrackingConsent;
Expand Down
4 changes: 4 additions & 0 deletions datadog/src/commonMain/kotlin/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.juul.datadog

public interface Logger {
public fun log(level: Level, message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)

public fun debug(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
public fun info(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
public fun warn(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
public fun error(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
public fun addAttribute(key: String, value: String)

public fun removeAttribute(key: String)

public enum class Level {
Verbose, // Android only, maps to Debug on iOS & JS.
Expand Down
11 changes: 11 additions & 0 deletions datadog/src/commonMain/kotlin/NoopLogger.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.juul.datadog

public class NoopLogger : Logger {
override fun log(level: Logger.Level, message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
// No-op
}

override fun debug(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
// No-op
Expand All @@ -17,4 +20,12 @@ public class NoopLogger : Logger {
override fun error(message: String, attributes: Map<String, Any?>?, throwable: Throwable?) {
// No-op
}

override fun addAttribute(key: String, value: String) {
// No-op
}

override fun removeAttribute(key: String) {
// No-op
}
}
1 change: 1 addition & 0 deletions datadog/src/iosMain/kotlin/IosLogger.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.juul.datadog

public interface IosLogger : Logger {
public fun critical(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
public fun notice(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
}
1 change: 1 addition & 0 deletions datadog/src/jvmMain/kotlin/JvmLogger.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.juul.datadog

public interface JvmLogger : Logger {
public fun assert(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
public fun verbose(message: String, attributes: Map<String, Any?>? = null, throwable: Throwable? = null)
}
Loading

0 comments on commit 73ef2c5

Please sign in to comment.