Anroid 工具类 —— 日志
【摘要】
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java....
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;
//这个类可以让maven根据dev或prod自动生成,里面只有一个isDebugEnabled变量
import com.newland.mtype.common.RunningModel;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
/**
* 日志工厂<p>
*/
public class DeviceLoggerFactory {
enum Level {
Debug,
Info,
Warn,
Error
}
public static final byte NONE = 0x00;
public static final byte CONSOLE = 0x01;
public static final byte BROADCAST = 0x02;
public static final byte FILE = 0x04;
public static final byte ALL = (byte) 0xFF;
public static final String ACTION_LOG = "LOG";
public static final String EXTRA_LEVEL = "LEVEL";
public static final String EXTRA_TAG = "TAG";
public static final String EXTRA_MSG = "MSG";
private static final SimpleDateFormat SDF_DATE = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault());
private static final SimpleDateFormat SDF_TIME = new SimpleDateFormat("hh:mm:ss.SSS", Locale.getDefault());
private static Context context;
private static byte logType = CONSOLE;
private static File file = null;
public static void init(Context context, byte logType) {
DeviceLoggerFactory.context = context;
DeviceLoggerFactory.logType = logType;
}
private static void initFile() {
//这里的path无论有没有申明权限都能拿到,通常值为 "/storage/emulated/0"
if(file != null && file.exists()) return;
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String logDir = sdPath + "/newland/log/";
String fileName = SDF_DATE.format(new Date()) + ".txt";
File dir = new File(logDir);
//当路径不存在时,必须调用mkdirs主动创建,BufferedWriter和OutputStreamWriter都不会帮我们创建目录
if(!dir.exists()) {
dir.mkdirs();
}
file = new File(logDir + fileName);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
file = null;
}
}
}
public static DeviceLogger getLogger(final String tag) {
return new DeviceLogger() {
private void recordLog(Level level, String msg) {
//debug的日志只有在允许打印时,才会打印。其它日志是无论什么情况都打印
if(logType == NONE) return;
if(RunningModel.isDebugEnabled && level == Level.Debug || level != Level.Debug) {
if((logType & BROADCAST) != 0 && context != null) {
Intent intent = new Intent(ACTION_LOG);
//由于extra只能存放一些基本变量,因此需要将枚举类转为string
intent.putExtra(EXTRA_LEVEL, level.toString());
intent.putExtra(EXTRA_TAG, tag);
intent.putExtra(EXTRA_MSG, msg);
context.sendBroadcast(intent);
}
if((logType & FILE) != 0) {
initFile();
if(file != null) {
try {
//FileOutputStream的第二个参数表示是否append(如果文件存在,是追加还是清空重写)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
bw.write(level + " " + SDF_TIME.format(new Date()) + " " + tag + " " + msg + "\r\n");
//close方法自带flush,所以前面就不用多此一举的去flush了
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if((logType & CONSOLE) != 0) {
switch(level) {
case Debug:
Log.d(tag, msg);
break;
case Info:
Log.i(tag, msg);
break;
case Warn:
Log.w(tag, msg);
break;
case Error:
Log.e(tag, msg);
break;
}
}
}
}
private void recordLog(Level level, String msg, Throwable e) {
recordLog(level, msg + "\r\n\t" + e.getMessage());
}
@Override
public void debug(String msg) {
recordLog(Level.Debug, msg);
}
@Override
public void debug(String msg, Throwable e) {
recordLog(Level.Debug, msg, e);
}
@Override
public void info(String msg) {
recordLog(Level.Info, msg);
}
@Override
public void info(String msg, Throwable e) {
recordLog(Level.Info, msg, e);
}
@Override
public void warn(String msg) {
recordLog(Level.Warn, msg);
}
@Override
public void warn(String msg, Throwable e) {
recordLog(Level.Warn, msg, e);
}
@Override
public void error(String msg) {
recordLog(Level.Error, msg);
}
@Override
public void error(String msg, Throwable e) {
recordLog(Level.Error, msg, e);
}
};
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/86704631
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)