默认的打印日志有长度限制,写一个简单的工具类,解决日志太长打印不全的小问题:
public class LogUtils {
private static final int MAX_LOG_LENGTH =3500 ;
private static boolean isDeBug = BuildConfig.DEBUG;
public final static StringTAG ="yourTag";
public static void showLog(String tag, String msg) {
if (isDeBug) {
if (msg !=null){
if (msg.length() >MAX_LOG_LENGTH) {
for (int i =0; i < msg.length(); i +=MAX_LOG_LENGTH) {
//当前截取的长度<总长度则继续截取最大的长度来打印
if (i +MAX_LOG_LENGTH < msg.length()) {
Log.i(tag + i, msg.substring(i, i +MAX_LOG_LENGTH));
}else {
//当前截取的长度已经超过了总长度,则打印出剩下的全部信息
Log.i(tag + i, msg.substring(i, msg.length()));
}
}
}else {
//直接打印
Log.i(tag, msg);
}
}
}
}
public static void showLog(String msg) {
if (isDeBug) {
if (msg !=null){
if (msg.length() >MAX_LOG_LENGTH) {
for (int i =0; i < msg.length(); i +=MAX_LOG_LENGTH) {
//当前截取的长度<总长度则继续截取最大的长度来打印
if (i +MAX_LOG_LENGTH < msg.length()) {
Log.i(TAG + i, msg.substring(i, i +MAX_LOG_LENGTH));
}else {
//当前截取的长度已经超过了总长度,则打印出剩下的全部信息
Log.i(TAG + i, msg.substring(i, msg.length()));
}
}
}else {
//直接打印
Log.i(TAG, msg);
}
}
}
}
}
或者用这个:
public class LogUtils {
private static BooleanLOG_SWITCH =true; // 日志文件总开关
private static BooleanLOG_TO_FILE =false; // 日志写入文件开关
private static StringLOG_TAG ="###stone"; // 默认的tag
private static char LOG_TYPE ='v';// 输入日志类型,v代表输出所有信息,w则只输出警告...
private static int LOG_SAVE_DAYS =30;// sd卡中日志文件的最多保存天数
private final static SimpleDateFormatLOG_FORMAT =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日志的输出格式
private final static SimpleDateFormatFILE_SUFFIX =new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
private static StringLOG_FILE_PATH; // 日志文件保存路径
private static StringLOG_FILE_NAME;// 日志文件保存名称
public static void init(Context context) {// 在Application中初始化
LOG_FILE_PATH = FileUtils.createRootPath(context);
LOG_FILE_NAME ="StoneLogFile";
}
/**************************** Warn *********************************/
public static void w(Object msg) {
w(LOG_TAG, msg);
}
public static void w(String tag, Object msg) {
w(tag, msg, null);
}
public static void w(String tag, Object msg, Throwable tr) {
log(tag, msg.toString(), tr, 'w');
}
/*************************** Error ********************************/
public static void e(Object msg) {
e(LOG_TAG, msg);
}
public static void e(String tag, Object msg) {
e(tag, msg, null);
}
public static void e(String tag, Object msg, Throwable tr) {
log(tag, msg.toString(), tr, 'e');
}
/*************************** Debug ********************************/
public static void d(Object msg) {
d(LOG_TAG, msg);
}
public static void d(String tag, Object msg) {// 调试信息
d(tag, msg, null);
}
public static void d(String tag, Object msg, Throwable tr) {
log(tag, msg.toString(), tr, 'd');
}
/**************************** Info *********************************/
public static void i(Object msg) {
i(LOG_TAG, msg);
}
public static void i(String tag, Object msg) {
i(tag, msg, null);
}
public static void i(String tag, Object msg, Throwable tr) {
log(tag, msg.toString(), tr, 'i');
}
/************************** Verbose ********************************/
public static void v(Object msg) {
v(LOG_TAG, msg);
}
public static void v(String tag, Object msg) {
v(tag, msg, null);
}
public static void v(String tag, Object msg, Throwable tr) {
log(tag, msg.toString(), tr, 'v');
}
/**
* 根据tag, msg和等级,输出日志
*
* @param tag
* @param msg
* @param level
*/
private static void log(String tag, String msg, Throwable tr, char level) {
if(msg.length() >3500){
String show = msg.substring(0, 3500);
if (LOG_SWITCH) {
if ('e' == level && ('e' ==LOG_TYPE ||'v' ==LOG_TYPE)) {// 输出错误信息
Log.e(tag, show, tr);
}else if ('w' == level && ('w' ==LOG_TYPE ||'v' ==LOG_TYPE)) {
Log.w(tag, show, tr);
}else if ('d' == level && ('d' ==LOG_TYPE ||'v' ==LOG_TYPE)) {
Log.d(tag, show, tr);
}else if ('i' == level && ('d' ==LOG_TYPE ||'v' ==LOG_TYPE)) {
Log.i(tag, show, tr);
}else {
Log.v(tag, show, tr);
}
if (LOG_TO_FILE)
log2File(String.valueOf(level), tag, show + tr ==null ?"" :"\n" + Log.getStackTraceString(tr));
}
if((msg.length() -3500)>3500){//剩下的文本还是大于规定长度
String partLog = msg.substring(3500,msg.length());
log(tag,partLog, tr, level);
}else{
String surplusLog = msg.substring(3500, msg.length());
log(tag,surplusLog, tr, level);
}
}else{
if (LOG_SWITCH) {
if ('e' == level && ('e' ==LOG_TYPE ||'v' ==LOG_TYPE)) {// 输出错误信息
Log.e(tag, msg, tr);
}else if ('w' == level && ('w' ==LOG_TYPE ||'v' ==LOG_TYPE)) {
Log.w(tag, msg, tr);
}else if ('d' == level && ('d' ==LOG_TYPE ||'v' ==LOG_TYPE)) {
Log.d(tag, msg, tr);
}else if ('i' == level && ('d' ==LOG_TYPE ||'v' ==LOG_TYPE)) {
Log.i(tag, msg, tr);
}else {
Log.v(tag, msg, tr);
}
if (LOG_TO_FILE)
log2File(String.valueOf(level), tag, msg + tr ==null ?"" :"\n" + Log.getStackTraceString(tr));
}
}
}
/**
* 打开日志文件并写入日志
*
* @return
**/
private synchronized static void log2File(String mylogtype, String tag, String text) {
Date nowtime =new Date();
String date =FILE_SUFFIX.format(nowtime);
String dateLogContent =LOG_FORMAT.format(nowtime) +":" + mylogtype +":" + tag +":" + text; // 日志输出格式
File destDir =new File(LOG_FILE_PATH);
if (!destDir.exists()) {
destDir.mkdirs();
}
File file =new File(LOG_FILE_PATH, LOG_FILE_NAME + date);
try {
FileWriter filerWriter =new FileWriter(file, true);
BufferedWriter bufWriter =new BufferedWriter(filerWriter);
bufWriter.write(dateLogContent);
bufWriter.newLine();
bufWriter.close();
filerWriter.close();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除指定的日志文件
*/
public static void delFile() {// 删除日志文件
String needDelFiel =FILE_SUFFIX.format(getDateBefore());
File file =new File(LOG_FILE_PATH, needDelFiel +LOG_FILE_NAME);
if (file.exists()) {
file.delete();
}
}
/**
* 得到LOG_SAVE_DAYS天前的日期
*
* @return
*/
private static DategetDateBefore() {
Date nowtime =new Date();
Calendar now = Calendar.getInstance();
now.setTime(nowtime);
now.set(Calendar.DATE, now.get(Calendar.DATE) -LOG_SAVE_DAYS);
return now.getTime();
}
}
网友评论