Skip to content

Commit

Permalink
Merge pull request orhanobut#83 from orhanobut/log
Browse files Browse the repository at this point in the history
Change Logger.log() signature to log(priority,"tag","message",throwable)
  • Loading branch information
orhanobut authored Jun 28, 2016
2 parents dc20ff8 + 143e1f8 commit 865f69a
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 129 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Logger provides :

### Dependency
```groovy
compile 'com.orhanobut:logger:1.14'
compile 'com.orhanobut:logger:1.15'
```

### Current Log system
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.14
VERSION_NAME=1.15
GROUP=com.orhanobut

POM_DESCRIPTION=Simple, Pretty and Advanced Logger
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion logger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ dependencies {
testCompile deps.mockito
}

apply from: rootProject.file('maven_push.gradle')
apply from: rootProject.file('gradle/maven_push.gradle')
4 changes: 2 additions & 2 deletions logger/src/main/java/com/orhanobut/logger/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
60 changes: 33 additions & 27 deletions logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -117,40 +117,31 @@ public LoggerPrinter() {
} else {
message = object.toString();
}
log(DEBUG, message);
log(DEBUG, null, message);
}

@Override public void e(String message, Object... args) {
e(null, message, args);
}

@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);
}

/**
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion logger/src/main/java/com/orhanobut/logger/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
104 changes: 104 additions & 0 deletions logger/src/test/java/com.orhanobut.logger/LogAssert.java
Original file line number Diff line number Diff line change
@@ -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<ShadowLog.LogItem> items;
private final int priority;

private String tag;

private int index = 0;

LogAssert(List<ShadowLog.LogItem> 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;
}
}
Loading

0 comments on commit 865f69a

Please sign in to comment.