安卓(Android) LogUtils Log 日志工具类

在实际情况中,用户的各种神操作有可能把码农辛辛苦苦写的代码搞崩溃掉,那么如果有用户的操作日志,一切问题将迎刃而解,日志对于码农来说简直是定位bug的一件利器.在开发过程中使用Android的Log类打印日志,并不能将保存日志下来,所以特意写了这个日志工具类,用来保存用户的操作日志,以便代码发生问题时,能够及时的定位问题.

/**

* Log 工具类

*

* @author CharlesRich

* @email [email protected]

* @mobile 18602438878

* @create 2020-1-29 18:19

*/

public class LogUtils {

/** 可用用于 release 时,统一关闭写入 Log 到文件*/

public static boolean isWriteLogToFile = SPUtils.getWriteLogFile(BaseApplication.getInstance());

private static String fileName;

private static String className;//类名

private static String methodName;//方法名

private static int lineNumber;//行数

private static long threadId;

private static String threadName;

/**

* 判断是否可以调试

* @return

*/

public static boolean isDebuggable() {

try {

ApplicationInfo info = BaseApplication.getInstance().getApplicationInfo();

return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;

} catch (Exception e) {

return false;

}

}

/**

* 获取文件名、方法名、所在行数

* @param sElements

*/

private static void getMethodNames(StackTraceElement[] sElements){

fileName = sElements[1].getFileName();

className = sElements[1].getFileName();

methodName = sElements[1].getMethodName();

lineNumber = sElements[1].getLineNumber();

threadId = Thread.currentThread().getId();

threadName = Thread.currentThread().getName();

}

/**

* 打印 VERBOSE 信息

*

* @param tag

* @param message

*/

public static void v(String tag, String message) {

if (!isDebuggable()) {

return;

}

getMethodNames(new Throwable().getStackTrace());

Log.v(tag, createLog(message));

if (isWriteLogToFile) {

LogToFileUtils.i(className, createLog(message));

}

}

/**

* 打印 INFO 信息

*

* @param tag

* @param message

*/

public static void i(String tag, String message) {

if (!isDebuggable()) {

return;

}

getMethodNames(new Throwable().getStackTrace());

Log.i(tag, createLog(message));

if (isWriteLogToFile) {

LogToFileUtils.i(className, createLog(message));

}

}

/**

* 打印 DEBUG 信息

*

* @param tag

* @param message

*/

public static void d(String tag, String message) {

if (!isDebuggable()) {

return;

}

getMethodNames(new Throwable().getStackTrace());

Log.d(tag, createLog(message));

if (isWriteLogToFile) {

LogToFileUtils.i(className, createLog(message));

}

}

/**

* 打印 WARN 信息

*

* @param tag

* @param message

*/

public static void w(String tag, String message) {

if (!isDebuggable()) {

return;

}

getMethodNames(new Throwable().getStackTrace());

Log.w(tag, createLog(message));

if (isWriteLogToFile) {

LogToFileUtils.i(className, createLog(message));

}

}

/**

* 打印 ERROR 信息

*

* @param tag

* @param message

*/

public static void e(String tag, String message) {

if (!isDebuggable()) {

return;

}

getMethodNames(new Throwable().getStackTrace());

Log.e(tag, createLog(message));

if (isWriteLogToFile) {

LogToFileUtils.i(className, createLog(message));

}

}

/**

* 输出 Log 中包含的信息

*

* @param message

* @return Log 中包含的信息

*/

public static String createLog(String message) {

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("[ ");

stringBuilder.append("threadID=" + threadId).append(", ");

stringBuilder.append("threadName=" + threadName).append(", ");

stringBuilder.append("fileName=" + fileName).append(", ");

stringBuilder.append("className=" + className).append(", ");

stringBuilder.append("methodName=" + methodName).append(", ");

stringBuilder.append("lineNumber=" + lineNumber);

stringBuilder.append(" ] ");

stringBuilder.append(":" + message);

return stringBuilder.toString();

}

}

/**

* 将Log日志写入文件中

*

* @author CharlesRich

* @email [email protected]

* @mobile 18602438878

* @create 2019-11-09 16:57

*/

public class LogToFileUtils {

private static String logPath = null;//log日志存放路径

private static SimpleDateFormat dateFormatFile = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.US);

private static Date date = new Date();

public static String getFilePath() {

String file_dir = "";

/** SD卡是否存在 */

boolean isSDCardExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());

boolean isRootDirExist = Environment.getExternalStorageDirectory().exists();

if (isSDCardExist && isRootDirExist) {

file_dir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "LOG_FILE" + File.separator + "log" + File.separator;

} else {

file_dir = BaseApplication.getInstance().getFilesDir().getAbsolutePath() + File.separator + "log" + File.separator;

}

return file_dir;

}

private static final String VERBOSE = "VERBOSE";

private static final String DEBUG = "DEBUG";

private static final String INFO = "INFO";

private static final String WARN = "WARN";

private static final String ERROR = "ERROR";

public static void v(String tag, String msg) {

writeToFile(VERBOSE, tag, msg);

}

public static void d(String tag, String msg) {

writeToFile(DEBUG, tag, msg);

}

public static void i(String tag, String msg) {

writeToFile(INFO, tag, msg);

}

public static void w(String tag, String msg) {

writeToFile(WARN, tag, msg);

}

public static void e(String tag, String msg) {

writeToFile(ERROR, tag, msg);

}

/**

* 将log信息写入文件中

*

* @param type

* @param tag

* @param msg

*/

private static void writeToFile(String type, String tag, String msg) {

if (null == logPath) {

logPath = getFilePath();

}

String fileName = logPath + "/log_" + dateFormatFile.format(new Date()) + ".log";

StringBuilder sb = new StringBuilder();

File file = new File(logPath);

/** 如果父路径不存在 */

if (!file.exists()) {

file.mkdirs();//创建父路径

sb.append("手机厂商:" + SystemUtils.getDeviceBrand() + "\\n");

sb.append("手机型号:" + SystemUtils.getSystemModel() + "\\n");

sb.append("手机版本:" + SystemUtils.getSystemVersion() + "\\n");

}

sb.append(type + " " + dateFormat.format(date) + " ");

sb.append(tag + ": ");

sb.append(msg + "\\n");

FileOutputStream fos = null;

BufferedWriter bw = null;

try {

fos = new FileOutputStream(fileName, true);

bw = new BufferedWriter(new OutputStreamWriter(fos));

bw.write(sb.toString());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bw != null) {

bw.close();//关闭缓冲流

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* Application基类

*

* @author CharlesRich

* @email [email protected]

* @mobile 18602438878

* @create 2020-1-19 12:19

*/

public class BaseApplication extends Application {

private static final String TAG = BaseApplication.class.getSimpleName();

private static BaseApplication mInstance;

private static Handler mHandler = new Handler();

@Override

public void onCreate(){

super.onCreate();

mInstance = this;

}

/**

* 获取Instance

*

*/

public static BaseApplication getInstance(){

return mInstance;

}

/**

* 获取Application Context

*

*/

public static Context getAppContext(){

return mInstance != null ? mInstance.getApplicationContext():null;

}

/**

* 在主线程上执行Runnable

*

*/

public static void runOnMainThread(Runnable runnable){

mHandler.post(runnable);

}

}


分享到:


相關文章: