Skip to content

Commit 1122901

Browse files
committed
Initial commit
0 parents  commit 1122901

File tree

19 files changed

+1074
-0
lines changed

19 files changed

+1074
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.{kt,kts,gradle}]
10+
indent_style = space
11+
indent_size = 4
12+
continuation_indent_size = 4

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Hidden files
2+
.*
3+
4+
# Temporary files
5+
*~
6+
7+
# Git
8+
!.git*
9+
10+
# EditorConfig
11+
!.editorconfig
12+
13+
# IntelliJ Idea
14+
out/
15+
*.iml
16+
*.ipr
17+
*.iws
18+
19+
# Travis CI
20+
!.travis.yml
21+
22+
# Gradle
23+
build/
24+
25+
# JVM error logs
26+
hs_err_pid*.log
27+
replay_pid*.log

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: java
2+
jdk:
3+
- oraclejdk8
4+
5+
notifications:
6+
email: false

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2018 Michael Bull (https://www.michael-bull.com)
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# kotlin-inline-logger
2+
3+
[![Release](https://api.bintray.com/packages/michaelbull/maven/kotlin-inline-logger/images/download.svg)](https://bintray.com/michaelbull/maven/kotlin-inline-logger/_latestVersion) [![Build Status](https://travis-ci.org/michaelbull/kotlin-inline-logger.svg?branch=master)](https://travis-ci.org/michaelbull/kotlin-inline-logger) [![License](https://img.shields.io/github/license/michaelbull/kotlin-inline-logger.svg)](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+
![ReplaceWith example](replacewith-example.gif)
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

build.gradle.kts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import com.jfrog.bintray.gradle.BintrayExtension
2+
import com.jfrog.bintray.gradle.tasks.BintrayUploadTask
3+
4+
description = "A logger facilitating lazily-evaluated log calls via Kotlin's inline classes & functions."
5+
6+
plugins {
7+
`maven-publish`
8+
kotlin("multiplatform") version ("1.3.31")
9+
id("com.github.ben-manes.versions") version ("0.21.0")
10+
id("com.jfrog.bintray") version ("1.8.4")
11+
id("net.researchgate.release") version ("2.8.0")
12+
}
13+
14+
repositories {
15+
mavenCentral()
16+
jcenter()
17+
}
18+
19+
kotlin {
20+
sourceSets {
21+
named("commonMain") {
22+
dependencies {
23+
implementation(kotlin("stdlib-common"))
24+
}
25+
}
26+
27+
named("commonTest") {
28+
dependencies {
29+
implementation(kotlin("test-common"))
30+
implementation(kotlin("test-annotations-common"))
31+
}
32+
}
33+
}
34+
35+
jvm {
36+
compilations.named("main") {
37+
kotlinOptions {
38+
jvmTarget = "1.8"
39+
freeCompilerArgs = listOf("-Xno-call-assertions", "-Xno-receiver-assertions", "-Xno-param-assertions")
40+
}
41+
42+
dependencies {
43+
implementation(kotlin("stdlib-jdk8"))
44+
implementation("org.slf4j:slf4j-api:1.7.26")
45+
}
46+
}
47+
48+
compilations.named("test") {
49+
kotlinOptions {
50+
jvmTarget = "1.8"
51+
freeCompilerArgs = listOf("-Xno-call-assertions", "-Xno-receiver-assertions", "-Xno-param-assertions")
52+
}
53+
54+
dependencies {
55+
implementation(kotlin("test"))
56+
implementation(kotlin("test-junit"))
57+
}
58+
}
59+
}
60+
}
61+
62+
fun BintrayExtension.pkg(configure: BintrayExtension.PackageConfig.() -> Unit) {
63+
pkg(delegateClosureOf(configure))
64+
}
65+
66+
val bintrayUser: String? by project
67+
val bintrayKey: String? by project
68+
69+
bintray {
70+
user = bintrayUser
71+
key = bintrayKey
72+
setPublications("jvm", "metadata")
73+
74+
pkg {
75+
repo = "maven"
76+
name = project.name
77+
desc = project.description
78+
websiteUrl = "https://github.com/michaelbull/kotlin-inline-logger"
79+
issueTrackerUrl = "https://github.com/michaelbull/kotlin-inline-logger/issues"
80+
vcsUrl = "[email protected]:michaelbull/kotlin-inline-logger.git"
81+
githubRepo = "michaelbull/kotlin-inline-logger"
82+
setLicenses("ISC")
83+
}
84+
}
85+
86+
val bintrayUpload by tasks.existing(BintrayUploadTask::class) {
87+
dependsOn("build")
88+
dependsOn("generatePomFileForJvmPublication")
89+
dependsOn("generatePomFileForMetadataPublication")
90+
}
91+
92+
tasks.named("afterReleaseBuild") {
93+
dependsOn(bintrayUpload)
94+
}

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
group=com.michael-bull.kotlin-inline-logger
2+
version=1.0.0

gradle/wrapper/gradle-wrapper.jar

54.9 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)