Skip to content

Instantly share code, notes, and snippets.

@deskid
Created September 18, 2015 09:38
Show Gist options
  • Save deskid/689b50eb715474781b23 to your computer and use it in GitHub Desktop.
Save deskid/689b50eb715474781b23 to your computer and use it in GitHub Desktop.
hello world at MainActivity.java.onCreate (MainActivity.java:37) 在Android studio中这种格式的log可以类似超链接响应点击跳到对应文件行数
package com.example.zhou.helloword.log;
import android.util.Log;
import com.example.zhou.helloword.BuildConfig;
public class Logger {
private Logger() {
}
public static boolean isDebug() {
return BuildConfig.DEBUG;
}
private static String logWithMethodAndLine(String log, StackTraceElement sElements) {
String className = sElements.getFileName();
String methodName = sElements.getMethodName();
int lineNumber = sElements.getLineNumber();
String fileName = sElements.getFileName();
StringBuffer buffer = new StringBuffer(log);
buffer.append(" at ")
.append(className)
.append(".")
.append(methodName)
.append(" (")
.append(fileName)
.append(":")
.append(lineNumber)
.append(") ");
return buffer.toString();
}
public static void e(String tag, String message) {
if (isDebug()) {
Log.e(tag, logWithMethodAndLine(message, new Throwable().getStackTrace()[1]));
}
}
public static void i(String tag, String message) {
if (isDebug()) {
Log.i(tag, logWithMethodAndLine(message, new Throwable().getStackTrace()[1]));
}
}
public static void d(String tag, String message) {
if (isDebug()) {
Log.d(tag, logWithMethodAndLine(message, new Throwable().getStackTrace()[1]));
}
}
public static void v(String tag, String message) {
if (isDebug()) {
Log.v(tag, logWithMethodAndLine(message, new Throwable().getStackTrace()[1]));
}
}
public static void w(String tag, String message) {
if (isDebug()) {
Log.w(tag, logWithMethodAndLine(message, new Throwable().getStackTrace()[1]));
}
}
public static void wtf(String tag, String message) {
if (isDebug()) {
Log.wtf(tag, logWithMethodAndLine(message, new Throwable().getStackTrace()[1]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment