按照日志级别从高到低为ERROR, WARN, INFO, DEBUG, VERBOSE
- 1、Log.v 的输出颜色为黑色的,输出大于或等于VERBOSE日志级别的信息
- 2、Log.d的输出颜色是蓝色的,输出大于或等于DEBUG日志级别的信息
- 3、Log.i的输出为绿色,输出大于或等于INFO日志级别的信息
- 4、Log.w的输出为橙色, 输出大于或等于WARN日志级别的信息
- 5、Log.e的输出为红色,仅输出ERROR日志级别的信息
另外
- System.err的级别和 W 相同,Tag为默认的System.err
- System.out的级别和 I 相同,Tag为默认的System.out
演示
打印的日志
产生的文件内容
public class LogUtils {
public final static boolean DEBUG = BuildConfig.DEBUG;
public static void v(String message, Object... args) {
if (DEBUG) log(Log.VERBOSE, formatMessage(message, args));
}
public static void d(String message, Object... args) {
if (DEBUG) log(Log.DEBUG, formatMessage(message, args));
}
public static void i(String message, Object... args) {
if (DEBUG) log(Log.INFO, formatMessage(message, args));
}
public static void w(String message, Object... args) {
log(Log.WARN, formatMessage(message, args));
}
public static void e(String message, Object... args) {
log(Log.ERROR, formatMessage(message, args));
}
/**
* 根据type输出日志消息,包括方法名,方法行数,Message
*
* @param type 日志类型,如Log.INFO
* @param message 日志内容
*/
private static void log(int type, String message) {
StackTraceElement stackTrace = Thread.currentThread().getStackTrace()[4];
String className = stackTrace.getClassName();
String tag = className.substring(className.lastIndexOf('.') + 1);
StringBuilder sb = new StringBuilder();
sb.append("包青天")
.append("(")
.append(stackTrace.getFileName())//文件名
.append(":")
.append(stackTrace.getLineNumber())//行号
.append(")")
.append("#")
.append(stackTrace.getMethodName())//方法名
.append("【")
.append(message)//消息
.append("】");
switch (type) {
case Log.DEBUG:
Log.d(tag, sb.toString());
break;
case Log.INFO:
Log.i(tag, sb.toString());
break;
case Log.WARN:
Log.w(tag, sb.toString());
break;
case Log.ERROR:
Log.e(tag, sb.toString());
break;
case Log.VERBOSE:
Log.v(tag, sb.toString());
break;
}
}
private static String formatMessage(String message, Object... args) {
if (TextUtils.isEmpty(message)) return "";
if (args != null && args.length > 0) {
try {
return String.format(message, args);
} catch (Exception e) {
e.printStackTrace();
}
}
return message;
}
}
public class MyLog {
private static Boolean MYLOG_SWITCH = true; // 是否打印日志及是否写入到文件的总开关
private static Boolean MYLOG_WRITE_TO_FILE = true;// 是否将日志写入到文件的开关
private static char MYLOG_TYPE = 'v';// 输入日志类型,仅输出比自己级别高的日志,日志级别ERROR, WARN, INFO, DEBUG, VERBOSE
private static String MYLOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory().getPath() + "/bqt";// 日志文件的路径
private static String MYLOGFILEName = "Log-";// 日志文件名称①
private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件名称②
//**************************************************************************************************************************
public static void w(String tag, String text) {
log(tag, text, 'w');
}
public static void e(String tag, String text) {
log(tag, text, 'e');
}
public static void d(String tag, String text) {
log(tag, text, 'd');
}
public static void i(String tag, String text) {
log(tag, text, 'i');
}
public static void v(String tag, String text) {
log(tag, text, 'v');
}
//**************************************************************************************************************************
//根据tag, msg和等级输出日志
private static void log(String tag, String msg, char level) {
if (MYLOG_SWITCH) {
if ('e' == level) Log.e(tag, msg);
else if ('w' == level && ('w' == MYLOG_TYPE || 'i' == MYLOG_TYPE || 'd' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) Log.w(tag, msg);
else if ('i' == level && ('i' == MYLOG_TYPE || 'd' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) Log.i(tag, msg);
else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) Log.d(tag, msg);
else if ('v' == level && 'v' == MYLOG_TYPE) Log.v(tag, msg);
if (MYLOG_WRITE_TO_FILE) writeLogtoFile(String.valueOf(level), tag, msg);
}
}
//将日志写入文件,文件名格式为MYLOGFILEName + logfile.format(nowtime) + ".txt",如【Log-2015-12-25.txt】
private static void writeLogtoFile(String mylogtype, String tag, String text) {
Date nowtime = new Date();
SimpleDateFormat myLogSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String myLogs = myLogSdf.format(nowtime) + " " + mylogtype + " " + tag + " " + text;
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) return;//判断sdcard是否插入
File fileDir = new File(MYLOG_PATH_SDCARD_DIR);
if (!fileDir.exists()) fileDir.mkdirs();//创建文件夹,这一步不能少
File file = new File(fileDir, MYLOGFILEName + logfile.format(nowtime) + ".txt");
try {
FileWriter filerWriter = new FileWriter(file, true);//true代表在文件中原来的数据后面添加,不进行覆盖
BufferedWriter bufWriter = new BufferedWriter(filerWriter);
bufWriter.write(myLogs);
bufWriter.newLine();
bufWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}