Skip to content

Commit

Permalink
Support Keys with arguments, improved API for SimpleLoggers, and …
Browse files Browse the repository at this point in the history
…new khronicle-test module (#33)
  • Loading branch information
cedrickcooke authored Apr 3, 2024
1 parent aa004ef commit 5e15c88
Show file tree
Hide file tree
Showing 22 changed files with 358 additions and 204 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ tasks.dokkaHtmlMultiModule.configure {
outputDirectory.fileProvider(layout.buildDirectory.file("dokkaHtmlMultiModule").map { it.asFile })
}

apiValidation {
nonPublicMarkers.add("com.juul.khronicle.KhronicleInternal")
}

fun Project.withPluginWhenEvaluated(plugin: String, action: Project.() -> Unit) {
pluginManager.withPlugin(plugin) { whenEvaluated(action) }
}
Expand Down
56 changes: 18 additions & 38 deletions khronicle-core/api/khronicle-core.api
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
public final class com/juul/khronicle/Call {
public fun <init> (Lcom/juul/khronicle/LogLevel;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;Lcom/juul/khronicle/ReadMetadata;)V
public final fun component1 ()Lcom/juul/khronicle/LogLevel;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Ljava/lang/String;
public final fun component4 ()Ljava/lang/Throwable;
public final fun component5 ()Lcom/juul/khronicle/ReadMetadata;
public final fun copy (Lcom/juul/khronicle/LogLevel;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;Lcom/juul/khronicle/ReadMetadata;)Lcom/juul/khronicle/Call;
public static synthetic fun copy$default (Lcom/juul/khronicle/Call;Lcom/juul/khronicle/LogLevel;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;Lcom/juul/khronicle/ReadMetadata;ILjava/lang/Object;)Lcom/juul/khronicle/Call;
public fun equals (Ljava/lang/Object;)Z
public final fun getLevel ()Lcom/juul/khronicle/LogLevel;
public final fun getMessage ()Ljava/lang/String;
public final fun getMetadata ()Lcom/juul/khronicle/ReadMetadata;
public final fun getTag ()Ljava/lang/String;
public final fun getThrowable ()Ljava/lang/Throwable;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public class com/juul/khronicle/CallListLogger : com/juul/khronicle/Logger {
public fun <init> ()V
public fun assert (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public fun debug (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public fun error (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public final fun getAllCalls ()Ljava/util/List;
public final fun getAssertCalls ()Ljava/util/List;
public final fun getDebugCalls ()Ljava/util/List;
public final fun getErrorCalls ()Ljava/util/List;
public final fun getInfoCalls ()Ljava/util/List;
public fun getMinimumLogLevel ()Lcom/juul/khronicle/LogLevel;
public final fun getVerboseCalls ()Ljava/util/List;
public final fun getWarnCalls ()Ljava/util/List;
public fun info (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public fun verbose (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public fun warn (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
}

public final class com/juul/khronicle/ConsoleLogger : com/juul/khronicle/Logger {
public static final field INSTANCE Lcom/juul/khronicle/ConsoleLogger;
public fun assert (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
Expand Down Expand Up @@ -118,7 +81,7 @@ public final class com/juul/khronicle/LogLevel : java/lang/Enum {
public static fun values ()[Lcom/juul/khronicle/LogLevel;
}

public abstract interface class com/juul/khronicle/Logger {
public abstract interface class com/juul/khronicle/Logger : com/juul/khronicle/HideFromStackTraceTag {
public abstract fun assert (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public abstract fun debug (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public abstract fun error (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
Expand All @@ -132,6 +95,10 @@ public final class com/juul/khronicle/Logger$DefaultImpls {
public static fun getMinimumLogLevel (Lcom/juul/khronicle/Logger;)Lcom/juul/khronicle/LogLevel;
}

public final class com/juul/khronicle/LoggerKt {
public static final fun dynamic (Lcom/juul/khronicle/Logger;Lcom/juul/khronicle/LogLevel;Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
}

public final class com/juul/khronicle/PrintlnLogger : com/juul/khronicle/HideFromStackTraceTag, com/juul/khronicle/Logger {
public static final field INSTANCE Lcom/juul/khronicle/PrintlnLogger;
public fun assert (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
Expand All @@ -146,6 +113,7 @@ public final class com/juul/khronicle/PrintlnLogger : com/juul/khronicle/HideFro
public abstract interface class com/juul/khronicle/ReadMetadata {
public abstract fun copy ()Lcom/juul/khronicle/ReadMetadata;
public abstract fun get (Lcom/juul/khronicle/Key;)Ljava/lang/Object;
public abstract fun getAll (Lkotlin/reflect/KClass;)Ljava/util/Map;
}

public final class com/juul/khronicle/Sensitivity : java/lang/Enum {
Expand All @@ -160,6 +128,18 @@ public final class com/juul/khronicle/Sensitivity : java/lang/Enum {
public final class com/juul/khronicle/Sensitivity$Companion : com/juul/khronicle/Key {
}

public abstract class com/juul/khronicle/SimpleLogger : com/juul/khronicle/Logger {
public fun <init> ()V
public final fun assert (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public final fun debug (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public final fun error (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public fun getMinimumLogLevel ()Lcom/juul/khronicle/LogLevel;
public final fun info (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public abstract fun log (Lcom/juul/khronicle/LogLevel;Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public final fun verbose (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
public final fun warn (Ljava/lang/String;Ljava/lang/String;Lcom/juul/khronicle/ReadMetadata;Ljava/lang/Throwable;)V
}

public abstract interface class com/juul/khronicle/TagGenerator {
public abstract fun getTag ()Ljava/lang/String;
}
Expand Down
1 change: 1 addition & 0 deletions khronicle-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kotlin {

commonTest.dependencies {
implementation(kotlin("test"))
implementation(projects.khronicleTest)
}
}
}
41 changes: 0 additions & 41 deletions khronicle-core/src/commonMain/kotlin/CallListLogger.kt

This file was deleted.

13 changes: 12 additions & 1 deletion khronicle-core/src/commonMain/kotlin/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.juul.khronicle

/** Classes which implement [Logger] can write logs. */
public interface Logger {
public interface Logger : HideFromStackTraceTag {

/** Minimum level for this logger. Defaults to [LogLevel.Verbose] (all logs) if not overwritten. */
public val minimumLogLevel: LogLevel
Expand All @@ -25,3 +25,14 @@ public interface Logger {
/** Log at assert-level. Do not store a reference to [metadata], create a [copy][ReadMetadata.copy] if you need to. */
public fun assert(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?)
}

public fun Logger.dynamic(level: LogLevel, tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
when (level) {
LogLevel.Verbose -> verbose(tag, message, metadata, throwable)
LogLevel.Debug -> debug(tag, message, metadata, throwable)
LogLevel.Info -> info(tag, message, metadata, throwable)
LogLevel.Warn -> warn(tag, message, metadata, throwable)
LogLevel.Error -> error(tag, message, metadata, throwable)
LogLevel.Assert -> assert(tag, message, metadata, throwable)
}
}
22 changes: 16 additions & 6 deletions khronicle-core/src/commonMain/kotlin/Metadata.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.juul.khronicle

internal class Metadata : ReadMetadata, WriteMetadata {
import kotlin.reflect.KClass

@KhronicleInternal
public class Metadata : ReadMetadata, WriteMetadata {
private val storedData = mutableMapOf<Key<*>, Any>()

@Suppress("UNCHECKED_CAST")
public override operator fun <T : Any> get(key: Key<T>): T? =
override operator fun <T : Any> get(key: Key<T>): T? =
storedData[key] as? T

public override operator fun <T : Any> set(key: Key<T>, value: T) {
@Suppress("UNCHECKED_CAST")
override fun <K : Key<T>, T : Any> getAll(clazz: KClass<out K>): Map<K, T> =
storedData.filter { (key, _) -> clazz.isInstance(key) } as Map<K, T>

override operator fun <T : Any> set(key: Key<T>, value: T) {
storedData[key] = value
}

public override fun copy(): Metadata = Metadata().also { copy ->
override fun copy(): Metadata = Metadata().also { copy ->
copy.storedData += this.storedData
}

Expand All @@ -31,16 +38,19 @@ internal class Metadata : ReadMetadata, WriteMetadata {
* to [ReadMetadata] arguments after the function returns. If a [ReadMetadata] reference must be kept after
* function return, create a [copy].
*/
public interface ReadMetadata {
public sealed interface ReadMetadata {

public operator fun <T : Any> get(key: Key<T>): T?

public fun <K : Key<T>, T : Any> getAll(clazz: KClass<out K>): Map<K, T>

public fun copy(): ReadMetadata
}

/**
* Additional data associated with a log. It's important that [Log] calls do NOT hold onto references
* to [WriteMetadata] arguments after the lambda returns.
*/
public interface WriteMetadata {
public sealed interface WriteMetadata {
public operator fun <T : Any> set(key: Key<T>, value: T)
}
35 changes: 35 additions & 0 deletions khronicle-core/src/commonMain/kotlin/SimpleLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.juul.khronicle

/**
* Simplified [Logger] which passes all logs through a single [log] function that accepts an
* additional [LogLevel] argument. This is particularly useful when a logging backend does not have
* separate function calls for each log level.
*/
public abstract class SimpleLogger : Logger {

final override fun verbose(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log(LogLevel.Verbose, tag, message, metadata, throwable)
}

final override fun debug(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log(LogLevel.Debug, tag, message, metadata, throwable)
}

final override fun info(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log(LogLevel.Info, tag, message, metadata, throwable)
}

final override fun warn(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log(LogLevel.Warn, tag, message, metadata, throwable)
}

final override fun error(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log(LogLevel.Error, tag, message, metadata, throwable)
}

final override fun assert(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log(LogLevel.Assert, tag, message, metadata, throwable)
}

public abstract fun log(level: LogLevel, tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?)
}
Loading

0 comments on commit 5e15c88

Please sign in to comment.