diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b434e3f..96c45335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # CHANGELOG +### 1.15 +- Logger.log signature is changed +```java +Logger.log(int priority, String tag, String message, Throwable throwable); +``` ### 1.14 - Logger.log(int priority, String tag, Object... args) added. diff --git a/README.md b/README.md index 72a474c3..2a1c6f03 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Logger provides : ### Dependency ```groovy -compile 'com.orhanobut:logger:1.14' +compile 'com.orhanobut:logger:1.15' ``` ### Current Log system @@ -43,7 +43,7 @@ Logger.v("hello"); Logger.wtf("hello"); Logger.json(JSON_CONTENT); Logger.xml(XML_CONTENT); -Logger.log(DEBUG, "message %s", "args"); +Logger.log(DEBUG, "tag", "message", throwable); ``` #### String format arguments are supported diff --git a/gradle.properties b/gradle.properties index 76f21c66..bfdc4913 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.14 +VERSION_NAME=1.15 GROUP=com.orhanobut POM_DESCRIPTION=Simple, Pretty and Advanced Logger diff --git a/maven_push.gradle b/gradle/maven_push.gradle similarity index 100% rename from maven_push.gradle rename to gradle/maven_push.gradle diff --git a/logger/build.gradle b/logger/build.gradle index ed0d138c..579096e5 100644 --- a/logger/build.gradle +++ b/logger/build.gradle @@ -21,4 +21,4 @@ dependencies { testCompile deps.mockito } -apply from: rootProject.file('maven_push.gradle') \ No newline at end of file +apply from: rootProject.file('gradle/maven_push.gradle') \ No newline at end of file diff --git a/logger/src/main/java/com/orhanobut/logger/Logger.java b/logger/src/main/java/com/orhanobut/logger/Logger.java index 40232c8b..4f6ba77b 100644 --- a/logger/src/main/java/com/orhanobut/logger/Logger.java +++ b/logger/src/main/java/com/orhanobut/logger/Logger.java @@ -55,8 +55,8 @@ public static Printer t(String tag, int methodCount) { return printer.t(tag, methodCount); } - public static void log(int priority, String message, Object... args) { - printer.log(priority, message, args); + public static void log(int priority, String tag, String message, Throwable throwable) { + printer.log(priority, tag, message, throwable); } public static void d(String message, Object... args) { diff --git a/logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java b/logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java index 8206fbe2..aa9f8168 100644 --- a/logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java +++ b/logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java @@ -107,7 +107,7 @@ public LoggerPrinter() { } @Override public void d(String message, Object... args) { - log(DEBUG, message, args); + log(DEBUG, null, message, args); } @Override public void d(Object object) { @@ -117,7 +117,7 @@ public LoggerPrinter() { } else { message = object.toString(); } - log(DEBUG, message); + log(DEBUG, null, message); } @Override public void e(String message, Object... args) { @@ -125,32 +125,23 @@ public LoggerPrinter() { } @Override public void e(Throwable throwable, String message, Object... args) { - if (throwable != null && message != null) { - message += " : " + Helper.getStackTraceString(throwable); - } - if (throwable != null && message == null) { - message = Helper.getStackTraceString(throwable); - } - if (message == null) { - message = "No message/exception is set"; - } - log(ERROR, message, args); + log(ERROR, throwable, message, args); } @Override public void w(String message, Object... args) { - log(WARN, message, args); + log(WARN, null, message, args); } @Override public void i(String message, Object... args) { - log(INFO, message, args); + log(INFO, null, message, args); } @Override public void v(String message, Object... args) { - log(VERBOSE, message, args); + log(VERBOSE, null, message, args); } @Override public void wtf(String message, Object... args) { - log(ASSERT, message, args); + log(ASSERT, null, message, args); } /** @@ -206,21 +197,20 @@ public LoggerPrinter() { } } - @Override public void resetSettings() { - settings.reset(); - } - - /** - * This method is synchronized in order to avoid messy of logs' order. - */ - @Override public synchronized void log(int priority, String msg, Object... args) { + @Override public synchronized void log(int priority, String tag, String message, Throwable throwable) { if (settings.getLogLevel() == LogLevel.NONE) { return; } - String tag = getTag(); - String message = createMessage(msg, args); + if (throwable != null && message != null) { + message += " : " + Helper.getStackTraceString(throwable); + } + if (throwable != null && message == null) { + message = Helper.getStackTraceString(throwable); + } + if (message == null) { + message = "No message/exception is set"; + } int methodCount = getMethodCount(); - if (Helper.isEmpty(message)) { message = "Empty/NULL log message"; } @@ -250,6 +240,22 @@ public LoggerPrinter() { logBottomBorder(priority, tag); } + @Override public void resetSettings() { + settings.reset(); + } + + /** + * This method is synchronized in order to avoid messy of logs' order. + */ + private synchronized void log(int priority, Throwable throwable, String msg, Object... args) { + if (settings.getLogLevel() == LogLevel.NONE) { + return; + } + String tag = getTag(); + String message = createMessage(msg, args); + log(priority, tag, message, throwable); + } + private void logTopBorder(int logType, String tag) { logChunk(logType, tag, TOP_BORDER); } diff --git a/logger/src/main/java/com/orhanobut/logger/Printer.java b/logger/src/main/java/com/orhanobut/logger/Printer.java index c801dea5..a8dce327 100644 --- a/logger/src/main/java/com/orhanobut/logger/Printer.java +++ b/logger/src/main/java/com/orhanobut/logger/Printer.java @@ -28,7 +28,7 @@ public interface Printer { void xml(String xml); - void log(int priority, String message, Object... args); + void log(int priority, String tag, String message, Throwable throwable); void resetSettings(); diff --git a/logger/src/test/java/com.orhanobut.logger/LogAssert.java b/logger/src/test/java/com.orhanobut.logger/LogAssert.java new file mode 100644 index 00000000..7ca19a0d --- /dev/null +++ b/logger/src/test/java/com.orhanobut.logger/LogAssert.java @@ -0,0 +1,104 @@ +package com.orhanobut.logger; + +import org.robolectric.shadows.ShadowLog; + +import java.util.List; + +import static com.google.common.truth.Truth.assertThat; + +final class LogAssert { + private static final String DEFAULT_TAG = "PRETTYLOGGER"; + + private static final char TOP_LEFT_CORNER = '╔'; + private static final char BOTTOM_LEFT_CORNER = '╚'; + private static final char MIDDLE_CORNER = '╟'; + private static final char HORIZONTAL_DOUBLE_LINE = '║'; + private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════"; + private static final String SINGLE_DIVIDER = "────────────────────────────────────────────"; + private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER; + private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER; + private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER; + + private final List items; + private final int priority; + + private String tag; + + private int index = 0; + + LogAssert(List items, String tag, int priority) { + this.items = items; + this.tag = tag == null ? DEFAULT_TAG : tag; + this.priority = priority; + } + + public LogAssert hasTopBorder() { + return hasLog(priority, tag, TOP_BORDER); + } + + public LogAssert hasBottomBorder() { + return hasLog(priority, tag, BOTTOM_BORDER); + } + + public LogAssert hasMiddleBorder() { + return hasLog(priority, tag, MIDDLE_BORDER); + } + + public LogAssert hasThread(String threadName) { + return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + "Thread: " + threadName); + } + + public LogAssert hasMethodInfo(String methodInfo) { + return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + methodInfo); + } + + public LogAssert hasMessage(String message) { + return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + message); + } + + private LogAssert hasLog(int priority, String tag, String message) { + ShadowLog.LogItem item = items.get(index++); + assertThat(item.type).isEqualTo(priority); + assertThat(item.tag).isEqualTo(tag); + assertThat(item.msg).isEqualTo(message); + return this; + } + + public LogAssert skip() { + index++; + return this; + } + + public LogAssert defaultTag() { + tag = DEFAULT_TAG; + return this; + } + + public LogAssert hasTag(String tag) { + assertThat(tag).isEqualTo(this.tag); + return this; + } + + public void hasNoMoreMessages() { + assertThat(items).hasSize(index); + ShadowLog.getLogs().clear(); + } + + public LogAssert hasMessageWithDefaultSettings(String... messages) { + hasTopBorder(); + skip(); + hasMiddleBorder(); + skip(); + skip(); + hasMiddleBorder(); + + for (String message : messages) { + hasMessage(message); + } + + hasBottomBorder(); + hasNoMoreMessages(); + + return this; + } +} diff --git a/logger/src/test/java/com.orhanobut.logger/LoggerTest.java b/logger/src/test/java/com.orhanobut.logger/LoggerTest.java index db73cd1e..9b550f70 100644 --- a/logger/src/test/java/com.orhanobut.logger/LoggerTest.java +++ b/logger/src/test/java/com.orhanobut.logger/LoggerTest.java @@ -8,7 +8,6 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLog; -import org.robolectric.shadows.ShadowLog.LogItem; import java.util.Arrays; import java.util.HashMap; @@ -29,6 +28,8 @@ @Config(constants = BuildConfig.class, sdk = 21) public class LoggerTest { + private static final String DEFAULT_TAG = "PRETTYLOGGER"; + String threadName; @Before public void setup() { @@ -41,10 +42,22 @@ public class LoggerTest { Logger.resetSettings(); } - @Test public void log() { - Logger.log(ASSERT, "message %s", "1"); + @Test public void testLog() { + Logger.log(DEBUG, null, "message", null); + assertLog(DEBUG).hasMessageWithDefaultSettings("message"); - assertLog(ASSERT).hasMessageWithDefaultSettings("message 1"); + Logger.log(DEBUG, "tag", "message", null); + assertLog(DEFAULT_TAG + "-tag", DEBUG).hasMessageWithDefaultSettings("message"); + } + + @Ignore + @Test public void testLogThrowable() { + Throwable throwable = new Throwable("throwable"); + String stackString = "message : " + Helper.getStackTraceString(throwable); + String[] stackItems = stackString.split("\\n"); + Logger.log(DEBUG, null, "message", throwable); + + assertLog(DEBUG).hasMessageWithDefaultSettings(stackItems); } @Test public void debugLog() { @@ -361,95 +374,4 @@ private static LogAssert assertLog(String tag, int priority) { return new LogAssert(ShadowLog.getLogs(), tag, priority); } - private static final class LogAssert { - private static final String DEFAULT_TAG = "PRETTYLOGGER"; - - private static final char TOP_LEFT_CORNER = '╔'; - private static final char BOTTOM_LEFT_CORNER = '╚'; - private static final char MIDDLE_CORNER = '╟'; - private static final char HORIZONTAL_DOUBLE_LINE = '║'; - private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════"; - private static final String SINGLE_DIVIDER = "────────────────────────────────────────────"; - private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER; - private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER; - private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER; - - private final List items; - private final int priority; - - private String tag; - - private int index = 0; - - private LogAssert(List items, String tag, int priority) { - this.items = items; - this.tag = tag == null ? DEFAULT_TAG : tag; - this.priority = priority; - } - - public LogAssert hasTopBorder() { - return hasLog(priority, tag, TOP_BORDER); - } - - public LogAssert hasBottomBorder() { - return hasLog(priority, tag, BOTTOM_BORDER); - } - - public LogAssert hasMiddleBorder() { - return hasLog(priority, tag, MIDDLE_BORDER); - } - - public LogAssert hasThread(String threadName) { - return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + "Thread: " + threadName); - } - - public LogAssert hasMethodInfo(String methodInfo) { - return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + methodInfo); - } - - public LogAssert hasMessage(String message) { - return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + message); - } - - private LogAssert hasLog(int priority, String tag, String message) { - LogItem item = items.get(index++); - assertThat(item.type).isEqualTo(priority); - assertThat(item.tag).isEqualTo(tag); - assertThat(item.msg).isEqualTo(message); - return this; - } - - public LogAssert skip() { - index++; - return this; - } - - public LogAssert defaultTag() { - tag = DEFAULT_TAG; - return this; - } - - public void hasNoMoreMessages() { - assertThat(items).hasSize(index); - ShadowLog.getLogs().clear(); - } - - public LogAssert hasMessageWithDefaultSettings(String... messages) { - hasTopBorder(); - skip(); - hasMiddleBorder(); - skip(); - skip(); - hasMiddleBorder(); - - for (String message : messages) { - hasMessage(message); - } - - hasBottomBorder(); - hasNoMoreMessages(); - - return this; - } - } }