提交 a20fe7e0 编写于 作者: Z ZhaoKaiQiang

增加对log的保存支持

上级 b5d8b674
......@@ -3,6 +3,7 @@ package com.socks.sample;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
......@@ -15,6 +16,7 @@ public class MainActivity extends AppCompatActivity {
private static final String LOG_MSG = "KLog is a so cool Log Tool!";
private static final String JSON = "{\"menu\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"tag\":\"其他\"}";
private static final String JSON_LONG = "[{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},{\"中国食谱\":[\"上海醉蟹\",\"北京锅烧鸡\",\"川式凉拌豇豆\",\"清真酱牛肉\"],\"国外食谱\":[\"泰式柠檬肉片\",\"鸡柳汉堡\",\"蒸桂鱼卷 \"],\"更多\":{\"中式\":[\"南\",\"北\",{\"地方\":[\"小吃\",\"大餐\"]},\"更多选择\"]}},{\"菜谱分类\":[\"上海菜\",\"贵州菜\",\"潮汕菜\",\"云南菜\",\"东北菜\",\"安徽菜\",\"广东菜\",\"浙江菜\",\"湖南菜\"]},\"其它\"]";
private static final String TAG = "KLog";
@Override
......@@ -50,7 +52,7 @@ public class MainActivity extends AppCompatActivity {
KLog.a(TAG, LOG_MSG);
}
public void logWithNull(View view){
public void logWithNull(View view) {
KLog.v(null);
KLog.d(null);
KLog.i(null);
......@@ -63,10 +65,20 @@ public class MainActivity extends AppCompatActivity {
KLog.json(JSON);
}
public void logWithLongJson(View view) {
KLog.json(JSON_LONG);
}
public void logWithJsonTag(View view) {
KLog.json(TAG, JSON);
}
public void logWithFile(View view) {
KLog.file(Environment.getExternalStorageDirectory(), JSON_LONG);
KLog.file(TAG, Environment.getExternalStorageDirectory(), JSON_LONG);
KLog.file(TAG, Environment.getExternalStorageDirectory(), "test.txt", JSON_LONG);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_about, menu);
......
......@@ -28,6 +28,13 @@
android:layout_height="wrap_content"
android:onClick="logWithJson"
android:text="Log(JSON)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithLongJson"
android:text="Log(LONG JSON)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
......@@ -38,4 +45,11 @@
android:layout_height="wrap_content"
android:onClick="logWithNull"
android:text="Log(NULL)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithFile"
android:text="Log(File)" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.socks.klog"></manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.socks.klog">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
package com.socks.library;
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;
/**
* Created by zhaokaiqiang on 15/11/17.
*/
public class FileHelper {
public static boolean save(File dic, 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();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static String getFileName() {
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder("KLog_");
stringBuilder.append(Long.toString(System.currentTimeMillis()+random.nextInt(10000)).substring(4));
stringBuilder.append(".txt");
return stringBuilder.toString();
}
}
......@@ -7,6 +7,8 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
/**
* This is a Log tool,with this you can the following
* <ol>
......@@ -17,6 +19,7 @@ import org.json.JSONObject;
*
* @author zhaokaiqiang
* github https://github.com/ZhaoKaiQiang/KLog
* 15/11/17 扩展功能,添加对文件的支持
*/
public class KLog {
......@@ -33,6 +36,7 @@ public class KLog {
private static final int E = 0x5;
private static final int A = 0x6;
private static final int JSON = 0x7;
private static final int FILE = 0x8;
public static void init(boolean isShowLog) {
IS_SHOW_LOG = isShowLog;
......@@ -110,7 +114,6 @@ public class KLog {
printLog(A, tag, msg);
}
public static void json(String jsonFormat) {
printLog(JSON, null, jsonFormat);
}
......@@ -119,9 +122,20 @@ public class KLog {
printLog(JSON, tag, jsonFormat);
}
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);
}
private static void printLog(int type, String tagStr, Object objectMsg) {
String msg;
if (!IS_SHOW_LOG) {
return;
}
......@@ -134,22 +148,41 @@ public class KLog {
int lineNumber = stackTrace[index].getLineNumber();
String tag = (tagStr == null ? className : tagStr);
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodName).append(" ] ");
stringBuilder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodNameShort).append(" ] ");
String msg = (objectMsg == null) ? "Log with null Object" : objectMsg.toString();
if (objectMsg == null) {
msg = "Log with null Object";
} else {
msg = objectMsg.toString();
}
if (msg != null && type != JSON) {
stringBuilder.append(msg);
}
String logStr = stringBuilder.toString();
switch (type) {
case V:
case D:
case I:
case W:
case E:
case A:
printLog(type, tag, logStr);
break;
case JSON: {
if (TextUtils.isEmpty(msg)) {
Log.e(tag, "Empty or Null json content");
return;
}
printJson(tag, msg, logStr);
}
break;
}
}
private static void printLog(int type, String tag, String logStr) {
switch (type) {
case V:
Log.v(tag, logStr);
......@@ -169,41 +202,69 @@ public class KLog {
case A:
Log.wtf(tag, logStr);
break;
case JSON: {
}
}
if (TextUtils.isEmpty(msg)) {
Log.d(tag, "Empty or Null json content");
return;
}
private static void printJson(String tag, String msg, String logStr) {
String message = null;
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) {
e(tag, e.getCause().getMessage() + "\n" + msg);
return;
}
String message = null;
printLine(tag, true);
message = logStr + 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);
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);
}
break;
} catch (JSONException e) {
e(tag, e.getCause().getMessage() + "\n" + msg);
return;
}
printLine(tag, true);
message = logStr + 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);
}
private static void printFile(String tag, File targetDirectory, String fileName, Object objectMsg) {
if (!IS_SHOW_LOG) {
return;
}
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
int index = 4;
String className = stackTrace[index].getFileName();
String methodName = stackTrace[index].getMethodName();
int lineNumber = stackTrace[index].getLineNumber();
tag = (tag == null ? className : tag);
String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodNameShort).append(" ] ");
String msg = (objectMsg == null) ? "Log with null Object" : objectMsg.toString();
String headString = stringBuilder.toString();
if (msg != null) {
msg = headString + msg;
}
fileName = (fileName == null) ? FileHelper.getFileName() : fileName;
if (FileHelper.save(targetDirectory, fileName, msg)) {
Log.d(tag, headString + " save log success ! location is >>>" + targetDirectory.getAbsolutePath() + "/" + fileName);
} else {
Log.e(tag, headString + "save log fails !");
}
}
private static void printLine(String tag, boolean isTop) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册