Skip to content

Instantly share code, notes, and snippets.

@jackuhan
Created May 11, 2016 02:43
Show Gist options
  • Save jackuhan/946c68486bd07b362beac4f82441f582 to your computer and use it in GitHub Desktop.
Save jackuhan/946c68486bd07b362beac4f82441f582 to your computer and use it in GitHub Desktop.
public class LogUtils {
public static void v(String tag,String mess) {
if (true) {
Log.v(tag+getTag(),getTags()+" "+ buildMessage(mess));
}
}
public static void d(String tag,String mess) {
if (true) {
Log.d(tag+getTag(),getTags()+" "+ buildMessage(mess));
}
}
public static void i(String tag,String mess) {
if (true) {
Log.i(tag+getTag(),getTags()+" "+ buildMessage(mess));
}
}
public static void w(String tag,String mess) {
if (true) {
Log.w(tag+getTag(),getTags()+" "+ buildMessage(mess));
}
}
public static void e(String tag,String mess) {
if (true) {
Log.e(tag+getTag(),getTags()+" "+ buildMessage(mess));
}
}
public static void v(String mess) {
if (true) {
v("",mess);
}
}
public static void d(String mess) {
if (true) {
d("",mess);
}
}
public static void i(String mess) {
if (true) {
i("",mess);
}
}
public static void w(String mess) {
if (true) {
w("",mess);
}
}
public static void e(String mess) {
if (true) {
e("",mess);
}
}
//获取线程信息 方法 msg
private static String buildMessage(String msg) {
StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
String caller = "";
for (int i = 2; i < trace.length; i++) {
Class<?> clazz = trace[i].getClass();
if (!clazz.equals(LogUtils.class)) {
caller = trace[i].getMethodName();
break;
}
}
if (null == msg) {
msg = "";
}
return msg;
//return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread().getId(), caller, msg);
}
/**
* 获取日志的标签 格式:类名_方法名_行号 (需要权限:android.permission.GET_TASKS)
*
* @return tag
* @see [类、类#方法、类#成员]
*/
/**
* 异常栈位移
*/
private static final int EXCEPTION_STACK_INDEX = 2;
private static String getTags()
{
try
{
Exception exception = new LogException();
if (exception.getStackTrace() == null || exception.getStackTrace().length <= EXCEPTION_STACK_INDEX)
{
return "***";
}
StackTraceElement element = exception.getStackTrace()[EXCEPTION_STACK_INDEX];
String className = element.getClassName();
int index = className.lastIndexOf(".");
if (index > 0)
{
className = className.substring(index + 1);
}
return className + "_" + element.getMethodName() + "_" + element.getLineNumber();
}
catch (Throwable e)
{
e.printStackTrace();
return "***";
}
}
//获取类名
private static String getTag() {
StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
String callingClass = "";
for (int i = 2; i < trace.length; i++) {
Class<?> clazz = trace[i].getClass();
if (!clazz.equals(LogUtils.class)) {
callingClass = trace[i].getClassName();
callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
break;
}
}
return "";
//return callingClass;
}
/**
* 取日志标签用的的异常类,只是用于取得日志标签
*/
private static class LogException extends Exception
{
/**
* 注释内容
*/
private static final long serialVersionUID = 1L;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment