From 4594476416c8e2d38e0d59157cdc9b52cafba111 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sun, 27 Oct 2024 10:51:16 +0100 Subject: [PATCH] Version. 1.0.0 --- ...otlin_simple_logging_js_1_0_0_SNAPSHOT.xml | 4 +- ...tlin_simple_logging_jvm_1_0_0_SNAPSHOT.xml | 4 +- build.gradle.kts | 42 +++++++-- .../kotlin/nl/astraeus/logger/Logger.kt | 92 +++++++++++-------- .../kotlin/nl/astraeus/logger/Logger.js.kt | 9 ++ .../kotlin/nl/astraeus/logger/Logger.jvm.kt | 9 ++ 6 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 src/jsMain/kotlin/nl/astraeus/logger/Logger.js.kt create mode 100644 src/jvmMain/kotlin/nl/astraeus/logger/Logger.jvm.kt diff --git a/.idea/artifacts/kotlin_simple_logging_js_1_0_0_SNAPSHOT.xml b/.idea/artifacts/kotlin_simple_logging_js_1_0_0_SNAPSHOT.xml index 206e918..370f298 100644 --- a/.idea/artifacts/kotlin_simple_logging_js_1_0_0_SNAPSHOT.xml +++ b/.idea/artifacts/kotlin_simple_logging_js_1_0_0_SNAPSHOT.xml @@ -1,6 +1,8 @@ $PROJECT_DIR$/build/libs - + + + \ No newline at end of file diff --git a/.idea/artifacts/kotlin_simple_logging_jvm_1_0_0_SNAPSHOT.xml b/.idea/artifacts/kotlin_simple_logging_jvm_1_0_0_SNAPSHOT.xml index 5cec96c..2cffccd 100644 --- a/.idea/artifacts/kotlin_simple_logging_jvm_1_0_0_SNAPSHOT.xml +++ b/.idea/artifacts/kotlin_simple_logging_jvm_1_0_0_SNAPSHOT.xml @@ -1,6 +1,8 @@ $PROJECT_DIR$/build/libs - + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index d07f33e..41c445f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,15 +8,19 @@ plugins { } group = "nl.astraeus" -version = "1.0.0-SNAPSHOT" +version = "1.0.0" repositories { mavenCentral() } kotlin { - jvm {} - js {} + jvm { + withJava() + } + js { + browser {} + } sourceSets { val commonMain by getting val jvmMain by getting @@ -24,19 +28,33 @@ kotlin { } } +extra["PUBLISH_GROUP_ID"] = group +extra["PUBLISH_VERSION"] = version +extra["PUBLISH_ARTIFACT_ID"] = name + +// Stub secrets to let the project sync and build without the publication values set up +val signingKeyId: String? by project +val signingPassword: String? by project +val signingSecretKeyRingFile: String? by project + +extra["signing.keyId"] = signingKeyId +extra["signing.password"] = signingPassword +extra["signing.secretKeyRingFile"] = signingSecretKeyRingFile + val javadocJar by tasks.registering(Jar::class) { archiveClassifier.set("javadoc") } publishing { repositories { + mavenLocal() maven { name = "gitea" setUrl("https://gitea.astraeus.nl/api/packages/rnentjes/maven") - credentials() { - val giteaUsername: kotlin.String? by project - val giteaPassword: kotlin.String? by project + credentials { + val giteaUsername: String? by project + val giteaPassword: String? by project username = giteaUsername password = giteaPassword @@ -51,9 +69,9 @@ publishing { // Provide artifacts information requited by Maven Central pom { - name.set("kotlin-komponent") - description.set("Kotlin komponent") - url.set("https://github.com/rnentjes/komponent") + name.set("kotlin-simple-logging") + description.set("Kotlin simple logging") + url.set("https://gitea.astraeus.nl/rnentjes/kotlin-simple-logging") licenses { license { @@ -69,7 +87,7 @@ publishing { } } scm { - url.set("https://github.com/rnentjes/komponent") + url.set("https://gitea.astraeus.nl/rnentjes/kotlin-simple-logging.git") } } } @@ -78,3 +96,7 @@ publishing { signing { sign(publishing.publications) } + +tasks.withType { + dependsOn(tasks.withType()) +} \ No newline at end of file diff --git a/src/commonMain/kotlin/nl/astraeus/logger/Logger.kt b/src/commonMain/kotlin/nl/astraeus/logger/Logger.kt index 0ca93b0..a25e17f 100644 --- a/src/commonMain/kotlin/nl/astraeus/logger/Logger.kt +++ b/src/commonMain/kotlin/nl/astraeus/logger/Logger.kt @@ -1,52 +1,68 @@ package nl.astraeus.logger -enum class LogLevel { - TRACE, - DEBUG, - INFO, - WARN, - ERROR, - FATAL +enum class LogLevel( + val label: String +) { + TRACE("Trace"), + DEBUG("Debug"), + INFO(" Info"), + WARN(" Warn"), + ERROR("Error"), + FATAL("Fatal"); } +expect fun getTimestamp(): String +expect fun getCurrentThread(): String? +expect fun getCaller(): String? + class Logger( - var level: LogLevel = LogLevel.INFO + var level: LogLevel = LogLevel.INFO, + val name: String = "" + ) { - fun trace(message: () -> String?) { - if (level.ordinal <= LogLevel.TRACE.ordinal) { - println("TRACE: ${message()}") - } - } + fun trace(message: () -> String) = log(LogLevel.TRACE, null, message) + fun debug(message: () -> String) = log(LogLevel.DEBUG, null, message) + fun info(message: () -> String) = log(LogLevel.INFO, null, message) + fun warn(message: () -> String) = log(LogLevel.WARN, null, message) + fun error(message: () -> String) = log(LogLevel.ERROR, null, message) + fun fatal(message: () -> String) = log(LogLevel.FATAL, null, message) - fun debug(message: () -> String?) { - if (level.ordinal <= LogLevel.DEBUG.ordinal) { - println("DEBUG: ${message()}") - } - } + fun trace(thrown: Throwable, message: () -> String) = log(LogLevel.TRACE, thrown, message) + fun debug(thrown: Throwable, message: () -> String) = log(LogLevel.DEBUG, thrown, message) + fun info(thrown: Throwable, message: () -> String) = log(LogLevel.INFO, thrown, message) + fun warn(thrown: Throwable, message: () -> String) = log(LogLevel.WARN, thrown, message) + fun error(thrown: Throwable, message: () -> String) = log(LogLevel.ERROR, thrown, message) + fun fatal(thrown: Throwable, message: () -> String) = log(LogLevel.FATAL, thrown, message) - fun info(message: () -> String?) { - if (level.ordinal <= LogLevel.INFO.ordinal) { - println("INFO: ${message()}") - } - } + private fun log( + level: LogLevel, + thrown: Throwable? = null, + message: () -> String + ) { + if (level.ordinal >= this.level.ordinal) { + val timestamp: String = getTimestamp() + val thread: String? = getCurrentThread() + val caller: String? = getCaller() - fun warn(e: Throwable? = null, message: () -> String?) { - if (level.ordinal <= LogLevel.WARN.ordinal) { - println("WARN: ${message()}") - e?.printStackTrace() - } - } + val logEntry = StringBuilder() - fun error(e: Throwable? = null, message: () -> String?) { - if (level.ordinal <= LogLevel.ERROR.ordinal) { - println("ERROR: ${message()}") - e?.printStackTrace() - } - } + logEntry.append(timestamp) + logEntry.append(" - ") + logEntry.append(level.name) + logEntry.append(" - ") + if (thread != null) { + logEntry.append(thread) + logEntry.append(" - ") + } + if (caller != null) { + logEntry.append(caller) + logEntry.append(" - ") + } + logEntry.append(message()) - fun fatal(e: Throwable, message: () -> String?) { - println("FATAL: ${message()}") - e.printStackTrace() + println(logEntry) + thrown?.printStackTrace() + } } } diff --git a/src/jsMain/kotlin/nl/astraeus/logger/Logger.js.kt b/src/jsMain/kotlin/nl/astraeus/logger/Logger.js.kt new file mode 100644 index 0000000..e1eba3a --- /dev/null +++ b/src/jsMain/kotlin/nl/astraeus/logger/Logger.js.kt @@ -0,0 +1,9 @@ +package nl.astraeus.logger + +import kotlin.js.Date + +actual fun getTimestamp(): String = Date().toISOString() + +actual fun getCurrentThread(): String? = null + +actual fun getCaller(): String? = null \ No newline at end of file diff --git a/src/jvmMain/kotlin/nl/astraeus/logger/Logger.jvm.kt b/src/jvmMain/kotlin/nl/astraeus/logger/Logger.jvm.kt new file mode 100644 index 0000000..6683197 --- /dev/null +++ b/src/jvmMain/kotlin/nl/astraeus/logger/Logger.jvm.kt @@ -0,0 +1,9 @@ +package nl.astraeus.logger + +import java.util.* + +actual fun getTimestamp(): String = Date().toString() + +actual fun getCurrentThread(): String? = Thread.currentThread().name + +actual fun getCaller(): String? = Throwable().stackTrace[2].toString() \ No newline at end of file