Skip to content

Commit

Permalink
Merge pull request #46 from saschpe/filelogger-api
Browse files Browse the repository at this point in the history
FileLogger API improvement
  • Loading branch information
saschpe authored May 22, 2024
2 parents 93a3fee + 387f744 commit 0206999
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.1] - 2024-05-22
- FileLogger: Remove constructor with `kotlinx.io.files.Path`
Avoids having to add kotlinx-io as a dependency when using the class

## [1.3.0] - 2024-05-02
- Provide FileLogger for persistent logging to a file path with rotation and retention policy support.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
}

dependencies {
implementation("de.peilicke.sascha:log4k:1.3.0")
implementation("de.peilicke.sascha:log4k:1.3.1")
}
```

Expand Down
2 changes: 1 addition & 1 deletion log4k-slf4j/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {
}

group = "de.peilicke.sascha"
version = "1.3.0"
version = "1.3.1"

publishing {
publications.withType<MavenPublication> {
Expand Down
12 changes: 6 additions & 6 deletions log4k/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ kotlin {

sourceSets {
commonMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0-RC.2")
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.3.3")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0")
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.3.5")
}
commonTest.dependencies {
implementation(kotlin("test"))
}
jsMain.dependencies {
implementation("org.jetbrains.kotlin-wrappers:kotlin-node:20.11.30-pre.738")
implementation("org.jetbrains.kotlin-wrappers:kotlin-node:20.11.30-pre.751")
}
jsTest.dependencies {
implementation(kotlin("test-js"))
}
jvmTest.dependencies {
implementation("io.mockk:mockk:1.13.10")
implementation("io.mockk:mockk:1.13.11")
}
// https://kotlinlang.org/docs/multiplatform-android-layout.html#check-the-relationship-between-android-and-common-tests
val androidUnitTest by getting {
dependencies {
implementation("io.mockk:mockk-android:1.13.10")
implementation("io.mockk:mockk-android:1.13.11")
}
}
}
Expand All @@ -58,7 +58,7 @@ android {
}

group = "de.peilicke.sascha"
version = "1.3.0"
version = "1.3.1"

publishing {
publications.withType<MavenPublication> {
Expand Down
16 changes: 6 additions & 10 deletions log4k/src/commonMain/kotlin/saschpe/log4k/FileLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,29 @@ internal expect fun limitFolderToFilesByCreationTime(path: String, limit: Int)
*
* @param rotate Log file rotation, defaults to [Rotate.Daily]
* @param limit Log file retention limit, defaults to [Limit.Files]
* @param logPath Log file path, defaults to platform-specific temporary directory [defaultLogPath]
* @param logPath Log file path, defaults to a platform-specific temporary directory
*/
class FileLogger(
private val rotate: Rotate = Rotate.Daily,
private val limit: Limit = Limit.Files(10),
private val logPath: Path = defaultLogPath,
private val logPath: String? = null,
) : Logger() {
constructor(
rotation: Rotate = Rotate.Daily,
limit: Limit = Limit.Files(),
logPath: String,
) : this(rotation, limit, Path(logPath))
private val logPathInternal = logPath?.let { Path(it) } ?: defaultLogPath

init {
SystemFileSystem.createDirectories(logPath)
SystemFileSystem.createDirectories(logPathInternal)
}

override fun print(level: Log.Level, tag: String, message: String?, throwable: Throwable?) {
val logTag = tag.ifEmpty { getTraceTag() }
SystemFileSystem.sink(rotate.logFile(logPath), append = true).buffered().apply {
SystemFileSystem.sink(rotate.logFile(logPathInternal), append = true).buffered().apply {
writeString("${level.name.first()}/$logTag: $message")
throwable?.let { writeString(" $throwable") }
writeString("\n")
rotate.lineWritten()
flush()
}
limit.enforce(logPath)
limit.enforce(logPathInternal)
}

private fun getTraceTag(): String = try {
Expand Down
14 changes: 7 additions & 7 deletions log4k/src/commonTest/kotlin/saschpe/log4k/FileLoggerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FileLoggerTest {
@Test
fun log_rotate_after_9_lines() {
// Arrange
val logger = FileLogger(rotation = TEST_ROTATE_AFTER_9, logPath = testLogPathString)
val logger = FileLogger(rotate = TEST_ROTATE_AFTER_9, logPath = testLogPathString)

// Act
logger.testMessages()
Expand All @@ -74,7 +74,7 @@ class FileLoggerTest {
@Ignore // The class works, but testing async I/O is hard across all platforms
fun log_rotate_after_5_lines() {
// Arrange
val logger = FileLogger(rotate = TEST_ROTATE_AFTER_5, logPath = testLogPath)
val logger = FileLogger(rotate = TEST_ROTATE_AFTER_5, logPath = testLogPathString)

// Act
logger.testMessages()
Expand All @@ -93,7 +93,7 @@ class FileLoggerTest {
@Test
fun log_rotate_daily() {
// Arrange
val logger = FileLogger(rotate = Rotate.Daily, logPath = testLogPath)
val logger = FileLogger(rotate = Rotate.Daily, logPath = testLogPathString)

// Act
logger.testMessages()
Expand All @@ -112,7 +112,7 @@ class FileLoggerTest {
@Test
fun log_rotate_never() {
// Arrange
val logger = FileLogger(rotate = Rotate.Never, logPath = testLogPath)
val logger = FileLogger(rotate = Rotate.Never, logPath = testLogPathString)

// Act
logger.testMessages()
Expand All @@ -134,7 +134,7 @@ class FileLoggerTest {
fun log_limit_not() {
// Arrange
val rotate = Rotate.After(7)
val logger = FileLogger(rotate, Limit.Not, testLogPath)
val logger = FileLogger(rotate, Limit.Not, testLogPathString)

// Act
logger.testMessages()
Expand All @@ -150,7 +150,7 @@ class FileLoggerTest {
fun log_limit_to_7_files() {
// Arrange
val rotate = Rotate.After(3)
val logger = FileLogger(rotate, Limit.Files(7), testLogPath)
val logger = FileLogger(rotate, Limit.Files(7), testLogPathString)

// Act
logger.testMessages()
Expand All @@ -165,7 +165,7 @@ class FileLoggerTest {
fun log_limit_to_4_files() {
// Arrange
val rotate = Rotate.After(5)
val logger = FileLogger(rotate, Limit.Files(4), testLogPath)
val logger = FileLogger(rotate, Limit.Files(4), testLogPathString)

// Act
logger.testMessages()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package saschpe.log4k

import kotlinx.io.files.Path
import saschpe.log4k.FileLogger.Rotate
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -23,7 +22,6 @@ class MultipleLoggersTest {
}

companion object {
private val TEST_LOG_PATH_STRING = "build/test/${MultipleLoggersTest::class.simpleName}"
private val TEST_LOG_PATH = Path(TEST_LOG_PATH_STRING)
private val TEST_LOG_PATH = "build/test/${MultipleLoggersTest::class.simpleName}"
}
}

0 comments on commit 0206999

Please sign in to comment.