<span style="font-size:18px;">/***
*----------- 版本说明---------------
* 1.0.0实现的功能:
* a、根据配置文件,控制了是否需要打印各种调试日志的信息;
* b、该工具的使用是全局单例模式实现的,具体的使用步骤如下:
* (1)首先在资源文件中自定义一个资源变量is_print_log,格式为: <bool name="is_print_log">true</bool>;
* (2)其次,在第一次使用的时候实例化,Logger.getInstance(Context);
* (3)最后,可以直接调用对应的方法即可,如Logger.i("","");
* 2.0.0实现的功能:
* a、添加了将日志保存在本地的功能;
* 3.0.0预实现的功能:删除本地的日志以及本地的日志回滚;
*/
package com.keyisoftware.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
/**
* 自定义的日志输出,建议当程序发布的时需要在configs.xml文件中将is_print_log设置为false;
*
* @author keyisoftware@163.com
* @version 2.0.0
*/
public class KyLog {
public enum LogType {
debug, error, info, verbose, warn
}
private static final String TAG = "KyLog_";
private static KyLog log;
/** 是否打印调试日志 */
private static boolean isPrintDebugLog = false;
/** 是否写日志到本地 */
private static boolean isWriteToSdcard = false;
/** 项目文件夹 */
private static String projectDirectory;
private static String DebugDir = "";
private static String ErrorDir = "";
private static String VerboseDir = "";
private static String WarnDir = "";
private static String InfoDir = "";
/**
* 单例模式
*
* @param context
* 上下文
*/
private KyLog(Context context) {
try {
// TODO:此种方法不好之处在于必须关联到R文件了。
isPrintDebugLog = context.getResources().getBoolean(R.bool.is_print_log);
isWriteToSdcard = context.getResources().getBoolean(R.bool.is_write_to_sdcard);
projectDirectory = context.getResources().getString(R.string.project_directory);
// 创建所有的文件夹
/** 构建目录路径 **/
if (projectDirectory == null || projectDirectory == "") {
projectDirectory = "KyUtils";
}
String LogDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + projectDirectory + "/Log/";
DebugDir = LogDirectory + "/Debug/";
ErrorDir = LogDirectory + "/Error/";
VerboseDir = LogDirectory + "/Verbose/";
WarnDir = LogDirectory + "/Warn/";
InfoDir = LogDirectory + "/Info/";
/** 创建文件目录 **/
File dir = new File(DebugDir);
if (!dir.exists()) {
dir.mkdirs();
}
dir = new File(ErrorDir);
if (!dir.exists()) {
dir.mkdirs();
}
dir = new File(VerboseDir);
if (!dir.exists()) {
dir.mkdirs();
}
dir = new File(WarnDir);
if (!dir.exists()) {
dir.mkdirs();
}
dir = new File(InfoDir);
if (!dir.exists()) {
dir.mkdirs();
}
} catch (Exception e) {
Log.e(TAG, "Can't find is_print_log in configs.xml file!");
}
}
/**
* 初始化单例模式
*
* @param context
* 上下文
* @return 日志的单例模式
*/
public static KyLog getInstance(Context context) {
if (log == null) {
log = new KyLog(context);
}
return log;
}
public static void d(String tag, String msg) {
if (isPrintDebugLog) {
Log.d(TAG + tag, msg);
}
if (isWriteToSdcard) {
writeLogToSdCard(LogType.debug, tag, msg);
}
}
public static void e(String tag, String msg) {
if (isPrintDebugLog)
Log.e(TAG + tag, msg);
if (isWriteToSdcard) {
writeLogToSdCard(LogType.error, tag, msg);
}
}
public static void i(String tag, String msg) {
if (isPrintDebugLog)
Log.i(TAG + tag, msg);
if (isWriteToSdcard) {
writeLogToSdCard(LogType.info, tag, msg);
}
}
public static void v(String tag, String msg) {
if (isPrintDebugLog)
Log.v(TAG + tag, msg);
if (isWriteToSdcard) {
writeLogToSdCard(LogType.verbose, tag, msg);
}
}
public static void w(String tag, String msg) {
if (isPrintDebugLog)
Log.w(TAG + tag, msg);
if (isWriteToSdcard) {
writeLogToSdCard(LogType.warn, tag, msg);
}
}
public static KyLog getLog() {
return log;
}
public static void setLog(KyLog log) {
KyLog.log = log;
}
/***
* 设置日志文件的名称,日志文件的名称按照日期来设置,如2015-12-29
*
* @return 日志文件的名称
*/
private static String setLogFileName() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(new Date()) + ".txt";
}
/***
* 写日志到
*
* @param logType
* @param tag
* @param msg
*/
private static void writeLogToSdCard(LogType logType, String tag, String msg) {
String dir = "";
if (logType.equals(LogType.debug)) {
dir = DebugDir + setLogFileName();
} else if (logType.equals(LogType.error)) {
dir = ErrorDir + setLogFileName();
} else if (logType.equals(LogType.info)) {
dir = InfoDir + setLogFileName();
} else if (logType.equals(LogType.verbose)) {
dir = VerboseDir + setLogFileName();
} else if (logType.equals(LogType.warn)) {
dir = WarnDir + setLogFileName();
}
File file = new File(dir);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
writeLogMsg(file, tag, msg);
}
/**
* 按照固定格式设置日志内容
*
* @param tag
* @param msg
* @return
*/
private static String setLogContent(String tag, String msg) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String time = dateFormat.format(new Date());
return time + " " + tag + " " + msg + "\n";
}
/***
* 写日志信息
*
* @param file
* @param tag
* @param msg
*/
private static void writeLogMsg(File file, String tag, String msg) {
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter(file, true);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(setLogContent(tag, msg));
bufferedWriter.flush();
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
try {
bufferedWriter.close();
fileWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
/***
* 清理日志文件夹
*/
public static void clearLogDir() {
// TODO:待完成
}
}
</span>
自定义Android日志输出工具类
最新推荐文章于 2021-05-25 21:13:47 发布