Last active
November 10, 2015 04:10
-
-
Save esperia/1883723 to your computer and use it in GitHub Desktop.
Androidでログを吐くためのクラス。クラス名とメソッド名はStackTraceから取ってきてるので、Log.d(LOG_TAG, "関数名, 文字列"); って書かなくても Logger.d("文字列"); でいける
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.esperia09.android.utils; | |
import com.esperia09.android.BuildConfig; | |
import android.util.Log; | |
public class Logger { | |
private static final boolean debug = BuildConfig.DEBUG; | |
private static final int TRACE_CALLER_COUNT = 2; | |
public static void v() { | |
if (debug) { | |
Log.v(getClassName(), getFunctionName()); | |
} | |
} | |
public static void v(String msg) { | |
if (debug) { | |
Log.v(getClassName(), getFunctionName() + ", " + nonNull(msg)); | |
} | |
} | |
public static void d() { | |
if (debug) { | |
Log.d(getClassName(), getFunctionName()); | |
} | |
} | |
public static void d(String msg) { | |
if (debug) { | |
Log.d(getClassName(), getFunctionName() + ", " + nonNull(msg)); | |
} | |
} | |
public static void i() { | |
if (debug) { | |
Log.i(getClassName(), getFunctionName()); | |
} | |
} | |
public static void i(String msg) { | |
if (debug) { | |
Log.i(getClassName(), getFunctionName() + ", " + nonNull(msg)); | |
} | |
} | |
public static void w(String msg) { | |
if (debug) { | |
Log.w(getClassName(), getFunctionName() + ", " + nonNull(msg)); | |
} | |
} | |
public static void w(String msg, Throwable e) { | |
if (debug) { | |
Log.w(getClassName(), getFunctionName() + ", " + nonNull(msg), e); | |
} | |
} | |
public static void e(String msg) { | |
if (debug) { | |
Log.e(getClassName(), getFunctionName() + ", " + nonNull(msg)); | |
} | |
} | |
public static void e(String msg, Throwable e) { | |
if (debug) { | |
Log.e(getClassName(), getFunctionName() + ", " + nonNull(msg), e); | |
} | |
} | |
private static String nonNull(String s) { | |
if (s == null) { | |
return "(null)"; | |
} | |
return s; | |
} | |
private static String getClassName() { | |
String fn = ""; | |
try { | |
fn = new Throwable().getStackTrace()[TRACE_CALLER_COUNT].getClassName(); | |
} catch (Exception e) { | |
} | |
return fn; | |
} | |
private static String getFunctionName() { | |
String fn = ""; | |
try { | |
fn = new Throwable().getStackTrace()[TRACE_CALLER_COUNT].getMethodName(); | |
} catch (Exception e) { | |
} | |
return fn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あれ?ライセンス書くところなかったっけな。。
ライセンスはpublic licenseです。