Logger.java
4.32 KB
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
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2020. All rights reserved.
*/
package com.wd.base.log.wheat;
import java.io.FileNotFoundException;
import java.net.BindException;
import java.net.SocketTimeoutException;
import java.security.acl.NotOwnerException;
import java.util.ConcurrentModificationException;
import java.util.MissingResourceException;
import java.util.jar.JarException;
import org.json.JSONException;
import android.database.SQLException;
import android.util.Log;
/**
* <日志工具类>
*
* @author wangnaiwen
* @version [V1.0.0.0, 2020/5/16]
* @since V1.0.0.0
*/
final class Logger {
/**
* 播放模块统一日志标签
*/
public static final String DEFAULT_TAG = "<WHEAT>|";
/**
* 播放模块统一日志标签
*/
public static final String PLAYER_TAG = "<PLAYER>|";
/**
* 播放模块统一日志标签
*/
public static final String NETWORK_TAG = "<NETWORK>|";
private static int mLogLevel = Log.DEBUG;
private Logger() {
}
public static void setLogLevel(int level) {
mLogLevel = level;
}
public static void d(String tag, Object message) {
if (mLogLevel <= Log.DEBUG) {
printLog(tag, message, Log.DEBUG);
}
}
public static void i(String tag, Object message) {
if (mLogLevel <= Log.INFO) {
printLog(tag, message, Log.INFO);
}
}
public static void w(String tag, Object message) {
if (mLogLevel <= Log.WARN) {
printLog(tag, message, Log.WARN);
}
}
public static void e(String tag, Object message) {
if (mLogLevel <= Log.ERROR) {
printLog(tag, message, Log.ERROR);
}
}
public static void d(String tag, Object message, Throwable throwable) {
if (mLogLevel <= Log.DEBUG) {
String newMessage = message + ":" + checkSensitiveException(throwable);
printLog(tag, newMessage, Log.DEBUG);
}
}
public static void i(String tag, Object message, Throwable throwable) {
if (mLogLevel <= Log.INFO) {
String newMessage = message + ":" + checkSensitiveException(throwable);
printLog(tag, newMessage, Log.INFO);
}
}
public static void w(String tag, Object message, Throwable throwable) {
if (mLogLevel <= Log.WARN) {
String newMessage = message + ":" + checkSensitiveException(throwable);
printLog(tag, newMessage, Log.WARN);
}
}
public static void e(String tag, Object message, Throwable throwable) {
if (mLogLevel <= Log.ERROR) {
String newMessage = message + ":" + checkSensitiveException(throwable);
printLog(tag, newMessage, Log.ERROR);
}
}
private static String checkSensitiveException(Object object) {
if (object instanceof FileNotFoundException) {
return "File is not legal ";
} else if (object instanceof ConcurrentModificationException) {
return "Illegal operation";
} else if (object instanceof SQLException) {
return "Sql exception";
} else if (object instanceof JSONException) {
return "Json convert exception";
} else if (object instanceof MissingResourceException) {
return "Resource is missing.";
} else if (object instanceof JarException) {
return "Error occurred while reading or writing a JAR file.";
} else if (object instanceof OutOfMemoryError) {
return "No more memory could be made available.";
} else if (object instanceof StackOverflowError) {
return "Stack overflow occurs because an application recurses too deeply.";
} else if (object instanceof NotOwnerException) {
return "Modification principal is not the owner of the object.";
} else if (object instanceof BindException) {
return "Exception occurred while binding a socket to a local address and port.";
} else {
return object instanceof SocketTimeoutException ? "Socket timeout exception" : null;
}
}
private static void printLog(String tag, Object msg, int level) {
// 日志打印统一出口
String logTag = tag == null ? DEFAULT_TAG : DEFAULT_TAG + tag;
Log.println(level, logTag, String.valueOf(msg));
}
}