Skip to content

Commit

Permalink
Print warning message instead of throwing exception #103
Browse files Browse the repository at this point in the history
  • Loading branch information
elvishew committed Jun 15, 2021
1 parent 9d03d9c commit 6b42cd3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
4 changes: 3 additions & 1 deletion xlog/src/main/java/com/elvishew/xlog/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.elvishew.xlog.formatter.thread.ThreadFormatter;
import com.elvishew.xlog.interceptor.Interceptor;
import com.elvishew.xlog.internal.DefaultsFactory;
import com.elvishew.xlog.internal.Platform;
import com.elvishew.xlog.internal.SystemCompat;
import com.elvishew.xlog.internal.util.StackTraceUtil;
import com.elvishew.xlog.printer.Printer;
Expand Down Expand Up @@ -577,10 +578,11 @@ private void printlnInternal(int logLevel, String msg) {

// Check if the log still healthy.
if (log.tag == null || log.msg == null) {
throw new IllegalStateException("Interceptor " + interceptor
Platform.get().error("Interceptor " + interceptor
+ " should not remove the tag or message of a log,"
+ " if you don't want to print this log,"
+ " just return a null when intercept.");
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.elvishew.xlog.formatter.message.json;

import com.elvishew.xlog.formatter.FormatException;
import com.elvishew.xlog.internal.Platform;

import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -32,7 +33,8 @@ public class DefaultJsonFormatter implements JsonFormatter {
public String format(String json) {
String formattedString = null;
if (json == null || json.trim().length() == 0) {
throw new FormatException("JSON empty.");
Platform.get().warn("JSON empty.");
return "";
}
try {
if (json.startsWith("{")) {
Expand All @@ -42,10 +44,12 @@ public String format(String json) {
JSONArray jsonArray = new JSONArray(json);
formattedString = jsonArray.toString(JSON_INDENT);
} else {
throw new FormatException("JSON should start with { or [, but found " + json);
Platform.get().warn("JSON should start with { or [");
return json;
}
} catch (Exception e) {
throw new FormatException("Parse JSON error. JSON string:" + json, e);
Platform.get().warn(e.getMessage());
return json;
}
return formattedString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.elvishew.xlog.formatter.message.xml;

import com.elvishew.xlog.internal.Platform;
import com.elvishew.xlog.internal.SystemCompat;
import com.elvishew.xlog.formatter.FormatException;

Expand All @@ -41,7 +42,8 @@ public class DefaultXmlFormatter implements XmlFormatter {
public String format(String xml) {
String formattedString;
if (xml == null || xml.trim().length() == 0) {
throw new FormatException("XML empty.");
Platform.get().warn("XML empty.");
return "";
}
try {
Source xmlInput = new StreamSource(new StringReader(xml));
Expand All @@ -54,7 +56,8 @@ public String format(String xml) {
formattedString = xmlOutput.getWriter().toString().replaceFirst(">", ">"
+ SystemCompat.lineSeparator);
} catch (Exception e) {
throw new FormatException("Parse XML error. XML string:" + xml, e);
Platform.get().warn(e.getMessage());
return xml;
}
return formattedString;
}
Expand Down
9 changes: 9 additions & 0 deletions xlog/src/main/java/com/elvishew/xlog/internal/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void warn(String msg) {
System.out.println(msg);
}

public void error(String msg) {
System.out.println(msg);
}

private static Platform findPlatform() {
try {
Class.forName("android.os.Build");
Expand Down Expand Up @@ -101,5 +105,10 @@ Map<Class<?>, ObjectFormatter<?>> builtinObjectFormatters() {
public void warn(String msg) {
android.util.Log.w("XLog", msg);
}

@Override
public void error(String msg) {
android.util.Log.e("XLog", msg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AndroidPrinter implements Printer {

/**
* Generally, android has a default length limit of 4096 for single log, but
* some device(like HUAWEI) has its own shorter limit, so we just use 2048
* some device(like HUAWEI) has its own shorter limit, so we just use 4000
* and wish it could run well in all devices.
*/
static final int DEFAULT_MAX_CHUNK_SIZE = 4000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.elvishew.xlog.flattener.Flattener;
import com.elvishew.xlog.flattener.Flattener2;
import com.elvishew.xlog.internal.DefaultsFactory;
import com.elvishew.xlog.internal.Platform;
import com.elvishew.xlog.internal.printer.file.backup.BackupUtil;
import com.elvishew.xlog.printer.Printer;
import com.elvishew.xlog.printer.file.backup.BackupStrategy;
Expand Down Expand Up @@ -125,7 +126,8 @@ private void doPrintln(long timeMillis, int logLevel, String tag, String msg) {
if (lastFileName == null || isWriterClosed || fileNameGenerator.isFileNameChangeable()) {
String newFileName = fileNameGenerator.generateFileName(logLevel, System.currentTimeMillis());
if (newFileName == null || newFileName.trim().length() == 0) {
throw new IllegalArgumentException("File name should not be empty.");
Platform.get().error("File name should not be empty, ignore log: " + msg);
return;
}
if (!newFileName.equals(lastFileName) || isWriterClosed) {
writer.close();
Expand Down

0 comments on commit 6b42cd3

Please sign in to comment.