最近换了工作,手头还有些忙,因此最近没太更新博客,其实做开发的总有种焦虑。担心年龄越来越大越来越难学知识,担心技术还能做多久,担心自己35以后还能继续做多久,担心如果做不了技术了,又该何去何从... ... 其实每个人或多或少都会有焦虑感,我也一样,这是中国当下的焦虑。
年入5000万人民币的日本人,他戳痛每个中国人的焦虑
学习之余也不妨看看社会性文章。
好了,闲话少说,写博客也是为了储备知识,将来对技术有个系统的资料查询,有备无患。
一、技术原理:
Log日志相信大家并不陌生,多数用来记录系统运行时状态的,常被开发人员作为调试的一种手段。Consle控制台会显示相应的信息
既然是日志的打印,我们就像能不能将任意格式的数据都打印出来,供我们查看呢?
答案是肯定的,但我们需要写很多代码去适配不同的文件。
本文只针对XML,JSON,File做出了适配,其他的如果大家有需求,可以留言,或者直接联系我微信:jambestwick
,一起研究。
二、实现代码
(1)Xml 的log日志
package com.huawei.genexcloud.base.framework.log.logtype;
import android.util.Log;
import com.huawei.genexcloud.base.framework.log.GCLogger;
import com.huawei.genexcloud.base.framework.log.LoggerUtil;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
* @author jambestwick
* date 2017-5-21
*/
public class XmlLog {
public static void printXml(String tag, String xml, String headString) {
if (xml != null) {
xml = XmlLog.formatXML(xml);
xml = headString + "\n" + xml;
} else {
xml = headString + GCLogger.NULL_TIPS;
}
LoggerUtil.printLine(tag, true);
String[] lines = xml.split(GCLogger.LINE_SEPARATOR);
for (String line : lines) {
if (!LoggerUtil.isEmpty(line)) {
Log.d(tag, "║ " + line);
}
}
LoggerUtil.printLine(tag, false);
}
private static String formatXML(String inputXML) {
try {
Source xmlInput = new StreamSource(new StringReader(inputXML));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (Exception e) {
e.printStackTrace();
return inputXML;
}
}
}
(2)Json的log日志
package com.huawei.genexcloud.base.framework.log.logtype;
import android.util.Log;
import com.huawei.genexcloud.base.framework.log.GCLogger;
import com.huawei.genexcloud.base.framework.log.LoggerUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
*/
public class JsonLog {
public static void printJson(String tag, String msg, String headString) {
String message;
try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(GCLogger.JSON_INDENT);
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(GCLogger.JSON_INDENT);
} else {
message = msg;
}
} catch (JSONException e) {
message = msg;
}
LoggerUtil.printLine(tag, true);
message = headString + GCLogger.LINE_SEPARATOR + message;
String[] lines = message.split(GCLogger.LINE_SEPARATOR);
for (String line : lines) {
Log.d(tag, "║ " + line);
}
LoggerUtil.printLine(tag, false);
}
}
(3)文件的log日志
package com.huawei.genexcloud.base.framework.log.logtype;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Random;
/**
* 文件log日志
*/
public class FileLog {
private static final String FILE_PREFIX = "ZLLog_";
private static final String FILE_FORMAT = ".log";
public static void printFile(String tag, File targetDirectory, @Nullable String fileName, String headString, String msg) {
fileName = (fileName == null) ? getFileName() : fileName;
if (save(targetDirectory, fileName, msg)) {
Log.d(tag, headString + " save log success ! location is >>>" + targetDirectory.getAbsolutePath() + File.separator + fileName);
} else {
Log.e(tag, headString + "save log fails !");
}
}
private static boolean save(File dic, @NonNull String fileName, String msg) {
File file = new File(dic, fileName);
try {
OutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
outputStreamWriter.write(msg);
outputStreamWriter.flush();
outputStream.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static String getFileName() {
Random random = new Random();
return FILE_PREFIX + Long.toString(System.currentTimeMillis() + random.nextInt(10000)).substring(4) + FILE_FORMAT;
}
}
核心代码都在,还有对各种logType的封装
package com.huawei.genexcloud.base.framework.log;
import android.text.TextUtils;
import android.util.Log;
/**
* @author jambestwick
*/
public class LoggerUtil {
public static boolean isEmpty(String line) {
return TextUtils.isEmpty(line) || line.equals("\n") || line.equals("\t") || TextUtils.isEmpty(line.trim());
}
public static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
} else {
Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
}
}
}
package com.huawei.genexcloud.base.framework.log;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.huawei.genexcloud.base.framework.log.logtype.BaseLog;
import com.huawei.genexcloud.base.framework.log.logtype.FileLog;
import com.huawei.genexcloud.base.framework.log.logtype.JsonLog;
import com.huawei.genexcloud.base.framework.log.logtype.XmlLog;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* GCLogger is a wrapper of {@link android.util.Log}
* But more pretty, simple and powerful
* @author jambestwick
*/
public final class GCLogger {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String NULL_TIPS = "Log with null object";
private static final String DEFAULT_MESSAGE = "execute";
private static final String PARAM = "Param";
private static final String NULL = "null";
private static final String TAG_DEFAULT = "KLog";
private static final String SUFFIX = ".java";
public static final int JSON_INDENT = 4;
public static final int VERBOSE = 0x1;
public static final int DEBUG = 0x2;
public static final int INFO = 0x3;
public static final int WARN = 0x4;
public static final int ERROR = 0x5;
public static final int ASSERT = 0x6;
private static final int JSON = 0x7;
private static final int XML = 0x8;
private static final int STACK_TRACE_INDEX_5 = 5;
private static final int STACK_TRACE_INDEX_4 = 4;
private static String mGlobalTag;
private static boolean mIsGlobalTagEmpty = true;
public static boolean IS_SHOW_LOG = true;
public static final boolean IS_DEBUG_MODE = true;
public static void init(boolean isShowLog) {
IS_SHOW_LOG = isShowLog;
}
public static void init(boolean isShowLog, @Nullable String tag) {
IS_SHOW_LOG = isShowLog;
mGlobalTag = tag;
mIsGlobalTagEmpty = TextUtils.isEmpty(mGlobalTag);
}
public static void verbose() {
printLog(VERBOSE, null, DEFAULT_MESSAGE);
}
public static void verbose(Object msg) {
printLog(VERBOSE, null, msg);
}
public static void verbose(String tag, Object... objects) {
printLog(VERBOSE, tag, objects);
}
public static void debug() {
if(IS_DEBUG_MODE){
printLog(DEBUG, null, DEFAULT_MESSAGE);
}
}
public static void debug(Object msg) {
if(IS_DEBUG_MODE){
printLog(DEBUG, null, msg);
}
}
public static void debug(String tag, Object... objects) {
if(IS_DEBUG_MODE){
printLog(DEBUG, tag, objects);
}
}
public static void info() {
printLog(INFO, null, DEFAULT_MESSAGE);
}
public static void info(Object msg) {
printLog(INFO, null, msg);
}
public static void info(String tag, Object... objects) {
printLog(INFO, tag, objects);
}
public static void warn() {
printLog(WARN, null, DEFAULT_MESSAGE);
}
public static void warn(Object msg) {
printLog(WARN, null, msg);
}
public static void warn(String tag, Object... objects) {
printLog(WARN, tag, objects);
}
public static void error() {
printLog(ERROR, null, DEFAULT_MESSAGE);
}
public static void error(Object msg) {
printLog(ERROR, null, msg);
}
public static void error(String tag, Object... objects) {
printLog(ERROR, tag, objects);
}
public static void a() {
printLog(ASSERT, null, DEFAULT_MESSAGE);
}
public static void a(Object msg) {
printLog(ASSERT, null, msg);
}
public static void a(String tag, Object... objects) {
printLog(ASSERT, tag, objects);
}
public static void json(String jsonFormat) {
printLog(JSON, null, jsonFormat);
}
public static void json(String tag, String jsonFormat) {
printLog(JSON, tag, jsonFormat);
}
public static void xml(String xml) {
printLog(XML, null, xml);
}
public static void xml(String tag, String xml) {
printLog(XML, tag, xml);
}
public static void file(File targetDirectory, Object msg) {
printFile(null, targetDirectory, null, msg);
}
public static void file(String tag, File targetDirectory, Object msg) {
printFile(tag, targetDirectory, null, msg);
}
public static void file(String tag, File targetDirectory, String fileName, Object msg) {
printFile(tag, targetDirectory, fileName, msg);
}
/*public static void debug() {
printDebug(null, DEFAULT_MESSAGE);
}
public static void debug(Object msg) {
printDebug(null, msg);
}
public static void debug(String tag, Object... objects) {
printDebug(tag, objects);
}*/
public static void trace() {
printStackTrace();
}
private static void printStackTrace() {
if (!IS_SHOW_LOG) {
return;
}
Throwable tr = new Throwable();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
pw.flush();
String message = sw.toString();
String traceString[] = message.split("\\n\\t");
StringBuilder sb = new StringBuilder();
sb.append("\n");
for (String trace : traceString) {
if (trace.contains("at com.socks.library.KLog")) {
continue;
}
sb.append(trace).append("\n");
}
String[] contents = wrapperContent(STACK_TRACE_INDEX_4, null, sb.toString());
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
BaseLog.printDefault(DEBUG, tag, headString + msg);
}
private static void printLog(int type, String tagStr, Object... objects) {
if (!IS_SHOW_LOG) {
return;
}
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
switch (type) {
case VERBOSE:
case DEBUG:
case INFO:
case WARN:
case ERROR:
case ASSERT:
BaseLog.printDefault(type, tag, headString + msg);
break;
case JSON:
JsonLog.printJson(tag, msg, headString);
break;
case XML:
XmlLog.printXml(tag, msg, headString);
break;
}
}
private static void printLog(int type, String tagStr, Object objects) {
if (!IS_SHOW_LOG) {
return;
}
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
switch (type) {
case VERBOSE:
case DEBUG:
case INFO:
case WARN:
case ERROR:
case ASSERT:
BaseLog.printDefault(type, tag, headString + msg);
break;
case JSON:
JsonLog.printJson(tag, msg, headString);
break;
case XML:
XmlLog.printXml(tag, msg, headString);
break;
}
}
private static void printDebug(String tagStr, Object... objects) {
if (!IS_SHOW_LOG) {
return;
}
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
BaseLog.printDefault(DEBUG, tag, headString + msg);
}
private static void printFile(String tagStr, File targetDirectory, String fileName, Object objectMsg) {
if (!IS_SHOW_LOG) {
return;
}
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objectMsg);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
FileLog.printFile(tag, targetDirectory, fileName, headString, msg);
}
private static String[] wrapperContent(int stackTraceIndex, String tagStr, Object... objects) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement targetElement = stackTrace[stackTraceIndex];
String className = targetElement.getClassName();
String[] classNameInfo = className.split("\\.");
if (classNameInfo.length > 0) {
className = classNameInfo[classNameInfo.length - 1] + SUFFIX;
}
if (className.contains("$")) {
className = className.split("\\$")[0] + SUFFIX;
}
String methodName = targetElement.getMethodName();
int lineNumber = targetElement.getLineNumber();
if (lineNumber < 0) {
lineNumber = 0;
}
String tag = (tagStr == null ? className : tagStr);
if (mIsGlobalTagEmpty && TextUtils.isEmpty(tag)) {
tag = TAG_DEFAULT;
} else if (!mIsGlobalTagEmpty) {
tag = mGlobalTag;
}
String msg = (objects == null) ? NULL_TIPS : getObjectsString(objects);
String headString = "[ (" + className + ":" + lineNumber + ")#" + methodName + " ] ";
return new String[]{tag, msg, headString};
}
private static String getObjectsString(Object... objects) {
if (objects.length > 1) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n");
for (int i = 0; i < objects.length; i++) {
Object object = objects[i];
if (object == null) {
stringBuilder.append(PARAM).append("[").append(i).append("]").append(" = ").append(NULL).append("\n");
} else {
stringBuilder.append(PARAM).append("[").append(i).append("]").append(" = ").append(object.toString()).append("\n");
}
}
return stringBuilder.toString();
} else {
Object object = objects[0];
return object == null ? NULL : object.toString();
}
}
}
项目代码https://github.com/jambestwick/LoggerUtil
非经本人允许,不得私自盗用!!!