0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Android】Kotlin Logging × SLF4J で実現する柔軟なロギング基盤

Last updated at Posted at 2025-09-19

はじめに

Android や Kotlin マルチモジュール開発において、ログ管理は欠かせません。

  • 単純に android.util.Log を使うとコードが散らばる
  • 直接 SLF4J を使うと Kotlin らしさが弱い

最新バージョン

そこで登場するのが Kotlin Logging
これは SLF4J の軽量ラッパー であり、Kotlin スタイルのシンプルな記述を可能にします。


1. Kotlin Logging とは?

依存関係:

implementation("io.github.oshai:kotlin-logging-jvm:7.0.13")
implementation("org.slf4j:slf4j-api:2.0.17")

使い方:

import io.github.oshai.kotlinlogging.KotlinLogging

private val log = KotlinLogging.logger {}

fun login(user: String) {
    log.info { "User logged in: $user" }
    log.debug { "Debug details..." }
}

特徴

  • Kotlin の ラムダ構文で遅延評価可能(不要な文字列連結を避ける)
  • SLF4J に完全準拠 → バックエンドを差し替え可能

2. SLF4J と実装の関係

SLF4J 自体は Facade(門面) であり、実際の出力は「バインディング」によって決まります。

主なバインディング

  • slf4j-androidLogcat 出力
  • slf4j-simple → 標準エラー出力(主にテスト用)
  • slf4j-nop → すべてのログを無効化
  • logback-classic → サーバーで定番、強力な設定可能

3. Android × Kotlin Logging 実践構成

libs.versions.toml

[versions]
slf4j = "2.0.17"
klogging = "7.0.13"

[libraries]
klogging-jvm     = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "klogging" }
klogging-android = { module = "io.github.oshai:kotlin-logging-android", version.ref = "klogging" }
slf4j-api        = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
slf4j-android    = { module = "uk.uuid.slf4j:slf4j-android", version = "2.0.17-0" }
slf4j-simple     = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" }
slf4j-nop        = { module = "org.slf4j:slf4j-nop", version.ref = "slf4j" }

app/build.gradle.kts

dependencies {
    implementation(libs.slf4j.api)
    implementation(libs.klogging.jvm)

    // Debug: Logcat 出力
    debugImplementation(libs.slf4j.android)

    // Release: ログ無効化
    releaseImplementation(libs.slf4j.nop)

    // テスト: コンソール出力
    testImplementation(libs.slf4j.simple)
}

4. マルチモジュールでの推奨依存

  • domain/data 層slf4j-api + kotlin-logging のみ
  • app 層 → 実際の実装(android / nop)を切り替え
  • test 層slf4j-simple

こうすることで ログの依存を上位層に閉じ込められる
実装を差し替えても domain コードは一切変更不要。


5. メリットまとめ

  • Kotlin らしい簡潔なログ記述
  • SLF4J を介してバックエンドを柔軟に切り替え可能
  • 環境ごとに「出す/出さない」を制御可能(Debug=Logcat, Release=NOP)
  • マルチモジュールでも責務を分離できる

6. まとめ

  • Kotlin Logging 7.0.13 + SLF4J 2.0.17 が現時点の安定構成
  • Android 開発では debug=slf4j-androidrelease=slf4j-noptest=slf4j-simple がベストプラクティス
  • マルチモジュールでは domain/data は API のみ依存し、app 層で実装を注入する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?