package com.example.yourapp.util;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* 将Log日志写入文件中
* <p>
* 使用单例模式是因为要初始化文件存放位置
* <p>
* Created by daniel on 2021/6/14.
*/
public class LogToFile {
private static String TAG = "YourApp";
private static FileOutputStream fos = null;//FileOutputStream会自动调用底层的close()方法,不用关闭
private static BufferedWriter bw = null;
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);//日期格式
/**
* 初始化,须在使用之前设置,最好在Application创建时调用
*
* @param logPath log日志存放路径, null to use /sdcard/
*/
public static void init(String logPath) {
// 每init一次,关闭以前的log文件
close();
//
if (logPath == null)
logPath = "/sdcard";
//如果父路径不存在
File file = new File(logPath);
if (!file.exists()) {
file.mkdirs();//创建父路径
}
Date date = new Date();//因为log日志是使用日期命名的,使用静态成员变量主要是为了在整个程序运行期间只存在一个.log文件中
String fileName = logPath + File.separator + dateFormat.format(date) + ".log";//log日志名,使用时间命名,保证不重复
try {
fos = new FileOutputStream(fileName);
bw = new BufferedWriter(new OutputStreamWriter(fos));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*
catch (IOException e) {
e.printStackTrace();
}
*/
}
/**
* 获得文件存储路径
*
* @return
*/
public static String getFilePath(Context context) {
//不用Environment.getExternalStorageDirectory(),SD根目录:/mnt/sdcard/(6.0后写入需要用户授权)
if (Environment.MEDIA_MOUNTED.equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) {//如果外部储存可用
return context.getExternalFilesDir(null).getPath();//获得外部存储路径,默认路径为/storage/emulated/0/Android/data/<package name>/files/2016-03-14_16-15-09.log
//return context.getExternalCacheDir().getAbsolutePath();
} else {
return context.getFilesDir().getPath();//直接存到/data/data/<package name>/files里,非root手机是看不到的
//return context.getCacheDir().getAbsolutePath();
}
}
private static final int VERBOSE = 'v';
private static final int DEBUG = 'd';
private static final int INFO = 'i';
private static final int WARN = 'w';
private static final int ERROR = 'e';
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
*/
public static void writeToFile(int type, String tag, String msg) {
if (null == fos || null == bw) {
Log.e(TAG, "未初始化LogToFile");
return;
}
Date date = new Date();
String log = dateFormat.format(date) + " " + type + " " + tag + " " + msg + "\n";//log日志内容,可以自行定制
try {
bw.write(log);
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将字节数组写入文件中
*
* @param dataBuffer
*/
public static void writeToFile(byte[] dataBuffer, int offset, int length) {
if (null == fos || null == bw) {
Log.e(TAG, "未初始化LogToFile");
return;
}
try {
fos.write(dataBuffer, offset, length);
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (fos != null) {
fos.close();
fos = null;
}
if (bw != null) {
bw.close();//关闭缓冲流
bw = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这个log类可以吐log到存储。写完了,但功能还需要进一步增强。