|
| 1 | +# kotlin-inline-logger |
| 2 | + |
| 3 | +[](https://bintray.com/michaelbull/maven/kotlin-inline-logger/_latestVersion) [](https://travis-ci.org/michaelbull/kotlin-inline-logger) [](https://github.com/michaelbull/kotlin-inline-logger/blob/master/LICENSE) |
| 4 | + |
| 5 | +A logger facilitating lazily-evaluated log calls via Kotlin's inline [classes][inline-classes] & [functions][inline-functions]. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```groovy |
| 10 | +repositories { |
| 11 | + maven { url = 'https://dl.bintray.com/michaelbull/maven' } |
| 12 | +} |
| 13 | +
|
| 14 | +dependencies { |
| 15 | + compile 'com.michael-bull.kotlin-inline-logger:kotlin-inline-logger-jvm:1.0.0' |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +## Introduction |
| 20 | + |
| 21 | +`kotlin-inline-logger` has been structured as a [`multiplatform project`][mpp]. |
| 22 | +Currently the only implementation supports the JVM, utilising [SLF4J][slf4j], |
| 23 | +however in future other platforms can also be supported by implementing the |
| 24 | +necessary platform-specific APIs found in [`src/commonMain`](src/commonMain). |
| 25 | + |
| 26 | +If you would like to implement support for a platform, please don't hesitate |
| 27 | +to open a pull request on [GitHub][github]. |
| 28 | + |
| 29 | +### JVM |
| 30 | + |
| 31 | +On the JVM, `kotlin-inline-logger` provides inline functions that delegate |
| 32 | +to [SLF4J][slf4j]. Users migrating from an existing SLF4J solution will find |
| 33 | +their existing calls to methods, e.g. `logger.info("Example")` now marked as |
| 34 | +`@Deprecated`, prompting them to replace their existing code with the |
| 35 | +lazily-evaluated alternatives. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +#### Creating loggers |
| 40 | + |
| 41 | +By default, passing no argument when creating an `InlineLogger()` will utilise |
| 42 | +`MethodHandles.lookup()`, introduced in JDK7, to derive the name of the logger |
| 43 | +from the class that created it. This is described in SLF4J's documentation as |
| 44 | +an [idiomatic method of declaring a logger][slf4j-idiom] that is resistant to |
| 45 | +cut-and-pasting between classes. |
| 46 | + |
| 47 | +Named loggers can be created by passing a name: |
| 48 | + |
| 49 | +```kotlin |
| 50 | +val transactionLogger = InlineLogger("DatabaseTransactions") |
| 51 | +``` |
| 52 | + |
| 53 | +Class loggers can be created by passing the `::class` reference: |
| 54 | + |
| 55 | +```kotlin |
| 56 | +class TransactionExecutor |
| 57 | + |
| 58 | +val transactionLogger = InlineLogger(TransactionExecutor::class) |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | +#### Effectiveness |
| 63 | + |
| 64 | +The code examples below illustrate how the Kotlin you write is compiled to |
| 65 | +interoperable Java, demonstrating how the `expensiveCalculation()` function is |
| 66 | +only invoked and interpolated with the log message if warn-level logging is |
| 67 | +enabled. |
| 68 | + |
| 69 | +This allows you to replace usages of SLF4J's [MessageFormatter][slf4j-formatter], |
| 70 | +with the more idiomatic [string templates][string-templates] Kotlin provides. |
| 71 | + |
| 72 | +##### Kotlin: |
| 73 | + |
| 74 | +```kotlin |
| 75 | +import com.github.michaelbull.logging.InlineLogger |
| 76 | + |
| 77 | +val logger = InlineLogger() |
| 78 | + |
| 79 | +fun expensiveCalculation(): Int { |
| 80 | + return 1234 * 5678 |
| 81 | +} |
| 82 | + |
| 83 | +fun main() { |
| 84 | + logger.warn { "The result is: ${expensiveCalculation()}" } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +##### Compiled Java: |
| 89 | + |
| 90 | +```java |
| 91 | +import com.github.michaelbull.logging.InlineLogger; |
| 92 | +import java.lang.invoke.MethodHandles; |
| 93 | +import kotlin.Metadata; |
| 94 | +import org.jetbrains.annotations.NotNull; |
| 95 | +import org.slf4j.Logger; |
| 96 | +import org.slf4j.LoggerFactory; |
| 97 | + |
| 98 | +public final class ExampleKt { |
| 99 | + @NotNull |
| 100 | + private static final Logger logger; |
| 101 | + |
| 102 | + @NotNull |
| 103 | + public static final Logger getLogger() { |
| 104 | + return logger; |
| 105 | + } |
| 106 | + |
| 107 | + public static final int expensiveCalculation() { |
| 108 | + return 1234 * 5678; |
| 109 | + } |
| 110 | + |
| 111 | + public static final void main() { |
| 112 | + Logger $this$iv = logger; |
| 113 | + boolean $i$f$warn = false; |
| 114 | + if (InlineLogger.isWarnEnabled-impl((Logger)$this$iv)) { |
| 115 | + Logger logger = $this$iv; |
| 116 | + boolean bl = false; |
| 117 | + String string = "The result is: " + ExampleKt.expensiveCalculation(); |
| 118 | + logger.warn(String.valueOf(string)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public static /* synthetic */ void main(String[] arrstring) { |
| 123 | + ExampleKt.main(); |
| 124 | + } |
| 125 | + |
| 126 | + static { |
| 127 | + boolean $i$f$InlineLogger = false; |
| 128 | + Logger delegate$iv = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 129 | + logger = InlineLogger.constructor-impl((Logger)delegate$iv); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Contributing |
| 135 | + |
| 136 | +Bug reports and pull requests are welcome on [GitHub][github]. |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +This project is available under the terms of the ISC license. See the |
| 141 | +[`LICENSE`](LICENSE) file for the copyright information and licensing terms. |
| 142 | + |
| 143 | +[inline-classes]: https://kotlinlang.org/docs/reference/inline-classes.html |
| 144 | +[inline-functions]: https://kotlinlang.org/docs/reference/inline-functions.html |
| 145 | +[mpp]: https://kotlinlang.org/docs/reference/multiplatform.html |
| 146 | +[string-templates]: https://kotlinlang.org/docs/reference/basic-types.html#string-templates |
| 147 | +[github]: https://github.com/michaelbull/kotlin-inline-logger |
| 148 | +[slf4j]: https://www.slf4j.org/ |
| 149 | +[slf4j-idiom]: https://www.slf4j.org/faq.html#declaration_pattern |
| 150 | +[slf4j-formatter]: https://www.slf4j.org/api/org/slf4j/helpers/MessageFormatter.html |
0 commit comments