Skip to content

Commit 68714ba

Browse files
committed
完善功能
1 parent 6972046 commit 68714ba

File tree

7 files changed

+697
-15
lines changed

7 files changed

+697
-15
lines changed
Lines changed: 126 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,156 @@
11
package com.yhw.loglib;
22

3+
import android.content.Context;
4+
import android.text.TextUtils;
35
import android.util.Log;
46

57
import com.yhw.loglib.config.LogConfig;
8+
import com.yhw.loglib.file.LogFile;
9+
import com.yhw.loglib.http.UploadLog;
10+
import com.yhw.loglib.utils.DateUtil;
11+
import com.yhw.loglib.utils.LogUtil;
612

713
/**
814
* 日志打印类
915
* Author: yhw on 2017-11-09.
1016
*/
1117

1218
public class Logger {
19+
private static final String TAG = Logger.class.getSimpleName();
20+
private static LogConfig mLogConfig;
21+
private static final String INFO = "INFO";
22+
private static final String VERBOSE = "VERBOSE";
23+
private static final String DEBUG = "DEBUG";
24+
private static final String WARN = "WARN";
25+
private static final String ERROR = "ERROR";
1326

14-
public static void i(String tag, String msg){
15-
if(LogConfig.isLog){
16-
Log.i(tag,msg);
27+
/**
28+
* 设置配置文件
29+
*/
30+
public static void init(LogConfig logConfig){
31+
if(logConfig==null){
32+
return;
33+
}
34+
mLogConfig = logConfig;
35+
if(logConfig.isSaveFile()){
36+
LogFile.getInstance(mLogConfig).deleteLogFile();
1737
}
1838
}
1939

40+
public static void i(String msg){
41+
print(INFO,msg);
42+
}
43+
44+
public static void i(String tag, String msg){
45+
print(INFO,tag,msg);
46+
}
47+
48+
public static void d(String msg){
49+
print(DEBUG,msg);
50+
}
51+
2052
public static void d(String tag, String msg){
21-
if(LogConfig.isLog){
22-
Log.d(tag,msg);
23-
}
53+
print(DEBUG,tag,msg);
54+
}
55+
56+
public static void v(String msg){
57+
print(VERBOSE,msg);
2458
}
2559

2660
public static void v(String tag, String msg){
27-
if(LogConfig.isLog){
28-
Log.v(tag,msg);
29-
}
61+
print(VERBOSE,tag,msg);
62+
}
63+
64+
public static void w(String msg){
65+
print(WARN,msg);
3066
}
3167

3268
public static void w(String tag, String msg){
33-
if(LogConfig.isLog){
34-
Log.w(tag,msg);
35-
}
69+
print(WARN,tag,msg);
70+
}
71+
72+
public static void e(String msg){
73+
print(ERROR,msg);
3674
}
3775

3876
public static void e(String tag, String msg){
39-
if(LogConfig.isLog){
40-
Log.e(tag,msg);
77+
print(VERBOSE,tag,msg);
78+
}
79+
80+
private static String getMessage(String msg){
81+
return msg;
82+
}
83+
84+
private static String getFileMessage(String type,String msg){
85+
return DateUtil.getDate(DateUtil.SIMPLE_FORMAT_1)
86+
+" "
87+
+DateUtil.getDate(DateUtil.SIMPLE_FORMAT_2)
88+
+" "
89+
+type
90+
+":"
91+
+msg
92+
+"\t\n"
93+
;
94+
}
95+
96+
/**根据不同类型打印日志*/
97+
private static void print(String type,String msg){
98+
print(type,null,msg);
99+
}
100+
101+
private static void print(String type,String tag,String msg){
102+
if(mLogConfig==null){
103+
throw new IllegalArgumentException("Logger没有初始化成功,LogConfig不能为null!");
104+
}
105+
106+
if(mLogConfig.isLog()){
107+
String t = TextUtils.isEmpty(tag)?mLogConfig.getTAG():tag;
108+
String message = getMessage(msg);
109+
if(INFO.equals(type)){
110+
Log.i(t,message);
111+
}
112+
else if(VERBOSE.equals(type)){
113+
Log.v(t,message);
114+
}
115+
else if(DEBUG.equals(type)){
116+
Log.e(t,message);
117+
}
118+
else if(WARN.equals(type)){
119+
Log.w(t,message);
120+
}
121+
else if(ERROR.equals(type)){
122+
Log.e(t,message);
123+
}
124+
}
125+
126+
if(mLogConfig.isSaveFile()){
127+
if(!LogUtil.isExistsSdcard()){
128+
Log.e(mLogConfig.getTAG(),"SdCard not exists!");
129+
return;
130+
}
131+
132+
writeToFile(getFileMessage(type,msg));
133+
}
134+
135+
}
136+
137+
private static void writeToFile(String logMsg){
138+
LogFile.getInstance(mLogConfig).writeToFile(logMsg);
139+
}
140+
141+
/**
142+
* 上传日志
143+
* @param context 上下文
144+
* @param uploadLogListener 上传结果回调接口
145+
*/
146+
public static void upLoadLog(Context context, UploadLog.UploadLogListener uploadLogListener){
147+
if(mLogConfig==null){
148+
throw new IllegalArgumentException("Logger没有初始化成功,LogConfig不能为null!");
41149
}
150+
UploadLog uploadLog = new UploadLog(context,uploadLogListener);
151+
uploadLog.setLogUrl(mLogConfig.getUploadUrl());
152+
uploadLog.setLogPath(mLogConfig.getLogPath());
153+
uploadLog.upload();
42154
}
43155

44156
}
Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,140 @@
11
package com.yhw.loglib.config;
22

3+
import android.text.TextUtils;
4+
5+
import com.yhw.loglib.utils.DateUtil;
6+
7+
import java.io.File;
8+
39
/**
410
* 日志配置
511
* Author: yhw on 2017-11-09.
612
*/
713

814
public class LogConfig {
15+
/**是否打印开关*/
16+
private final boolean isLog;
17+
/**是否将日志保存到文件*/
18+
private final boolean isSaveFile;
19+
/**日志TAG名称*/
20+
private final String TAG;
21+
/**日志文件名称*/
22+
private String logFileName;
23+
/**日志路径名称*/
24+
private String logPath;
25+
/**日志最大保存天数 默认7天*/
26+
private final int maxSaveDay;
27+
/**日志上传服务器地址*/
28+
private final String uploadUrl;
29+
30+
private LogConfig(Builder builder){
31+
this.isLog = builder.isLog;
32+
this.isSaveFile = builder.isSaveFile;
33+
this.TAG = builder.TAG;
34+
this.logFileName = builder.logFileName;
35+
this.logPath = builder.logPath;
36+
this.maxSaveDay = builder.maxSaveDay;
37+
this.uploadUrl = builder.uploadUrl;
38+
}
39+
40+
public boolean isLog() {
41+
return isLog;
42+
}
43+
44+
public boolean isSaveFile() {
45+
return isSaveFile;
46+
}
47+
48+
public String getTAG() {
49+
return TAG;
50+
}
51+
52+
public String getLogFileName() {
53+
return logFileName;
54+
}
55+
56+
public String getLogPath() {
57+
return logPath;
58+
}
59+
60+
public int getMaxSaveDay() {
61+
return maxSaveDay;
62+
}
63+
64+
public String getUploadUrl() {
65+
return uploadUrl;
66+
}
67+
68+
public static final class Builder{
69+
/**是否打印开关*/
70+
private boolean isLog = true;
71+
/**是否将日志保存到文件*/
72+
private boolean isSaveFile = false;
73+
/**日志TAG名称*/
74+
private String TAG = "LOG_TAG";
75+
/**日志文件名称*/
76+
private String logFileName;
77+
/**日志路径名称*/
78+
private String logPath;
79+
/**日志最大保存天数 默认7天*/
80+
private int maxSaveDay = 7;
81+
/**日志上传服务器地址*/
82+
private String uploadUrl;
83+
84+
public Builder setLog(boolean log) {
85+
isLog = log;
86+
return this;
87+
}
88+
89+
public Builder setSaveFile(boolean saveFile) {
90+
isSaveFile = saveFile;
91+
return this;
92+
}
93+
94+
public Builder setTAG(String TAG) {
95+
this.TAG = TAG;
96+
return this;
97+
}
98+
99+
public Builder setLogFileName(String logFileName) {
100+
this.logFileName = logFileName;
101+
return this;
102+
}
103+
104+
public Builder setLogPath(String logPath) {
105+
this.logPath = logPath;
106+
return this;
107+
}
108+
109+
public Builder setMaxSaveDay(int maxSaveDay) {
110+
this.maxSaveDay = maxSaveDay;
111+
return this;
112+
}
113+
114+
public Builder setUploadUrl(String uploadUrl) {
115+
this.uploadUrl = uploadUrl;
116+
return this;
117+
}
118+
119+
public LogConfig build(){
120+
LogConfig logConfig = new LogConfig(this);
121+
if(logConfig.isSaveFile){
122+
if(TextUtils.isEmpty(logConfig.logPath)){
123+
throw new IllegalArgumentException("日志路径设置错误,不能为空!");
124+
}else{
125+
logConfig.logPath = logConfig.logPath+File.separator+"fflog";
126+
}
9127

10-
public static boolean isLog = true;
128+
//如果名称设置为空或者没有设置 默认将日志名称设置为当前日期
129+
if(TextUtils.isEmpty(logConfig.logFileName)){
130+
logConfig.logFileName = DateUtil.getDate();
131+
}else{
132+
logConfig.logFileName = logConfig.logFileName+"_"+DateUtil.getDate();
133+
}
134+
}
135+
return logConfig;
136+
}
137+
}
11138

12139

13140
}

0 commit comments

Comments
 (0)