From 7eb4d0934c1e0ed0ebfeb2ea774a9faed3b33dc5 Mon Sep 17 00:00:00 2001 From: Django Cass Date: Sat, 7 Dec 2019 20:04:24 +1100 Subject: [PATCH] Added support for including throwables in log messages Updated README.md Updated Gradle to 6.0.1 --- README.md | 50 +++++++----- build.gradle.kts | 20 +++-- gradle/wrapper/gradle-wrapper.jar | Bin 55190 -> 55616 bytes gradle/wrapper/gradle-wrapper.properties | 2 +- gradlew | 22 +++++- gradlew.bat | 18 ++++- .../kotlin/dev/castive/log2/Extensions.kt | 30 ++++--- src/main/kotlin/dev/castive/log2/Log.kt | 74 +++++++++++------- src/test/kotlin/dev/castive/log2/LogTest.kt | 8 ++ 9 files changed, 157 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 5d1ba01..44b504a 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,36 @@ # LOG2 -Log2 is a simple library for showing pretty log statements. +Log2 is a library for simplifying logging. It uses Slf4j internally and therefore will not interfere with any custom logging solutions. -It is written in Kotlin and therefore should be fully interoperable with Java +It is written in Kotlin and therefore should be fully interoperable with Java. +If you are using ONLY Java, I'd recommend using Lombok's `@Slf4j` annotation instead. -Log2 is a rewrite of a project by the same name ([Log2](https://gitlab.com/django-sandbox/log2)) -### Download +Log2 is designed at the Java 11 language level and therefore may or may not work in lower versions of Java. YMMV. + +### Setup Gradle: 1. In your root build.gradle -```gradle -allprojects { - repositories { - ... - maven { url 'https://jitpack.io' } - } +```groovy +repositories { + maven { url 'https://jitpack.io' } +} +``` +```kotlin +repositories { + maven(url = "https://jitpack.io") } ``` 2. In your project build.gradle -```gradle +```groovy +dependencies { + implementation 'com.github.djcass44:log2:4.1' +} +``` +```kotlin dependencies { - implementation 'com.github.djcass44:log2:3.0' + implementation("com.github.djcass44:log2:4.1") } ``` Maven: @@ -40,7 +49,7 @@ Maven: com.github.djcass44 log2 - 3.0 + 4.1 ``` @@ -50,15 +59,20 @@ Maven: Log.e(javaClass, "An error occurred!") // Will print // 1970-01-01 00:00:00 000 | [main] |-ERROR in org.example.test.TestLog - An error occurred! +// kotlin extensions +"An error has occurred!".loge(javaClass) +try { + something.throwsErr() +} +catch (e: Exception) { + // include a throwable to print to console + "Caught exception in somemethod".logf(javaClass, e) +} ``` -**Additional options** - -```Log.USE_SHORT_COLOURS```(default: true): Only colour the part of the log which shows severity - ### License -Log2 is releasesed under the [Apache 2.0 license](LICENSE) +Log2 is released under the [Apache 2.0 license](LICENSE) ``` Copyright 2019 Django Cass diff --git a/build.gradle.kts b/build.gradle.kts index ca25a5a..0f33d18 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,7 +24,7 @@ plugins { } group = "dev.castive" -version = "4.0" +version = "4.1" val moduleName by extra("dev.castive.log2") val javaHome: String = System.getProperty("java.home") @@ -36,11 +36,17 @@ repositories { dependencies { implementation(kotlin("stdlib-jdk8")) - implementation("org.slf4j:slf4j-simple:1.7.29") - testImplementation("org.junit.jupiter:junit-jupiter-api:5.2.0") - testImplementation("org.junit.jupiter:junit-jupiter-params:5.2.0") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0") + val slfVersion = "1.7.29" + implementation("org.slf4j:slf4j-api:$slfVersion") + + + testImplementation("org.slf4j:slf4j-simple:$slfVersion") + + val junitVersion = "5.5.2" + testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") + testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion") } configure { sourceCompatibility = JavaVersion.VERSION_11 @@ -48,8 +54,8 @@ configure { } tasks { withType { - gradleVersion = "5.6.4" - distributionType = Wrapper.DistributionType.BIN + gradleVersion = "6.0.1" + distributionType = Wrapper.DistributionType.ALL } withType().all { kotlinOptions.jvmTarget = "11" diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 87b738cbd051603d91cc39de6cb000dd98fe6b02..5c2d1cf016b3885f6930543d57b744ea8c220a1a 100644 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g(k*UE#@ zrJm|m^EV_6jz}f($PrOb`S;imdEwtu`#cCu3aMXBgUUH4t2j_qu=KmOO645(v(_DL z^G5PF%RR0@X5D{(V%x5L{xD1Sa>^wR+$0j(DeVfwk;tp3<@i$~qOsvx^uUy!zV8G0~0`$f?VV=?vm zOwYnZB>UV_b#sh6ibtN`5I+l%mTE9T%*J!xaz}cWisUNLg@>nEiKv4hgmv`5C)GIDbBOgq{?5K-!=>z{CLJ$wIBkL-~yV{}~e*^#eZ1f%)RR;DgcM zfOqnA#42!t$D;@!QT3n50ve1d0$Zl^m}ABc){bz2HDhq#o&{ZLlQ=*lO9Alv7y_uW z`bTL2KkVsP<{%6$`1yeL}DmCZuxPZRJp*( z*Kk1M23@g@UjhQ6PEZ{58CL@Aqv>cB0|#ltT;SR`95{}ptMe0@zz&v<>j{GNDt-bE zn5EFw?u0e)Ee+J0^aq@C>E_j>A%MyU^@?Rcohe{^TCd{d<=ub5$bWAh) = Log.v(src.name, this) +public fun String.logv(src: Class, t: Throwable? = null) = Log.v(src.name, this, t) /** * Show a debug message * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logd(src: Class) = Log.d(src.name, this) +public fun String.logd(src: Class, t: Throwable? = null) = Log.d(src.name, this, t) /** * Show an information message * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logi(src: Class) = Log.i(src.name, this) +public fun String.logi(src: Class, t: Throwable? = null) = Log.i(src.name, this, t) /** * Show an ok message * Indicates that something is working * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logok(src: Class) = Log.ok(src.name, this) +public fun String.logok(src: Class, t: Throwable? = null) = Log.ok(src.name, this, t) /** * Show a good message * Indicates that a task completed correctly or something went as expected * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logg(src: Class) = Log.good(src.name, this) +public fun String.logg(src: Class, t: Throwable? = null) = Log.good(src.name, this, t) /** * Show an alert message * Useful for highlighting something to the user * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.loga(src: Class) = Log.a(src.name, this) +public fun String.loga(src: Class, t: Throwable? = null) = Log.a(src.name, this, t) /** * Show a warning message * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logw(src: Class) = Log.w(src.name, this) +public fun String.logw(src: Class, t: Throwable? = null) = Log.w(src.name, this, t) /** * Show an error message * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.loge(src: Class) = Log.e(src.name, this) +public fun String.loge(src: Class, t: Throwable? = null) = Log.e(src.name, this, t) /** * Show a fatal message * Fatal messages indicate critical failure that cannot be recovered from * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logf(src: Class) = Log.f(src.name, this) +public fun String.logf(src: Class, t: Throwable? = null) = Log.f(src.name, this, t) /** * Show a silent message * Silent messages are always shown and cannot be hidden * @param src Calling class + * @param t the exception (throwable) to log */ -public fun String.logs(src: Class) = Log.s(src.name, this) \ No newline at end of file +public fun String.logs(src: Class, t: Throwable? = null) = Log.s(src.name, this, t) \ No newline at end of file diff --git a/src/main/kotlin/dev/castive/log2/Log.kt b/src/main/kotlin/dev/castive/log2/Log.kt index 5648833..7d1ae0d 100644 --- a/src/main/kotlin/dev/castive/log2/Log.kt +++ b/src/main/kotlin/dev/castive/log2/Log.kt @@ -31,154 +31,174 @@ object Log { * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun v(src: Class, msg: String) = v(src.name, msg) + fun v(src: Class, msg: String, t: Throwable? = null) = v(src.name, msg, t) /** * Show a debug message * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun d(src: Class, msg: String) = d(src.name, msg) + fun d(src: Class, msg: String, t: Throwable? = null) = d(src.name, msg, t) /** * Show an information message * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun i(src: Class, msg: String) = i(src.name, msg) + fun i(src: Class, msg: String, t: Throwable? = null) = i(src.name, msg, t) /** * Show an ok message * Indicates that something is working * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun ok(src: Class, msg: String) = ok(src.name, msg) + fun ok(src: Class, msg: String, t: Throwable? = null) = ok(src.name, msg, t) /** * Show a good message * Indicates that a task completed correctly or something went as expected * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun good(src: Class, msg: String) = good(src.name, msg) + fun good(src: Class, msg: String, t: Throwable? = null) = good(src.name, msg, t) /** * Show an alert message * Useful for highlighting something to the user * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun a(src: Class, msg: String) = a(src.name, msg) + fun a(src: Class, msg: String, t: Throwable? = null) = a(src.name, msg, t) /** * Show a warning message * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun w(src: Class, msg: String) = w(src.name, msg) + fun w(src: Class, msg: String, t: Throwable? = null) = w(src.name, msg, t) /** * Show an error message * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun e(src: Class, msg: String) = e(src.name, msg) + fun e(src: Class, msg: String, t: Throwable? = null) = e(src.name, msg, t) /** * Show a fatal message * Fatal messages indicate critical failure that cannot be recovered from * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun f(src: Class, msg: String) = f(src.name, msg) + fun f(src: Class, msg: String, t: Throwable? = null) = f(src.name, msg, t) /** * Show a silent message * Silent messages are always shown and cannot be hidden. * * @param src Calling class * @param msg Message to show + * @param t the exception (throwable) to log */ - fun s(src: Class, msg: String) = s(src.name, msg) + fun s(src: Class, msg: String, t: Throwable? = null) = s(src.name, msg, t) /** * Show a verbose message * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun v(src: String, msg: String) = log(src, msg, Level.TRACE) + fun v(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.TRACE, t) /** * Show a debug message * Indicates that something is working * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun d(src: String, msg: String) = log(src, msg, Level.DEBUG) + fun d(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.DEBUG, t) /** * Show an information message * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun i(src: String, msg: String) = log(src, msg, Level.INFO) + fun i(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.INFO, t) /** * Show an ok message * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun ok(src: String, msg: String) = log(src, msg, Level.INFO) + fun ok(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.INFO, t) /** * Show a good message * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun good(src: String, msg: String) = log(src, msg, Level.INFO) + fun good(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.INFO, t) /** * Show an alert message * Useful for highlighting something to the user * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun a(src: String, msg: String) = log(src, msg, Level.WARN) + fun a(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.WARN, t) /** * Show a warning message * Indicates that a task completed correctly or something went as expected * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun w(src: String, msg: String) = log(src, msg, Level.WARN) + fun w(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.WARN, t) /** * Show an error message * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun e(src: String, msg: String) = log(src, msg, Level.WARN) + fun e(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.WARN, t) /** * Show a fatal message * Fatal messages indicate critical failure that cannot be recovered from * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun f(src: String, msg: String) = log(src, msg, Level.ERROR) + fun f(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.ERROR, t) /** * Show a silent message * Silent messages are always shown and cannot be hidden. * * @param src Calling classname or other indication of source * @param msg Message to show + * @param t the exception (throwable) to log */ - fun s(src: String, msg: String) = log(src, msg, Level.ERROR) + fun s(src: String, msg: String, t: Throwable? = null) = log(src, msg, Level.ERROR, t) - private fun log(name: String, msg: String, level: Level) { + private fun log(name: String, msg: String, level: Level, t: Throwable? = null) { val logger = handlers[name] ?: run { // We couldn't find an existing logger, so lets make a new one val newLogger = LoggerFactory.getLogger(name) @@ -186,13 +206,13 @@ object Log { return@run newLogger } when(level) { - Level.TRACE -> logger.trace(msg) - Level.DEBUG -> logger.debug(msg) - Level.INFO -> logger.info(msg) - Level.WARN -> logger.warn(msg) - Level.ERROR -> logger.error(msg) + Level.TRACE -> logger.trace(msg, t) + Level.DEBUG -> logger.debug(msg, t) + Level.INFO -> logger.info(msg, t) + Level.WARN -> logger.warn(msg, t) + Level.ERROR -> logger.error(msg, t) // assume info if we don't recognise the level - else -> logger.info(msg) + else -> logger.info(msg, t) } } diff --git a/src/test/kotlin/dev/castive/log2/LogTest.kt b/src/test/kotlin/dev/castive/log2/LogTest.kt index 2b8e800..fe2b4ba 100755 --- a/src/test/kotlin/dev/castive/log2/LogTest.kt +++ b/src/test/kotlin/dev/castive/log2/LogTest.kt @@ -31,4 +31,12 @@ class LogTest { Log.f(javaClass, "Fatal error test") Log.s(javaClass, "Silent test") } + + @Test + fun `test exceptions are handled correctly`() { + val e = Exception("Test exception") + + Log.i(javaClass, "Info test", e) + "Info test 2".logi(javaClass, e) + } } \ No newline at end of file