Created
December 3, 2012 05:17
-
-
Save takuya-i/4192883 to your computer and use it in GitHub Desktop.
[android] Log utility
This file contains 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 jp.co.so_da.android.extension; | |
import android.util.Log; | |
/** | |
* ファイル名 + 行番号付きで、Log出力を行うUtility クラス<br> | |
* <h4>使い方</h4> | |
* アプリ起動時に、Logger{@link #tag}とLogger{@link #logDebug}に必要な値を設定して下さい。 | |
* @author Takuya Inoue | |
* | |
*/ | |
public class Logger { | |
/** LogCatのTag名 | |
* Applicationの起動時に設定推奨 | |
*/ | |
public static String tag ="SDE"; | |
/** | |
* Logを出力するかどうか. | |
* ファイル名 +Log番号で出力するので、かなり処理重いため、リリース時はfalseにしてください.デフォルト値はfalseが指定してあります。 | |
*/ | |
public static boolean logDebug = false; | |
/** | |
* Logレベル infoでの出力 | |
* @param msg 出力メッセージ | |
*/ | |
public void i(String msg) { | |
if(logDebug)Log.i(tag, getFileName() +" : "+ msg); | |
} | |
/** | |
* Logレベル infoでの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public void i(String msg, Throwable th) { | |
if(logDebug)Log.i(tag, getFileName() +" : "+ msg, th); | |
} | |
/** | |
* Logレベル verboseでの出力 | |
* @param msg 出力メッセージ | |
*/ | |
public void v(String msg){ | |
if(logDebug)Log.i(tag, getFileName() +" : "+ msg); | |
} | |
public void v(String msg, Throwable th){ | |
if(logDebug)Log.i(tag, getFileName() +" : "+ msg, th); | |
} | |
/** | |
* Logレベル verboseでの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public static void d(String msg){ | |
if(logDebug){ | |
Log.d(tag, getFileName() +" : "+ msg); | |
} | |
} | |
/** | |
* Logレベル debugでの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public static void d(String msg, Throwable th){ | |
if(logDebug)Log.d(tag, getFileName() +" : "+ msg, th); | |
} | |
/** | |
* Logレベル debugの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public static void w(String msg) { | |
if(logDebug)Log.w(tag, getFileName() +" : "+ msg); | |
} | |
/** | |
* Logレベル debugの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public static void w(String msg, Throwable th){ | |
if(logDebug)Log.w(tag, getFileName() +" : "+ msg, th); | |
} | |
/** | |
* Logレベル errorの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public static void e(String msg){ | |
if(logDebug)Log.e(tag, getFileName() +" : "+ msg); | |
} | |
/** | |
* Logレベル errorでの出力 | |
* @param msg 出力メッセージ | |
* @param th Throwableインタフェース | |
*/ | |
public static void e(String msg, Throwable th){ | |
if(logDebug)Log.e(tag, getFileName() +" : "+ msg, th); | |
} | |
/** | |
* ファイル名と行番号を取得 | |
* コールスタックで2階層上のファイル名と行番号を取得する。 | |
* @return ファイル名 + 行番号 | |
* @see https://github.com/kenjikitamura/KenjiRepository/blob/master/AndroidLogger/Logger.java | |
*/ | |
private static String getFileName(){ | |
Exception e = new Exception(); | |
StackTraceElement[] elements = e.getStackTrace(); | |
String ret = ""; | |
if (elements.length > 2){ | |
ret = "("+elements[2].getFileName(); | |
ret += ":"; | |
ret += elements[2].getLineNumber()+")"; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment