1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidでのログ出力

Last updated at Posted at 2018-08-02

やりたいこと

リリース時にログを出さないようにしたい。
Androidアプリ作成時、ログのUtilクラスを毎回作っていたため、
いつでもコピペで使えるようにメモ。

BuiltTypeでログのON/OFFを切り替えるため、gradleに一度書けばビルド時に自動で切り替わります。

LogUtil.kt
import android.util.Log

class LogUtil
{
    companion object {
        val logFlag = BuildConfig.LOGGING //gradleに記載

        //Verbose
        fun verbose (TAG: String, msg: String) {
            if (logFlag) { Log.v(TAG, msg) }
        }
        fun verbose (TAG: String, msg: String, tr: Throwable) {
            if (logFlag) { Log.v(TAG, msg, tr) }
        }

        //Debug
        fun debug (TAG: String, msg: String) {
            if (logFlag) {  Log.d(TAG, msg) }
        }
        fun debug (TAG: String, msg: String, tr: Throwable){
            if (logFlag) { Log.d(TAG, msg, tr) }
        }

        //Info
        fun info (TAG: String, msg: String) {
            if (logFlag) { Log.i(TAG, msg) }
        }
        fun info (TAG: String, msg: String, tr: Throwable) {
            if (logFlag) { Log.i(TAG, msg, tr) }
        }

        //Warning
        fun warning (TAG: String, msg: String) {
            if (logFlag) { Log.w(TAG, msg) }
        }
        fun warning (TAG: String, msg: String, tr: Throwable) {
            if (logFlag) { Log.w(TAG, msg, tr) }
        }

        //error
        fun error (TAG: String, msg: String) {
            if (logFlag) { Log.e(TAG, msg) }
        }
        fun error (TAG: String, msg: String, tr: Throwable) {
            if (logFlag) { Log.e(TAG, msg, tr) }
        }
    }
}
build.gradle
    buildTypes {
        debug {
            buildConfigField 'boolean', 'LOGGING', 'true'
        }
        release {
            buildConfigField 'boolean', 'LOGGING', 'false'
        }
    }

ログレベルの使い分けに悩んだら

ログレベルの使い分けで迷った時の参考サイト
とても丁寧に解説されています。
https://kokufu.blogspot.com/2014/02/android_1.html

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?