Skip to content

Instantly share code, notes, and snippets.

@esperia
Last active November 10, 2015 04:10
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esperia/1883723 to your computer and use it in GitHub Desktop.
Save esperia/1883723 to your computer and use it in GitHub Desktop.
Androidでログを吐くためのクラス。クラス名とメソッド名はStackTraceから取ってきてるので、Log.d(LOG_TAG, "関数名, 文字列"); って書かなくても Logger.d("文字列"); でいける
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;
}
}
@esperia
Copy link
Author

esperia commented Feb 22, 2012

あれ?ライセンス書くところなかったっけな。。

ライセンスはpublic licenseです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment