ログツールクラス
25951 ワード
ログツールクラスL.java
これはログツールクラスで、とても使いやすいです!共有して、機能をサポートします各種レベルのlogログを出力する:v、d、i、e
L.v(「msg」)はtagのログ出力を持たず、現在位置するファイル、方法、行数を印刷することができる.L.vt(「tag」,「msg」)はtagのログ出力をカスタマイズする.
jsonデータのフォーマット
L.json(「json_string」)はtagのjsonログ出力を持たない.L.jsonT(「tag」,「json_string」)tag付きjsonログ出力;
Applicationでは、パッケージ化時に自動シールドlog出力 が構成する.
コードは次のとおりです.
このファイルはここでダウンロードできます.
gits: L.java
これはログツールクラスで、とても使いやすいです!共有して、機能をサポートします
L.v(「msg」)はtagのログ出力を持たず、現在位置するファイル、方法、行数を印刷することができる.L.vt(「tag」,「msg」)はtagのログ出力をカスタマイズする.
L.json(「json_string」)はtagのjsonログ出力を持たない.L.jsonT(「tag」,「json_string」)tag付きjsonログ出力;
if (!BuildConfig.DEBUG) {
// debug
L.disableDebug();
}
コードは次のとおりです.
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/** * log , log, log tag, log .<br/> * Tag Log, {@link #vt(String, String)} | * {@link #dt(String, String)} .<br/> * <br/> * Log, {@link #enableDebug()} .( Log) <br/> * Log, {@link #disableDebug()} .( Log) <br/> * <br/> * Log , {@link #enableFileLog(Context)} .( Log ) <br/> * Log , {@link #disableFileLog()} .( Log ) <br/> * * <pre> * Application BaseApplication}, . * , : * Application onCreate . , , Log. * if(!BuildConfig.DEBUG){// debug * L.disableDebug(); * } * * </pre> * * @author Kingt * */
public class L {
private static boolean DEBUG = true;
private static boolean LOG_ON_FILE = false;
private static String packageName = "";
private static String sClassName = null;
private static String sMethodName = null;
/** * */
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
/** * Json */
private static final int JSON_INDENT = 4;
/** * : 、 */
private static void getTrace() {
StackTraceElement caller = new Throwable().fillInStackTrace()
.getStackTrace()[2];
String className = new StringBuilder().append(caller.getClassName())
.toString();
sClassName = className.substring(className.lastIndexOf(".") + 1);
sMethodName = new StringBuilder().append(
caller.getMethodName() + "->" + caller.getLineNumber() + ": ")
.toString();
}
/** * Verbose * @param text */
public static void v(String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.v(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.VERBOSE, sClassName, message);
}
public static void v(String text, Object... args) {
if (!DEBUG)
return;
try {
text = String.format(text, args);
} catch (RuntimeException e) {
}
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.v(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.VERBOSE, sClassName, message);
}
public static void vt(String tag, String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.v(tag, message);
if (LOG_ON_FILE)
logOnFile(Log.VERBOSE, tag, message);
}
/** * Log, tag, log . * * @param text * Log */
public static void d(String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.d(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.DEBUG, sClassName, message);
}
/** * * Log, tag, log . , , * text <br/> * <br/> * * : * * <pre> * L.d(" :%s :%d", " ", 100); * LogCat :" : :100" * </pre> * * @param text * Log , %s %d %f * @param args * %s %d %f * @see String#format(String, Object...) */
public static void d(String text, Object... args) {
if (!DEBUG)
return;
try {
text = String.format(text, args);
} catch (RuntimeException e) {
}
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.d(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.DEBUG, sClassName, message);
}
/** * Tag Log, . * * @param tag * Tag * @param text * */
public static void dt(String tag, String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.d(tag, message);
if (LOG_ON_FILE)
logOnFile(Log.DEBUG, tag, message);
}
public static void i(String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.i(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.INFO, sClassName, message);
}
public static void i(String text, Object... args) {
if (!DEBUG)
return;
try {
text = String.format(text, args);
} catch (RuntimeException e) {
}
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.i(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.INFO, sClassName, message);
}
public static void w(String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.w(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.WARN, sClassName, message);
}
public static void w(String text, Object... args) {
if (!DEBUG)
return;
try {
text = String.format(text, args);
} catch (RuntimeException e) {
}
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.w(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.WARN, sClassName, message);
}
public static void e(String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.e(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.ERROR, sClassName, message);
}
/** * Tag Log, . * * @param tag * Tag * @param text * */
public static void et(String tag, String text) {
if (!DEBUG)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.e(tag, message);
if (LOG_ON_FILE)
logOnFile(Log.DEBUG, tag, message);
}
/** * Json tag , Tag * @param msg */
public static void json(String msg){
if (!DEBUG)
return;
if (TextUtils.isEmpty(msg)) {
Log.d(sClassName, "Empty or Null json content");
return;
}
getTrace();
String message = "";
try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(JSON_INDENT);
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(JSON_INDENT);
}
} catch (JSONException e) {
d(sClassName, e.getCause().getMessage() + "
" + msg);
return;
}
printLine(sClassName, true);
message = sClassName + "->" + sMethodName + LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
StringBuilder jsonContent = new StringBuilder();
for (String line : lines) {
jsonContent.append("║ ").append(line).append(LINE_SEPARATOR);
}
Log.d(sClassName, jsonContent.toString());
printLine(sClassName, false);
if (LOG_ON_FILE)
logOnFile(Log.DEBUG, sClassName, message);
}
/** * json * @param tag tag * @param msg json */
public static void jsonT(String tag , String msg){
if (!DEBUG)
return;
if (TextUtils.isEmpty(msg)) {
Log.d(sClassName, "Empty or Null json content");
return;
}
getTrace();
String message = "";
try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(JSON_INDENT);
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(JSON_INDENT);
}
} catch (JSONException e) {
dt(tag, e.getCause().getMessage() + "
" + msg);
return;
}
printLine(tag, true);
message = sClassName + "->" + sMethodName + LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
StringBuilder jsonContent = new StringBuilder();
for (String line : lines) {
jsonContent.append("║ ").append(line).append(LINE_SEPARATOR);
}
Log.d(tag, jsonContent.toString());
printLine(tag, false);
if (LOG_ON_FILE)
logOnFile(Log.DEBUG, tag, message);
}
private static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════");
} else {
Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════");
}
}
public static void e(String text, Object... args) {
if (!DEBUG)
return;
try {
text = String.format(text, args);
} catch (RuntimeException e) {
}
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.e(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.ERROR, sClassName, message);
}
public static void e(Throwable e) {
if (!DEBUG)
return;
if (e == null)
return;
getTrace();
String message = sClassName + "->" + sMethodName + e.toString();
Log.e(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.ERROR, sClassName, message);
}
public static void e(Throwable e, String text) {
if (!DEBUG)
return;
if (e == null)
return;
getTrace();
String message = sClassName + "->" + sMethodName + text;
Log.e(sClassName, message);
if (LOG_ON_FILE)
logOnFile(Log.ERROR, sClassName, sClassName);
}
private static void logOnFile(int level, String tag, String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.CHINA);
String time = sdf.format(new Date());
FileWriter writer = null;
try {
File logFile = new File(getCachedFolder() + "log.log");
if (logFile.exists() && logFile.length() > 100 * 1024) {// 100KB
logFile.renameTo(new File(getCachedFolder() + "log_" + time
+ ".log"));
}
writer = new FileWriter(logFile, true);
writer.write(time + "/" + level + "/" + tag + "/" + text);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
}
}
}
/** * Log * * @return Log */
private static String getCachedFolder() {
String path;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// SD
path = Environment.getExternalStorageDirectory().getPath()
+ "/Android/data/" + packageName + "/log/";
} else {
path = Environment.getDataDirectory().getPath() + "/data/"
+ packageName + "/log/";
}
File file = new File(path);
if (!file.isDirectory()) {
file.mkdirs();
}
return path;
}
/** * */
public static void enableDebug() {
DEBUG = true;
}
/** * */
public static void disableDebug() {
DEBUG = false;
}
/** * Log ( ) */
public static void enableFileLog(Context context) {
LOG_ON_FILE = true;
if (context != null)
packageName = context.getPackageName();
}
/** * Log ( ) */
public static void disableFileLog() {
LOG_ON_FILE = false;
}
}
このファイルはここでダウンロードできます.
gits: L.java