Skip to content

Commit ccf34a5

Browse files
authored
add javaMain module and remove duplication (#275)
javaMain is an abstraction for both jvm and android modules
1 parent 0ee8b30 commit ccf34a5

35 files changed

+253
-518
lines changed

build.gradle.kts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ kotlin {
9090
implementation(kotlin("test-annotations-common"))
9191
}
9292
}
93+
// common to jvm and android
94+
val javaMain by creating {
95+
dependsOn(commonMain)
96+
}
9397
val jvmMain by getting {
98+
dependsOn(javaMain)
9499
dependencies {
95100
compileOnly("org.slf4j:slf4j-api:${extra["slf4j_version"]}")
96101
}
@@ -109,7 +114,9 @@ kotlin {
109114
implementation("org.slf4j:jul-to-slf4j:${extra["slf4j_version"]}")
110115
}
111116
}
112-
val androidMain by getting {}
117+
val androidMain by getting {
118+
dependsOn(javaMain)
119+
}
113120
val androidTest by getting {
114121
dependencies {
115122
implementation(kotlin("test"))

src/androidMain/kotlin/io/github/oshai/KLogger.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,34 @@ public actual interface KLogger {
4040
public actual fun error(t: Throwable?, msg: () -> Any?)
4141

4242
/** Lazy add a log message if isTraceEnabled is true */
43-
public actual fun trace(marker: io.github.oshai.Marker?, msg: () -> Any?)
43+
public actual fun trace(marker: Marker?, msg: () -> Any?)
4444

4545
/** Lazy add a log message if isDebugEnabled is true */
46-
public actual fun debug(marker: io.github.oshai.Marker?, msg: () -> Any?)
46+
public actual fun debug(marker: Marker?, msg: () -> Any?)
4747

4848
/** Lazy add a log message if isInfoEnabled is true */
49-
public actual fun info(marker: io.github.oshai.Marker?, msg: () -> Any?)
49+
public actual fun info(marker: Marker?, msg: () -> Any?)
5050

5151
/** Lazy add a log message if isWarnEnabled is true */
52-
public actual fun warn(marker: io.github.oshai.Marker?, msg: () -> Any?)
52+
public actual fun warn(marker: Marker?, msg: () -> Any?)
5353

5454
/** Lazy add a log message if isErrorEnabled is true */
55-
public actual fun error(marker: io.github.oshai.Marker?, msg: () -> Any?)
55+
public actual fun error(marker: Marker?, msg: () -> Any?)
5656

5757
/** Lazy add a log message with throwable payload if isTraceEnabled is true */
58-
public actual fun trace(marker: io.github.oshai.Marker?, t: Throwable?, msg: () -> Any?)
58+
public actual fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?)
5959

6060
/** Lazy add a log message with throwable payload if isDebugEnabled is true */
61-
public actual fun debug(marker: io.github.oshai.Marker?, t: Throwable?, msg: () -> Any?)
61+
public actual fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?)
6262

6363
/** Lazy add a log message with throwable payload if isInfoEnabled is true */
64-
public actual fun info(marker: io.github.oshai.Marker?, t: Throwable?, msg: () -> Any?)
64+
public actual fun info(marker: Marker?, t: Throwable?, msg: () -> Any?)
6565

6666
/** Lazy add a log message with throwable payload if isWarnEnabled is true */
67-
public actual fun warn(marker: io.github.oshai.Marker?, t: Throwable?, msg: () -> Any?)
67+
public actual fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?)
6868

6969
/** Lazy add a log message with throwable payload if isErrorEnabled is true */
70-
public actual fun error(marker: io.github.oshai.Marker?, t: Throwable?, msg: () -> Any?)
70+
public actual fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)
7171

7272
/** Add a log message with all the supplied parameters along with method name */
7373
public actual fun entry(vararg argArray: Any?)

src/androidMain/kotlin/io/github/oshai/KotlinLogging.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package io.github.oshai.internal
22

3-
public actual typealias ErrorMessageProducer = DefaultErrorMessageProducer
3+
internal actual typealias ErrorMessageProducer = DefaultErrorMessageProducer
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.oshai.internal
2+
3+
import io.github.oshai.KLogger
4+
5+
@Suppress("NOTHING_TO_INLINE")
6+
internal actual object KLoggerFactory {
7+
8+
internal actual inline fun logger(name: String): KLogger = KLoggerAndroid(name)
9+
}

src/androidMain/kotlin/io/github/oshai/internal/KLoggerNameResolver.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/commonMain/kotlin/io/github/oshai/KLogger.kt

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,123 @@ public fun KLogger.isEnabledForLevel(level: Level): Boolean {
141141
else -> throw IllegalArgumentException("Level [$level] not recognized.")
142142
}
143143
}
144+
145+
@Suppress("TooManyFunctions")
146+
public interface ActualKLogger {
147+
148+
/**
149+
* Return the name of this `Logger` instance.
150+
* @return name of this logger instance
151+
*/
152+
public val name: String
153+
154+
/** Lazy add a log message if isTraceEnabled is true */
155+
public fun trace(msg: () -> Any?)
156+
157+
/** Lazy add a log message if isDebugEnabled is true */
158+
public fun debug(msg: () -> Any?)
159+
160+
/** Lazy add a log message if isInfoEnabled is true */
161+
public fun info(msg: () -> Any?)
162+
163+
/** Lazy add a log message if isWarnEnabled is true */
164+
public fun warn(msg: () -> Any?)
165+
166+
/** Lazy add a log message if isErrorEnabled is true */
167+
public fun error(msg: () -> Any?)
168+
169+
/** Lazy add a log message with throwable payload if isTraceEnabled is true */
170+
public fun trace(t: Throwable?, msg: () -> Any?)
171+
172+
/** Lazy add a log message with throwable payload if isDebugEnabled is true */
173+
public fun debug(t: Throwable?, msg: () -> Any?)
174+
175+
/** Lazy add a log message with throwable payload if isInfoEnabled is true */
176+
public fun info(t: Throwable?, msg: () -> Any?)
177+
178+
/** Lazy add a log message with throwable payload if isWarnEnabled is true */
179+
public fun warn(t: Throwable?, msg: () -> Any?)
180+
181+
/** Lazy add a log message with throwable payload if isErrorEnabled is true */
182+
public fun error(t: Throwable?, msg: () -> Any?)
183+
184+
/** Lazy add a log message if isTraceEnabled is true */
185+
public fun trace(marker: Marker?, msg: () -> Any?)
186+
187+
/** Lazy add a log message if isDebugEnabled is true */
188+
public fun debug(marker: Marker?, msg: () -> Any?)
189+
190+
/** Lazy add a log message if isInfoEnabled is true */
191+
public fun info(marker: Marker?, msg: () -> Any?)
192+
193+
/** Lazy add a log message if isWarnEnabled is true */
194+
public fun warn(marker: Marker?, msg: () -> Any?)
195+
196+
/** Lazy add a log message if isErrorEnabled is true */
197+
public fun error(marker: Marker?, msg: () -> Any?)
198+
199+
/** Lazy add a log message with throwable payload if isTraceEnabled is true */
200+
public fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?)
201+
202+
/** Lazy add a log message with throwable payload if isDebugEnabled is true */
203+
public fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?)
204+
205+
/** Lazy add a log message with throwable payload if isInfoEnabled is true */
206+
public fun info(marker: Marker?, t: Throwable?, msg: () -> Any?)
207+
208+
/** Lazy add a log message with throwable payload if isWarnEnabled is true */
209+
public fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?)
210+
211+
/** Lazy add a log message with throwable payload if isErrorEnabled is true */
212+
public fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)
213+
214+
/** Add a log message with all the supplied parameters along with method name */
215+
public fun entry(vararg argArray: Any?)
216+
217+
/** Add log message indicating exit of a method */
218+
public fun exit()
219+
220+
/** Add a log message with the return value of a method */
221+
public fun <T> exit(result: T): T where T : Any?
222+
223+
/** Add a log message indicating an exception will be thrown along with the stack trace. */
224+
public fun <T> throwing(throwable: T): T where T : Throwable
225+
226+
/** Add a log message indicating an exception is caught along with the stack trace. */
227+
public fun <T> catching(throwable: T) where T : Throwable
228+
229+
/**
230+
* Is the logger instance enabled for the TRACE level?
231+
*
232+
* @return True if this Logger is enabled for the TRACE level, false otherwise.
233+
*/
234+
public val isTraceEnabled: Boolean
235+
236+
/**
237+
* Is the logger instance enabled for the DEBUG level?
238+
*
239+
* @return True if this Logger is enabled for the DEBUG level, false otherwise.
240+
*/
241+
public val isDebugEnabled: Boolean
242+
243+
/**
244+
* Is the logger instance enabled for the INFO level?
245+
*
246+
* @return True if this Logger is enabled for the INFO level, false otherwise.
247+
*/
248+
public val isInfoEnabled: Boolean
249+
250+
/**
251+
* Is the logger instance enabled for the WARN level?
252+
*
253+
* @return True if this Logger is enabled for the WARN level, false otherwise.
254+
*/
255+
public val isWarnEnabled: Boolean
256+
257+
/**
258+
* Is the logger instance enabled for the ERROR level?
259+
*
260+
* @return True if this Logger is enabled for the ERROR level, false otherwise.
261+
*/
262+
public val isErrorEnabled: Boolean
263+
}

src/commonMain/kotlin/io/github/oshai/KMarkerFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package io.github.oshai
33
/** A platform independent factory to create markers. */
44
public object KMarkerFactory {
55

6-
public fun getMarker(name: String): io.github.oshai.Marker = io.github.oshai.SimpleMarker(name)
6+
public fun getMarker(name: String): Marker = SimpleMarker(name)
77
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
package io.github.oshai
22

3-
public expect object KotlinLogging {
3+
import io.github.oshai.internal.KLoggerFactory
4+
import io.github.oshai.internal.KLoggerNameResolver
5+
6+
public object KotlinLogging {
47
/**
58
* This method allow defining the logger in a file in the following way:
69
* ```
7-
* val logger = KotlinLogging.logger {}
10+
* private val logger = KotlinLogging.logger {}
811
* ```
912
*/
10-
public fun logger(func: () -> Unit): KLogger
13+
public fun logger(func: () -> Unit): KLogger = logger(KLoggerNameResolver.name(func))
1114

12-
public fun logger(name: String): KLogger
15+
/**
16+
* This method allow defining the logger in a file in the following way:
17+
* ```
18+
* private val logger = KotlinLogging.logger("io.github.oshai.MyLogger")
19+
* ```
20+
* In most cases the name represents the package notation of the file that the logger is defined
21+
* in.
22+
*/
23+
public fun logger(name: String): KLogger = KLoggerFactory.logger(name)
1324
}

src/commonMain/kotlin/io/github/oshai/Marker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public interface Marker {
66
public fun getName(): String
77
}
88

9-
internal data class SimpleMarker(private val name: String) : io.github.oshai.Marker {
9+
internal data class SimpleMarker(private val name: String) : Marker {
1010
override fun getName(): String = this.name
1111
}

0 commit comments

Comments
 (0)