Skip to content

Commit

Permalink
Add Logger.log() and support all collections
Browse files Browse the repository at this point in the history
  • Loading branch information
orhanobut committed Jun 25, 2016
1 parent 40b5740 commit 30f7322
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

### 1.14
- Logger.log(int priority, String tag, Object... args) added.
- Logger.d(Object object) added. Array, Map, Set and List are supported now. If the object type is none of them
Object.toString() will be used anyway.

### 1.13

- LogTool is renamed to LogAdapter
Expand Down
15 changes: 14 additions & 1 deletion 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.13'
compile 'com.orhanobut:logger:1.14'
```

### Current Log system
Expand Down Expand Up @@ -45,6 +45,19 @@ Logger.json(JSON_CONTENT);
Logger.xml(XML_CONTENT);
```

#### String format arguments are supported
```java
Logger.d("hello %s", "world");
```

#### Array, Map, Set and List are supported
```java
Logger.d(list);
Logger.d(map);
Logger.d(set);
Logger.d(new String[]);
```

### Change TAG
All logs
```java
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.13
VERSION_NAME=1.14
GROUP=com.orhanobut

POM_DESCRIPTION=Simple, Pretty and Advanced Logger
Expand Down
19 changes: 17 additions & 2 deletions logger/src/main/java/com/orhanobut/logger/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
* But more pretty, simple and powerful
*/
public final class Logger {
public static final int DEBUG = 3;
public static final int ERROR = 6;
public static final int ASSERT = 7;
public static final int INFO = 4;
public static final int VERBOSE = 2;
public static final int WARN = 5;

private static final String DEFAULT_TAG = "PRETTYLOGGER";

private static Printer printer = new LoggerPrinter();
Expand Down Expand Up @@ -32,8 +39,8 @@ public static Settings init(String tag) {
return printer.init(tag);
}

public static void clear() {
printer.clear();
public static void resetSettings() {
printer.resetSettings();
}

public static Printer t(String tag) {
Expand All @@ -48,10 +55,18 @@ 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 d(String message, Object... args) {
printer.d(message, args);
}

public static void d(Object object) {
printer.d(object);
}

public static void e(String message, Object... args) {
printer.e(null, message, args);
}
Expand Down
34 changes: 23 additions & 11 deletions logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
Expand Down Expand Up @@ -109,6 +110,16 @@ public LoggerPrinter() {
log(DEBUG, message, args);
}

@Override public void d(Object object) {
String message;
if (object.getClass().isArray()) {
message = Arrays.deepToString((Object[]) object);
} else {
message = object.toString();
}
log(DEBUG, message);
}

@Override public void e(String message, Object... args) {
e(null, message, args);
}
Expand Down Expand Up @@ -191,18 +202,18 @@ public LoggerPrinter() {
transformer.transform(xmlInput, xmlOutput);
d(xmlOutput.getWriter().toString().replaceFirst(">", ">\n"));
} catch (TransformerException e) {
e(e.getCause().getMessage() + "\n" + xml);
e("Invalid xml");
}
}

@Override public void clear() {
@Override public void resetSettings() {
settings.reset();
}

/**
* This method is synchronized in order to avoid messy of logs' order.
*/
private synchronized void log(int logType, String msg, Object... args) {
@Override public synchronized void log(int priority, String msg, Object... args) {
if (settings.getLogLevel() == LogLevel.NONE) {
return;
}
Expand All @@ -214,35 +225,36 @@ private synchronized void log(int logType, String msg, Object... args) {
message = "Empty/NULL log message";
}

logTopBorder(logType, tag);
logHeaderContent(logType, tag, methodCount);
logTopBorder(priority, tag);
logHeaderContent(priority, tag, methodCount);

//get bytes of message with system's default charset (which is UTF-8 for Android)
byte[] bytes = message.getBytes();
int length = bytes.length;
if (length <= CHUNK_SIZE) {
if (methodCount > 0) {
logDivider(logType, tag);
logDivider(priority, tag);
}
logContent(logType, tag, message);
logBottomBorder(logType, tag);
logContent(priority, tag, message);
logBottomBorder(priority, tag);
return;
}
if (methodCount > 0) {
logDivider(logType, tag);
logDivider(priority, tag);
}
for (int i = 0; i < length; i += CHUNK_SIZE) {
int count = Math.min(length - i, CHUNK_SIZE);
//create a new String with system's default charset (which is UTF-8 for Android)
logContent(logType, tag, new String(bytes, i, count));
logContent(priority, tag, new String(bytes, i, count));
}
logBottomBorder(logType, tag);
logBottomBorder(priority, tag);
}

private void logTopBorder(int logType, String tag) {
logChunk(logType, tag, TOP_BORDER);
}

@SuppressWarnings("StringBufferReplaceableByString")
private void logHeaderContent(int logType, String tag, int methodCount) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
if (settings.isShowThreadInfo()) {
Expand Down
7 changes: 6 additions & 1 deletion logger/src/main/java/com/orhanobut/logger/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface Printer {

void d(String message, Object... args);

void d(Object object);

void e(String message, Object... args);

void e(Throwable throwable, String message, Object... args);
Expand All @@ -26,5 +28,8 @@ public interface Printer {

void xml(String xml);

void clear();
void log(int priority, String message, Object... args);

void resetSettings();

}
71 changes: 63 additions & 8 deletions logger/src/test/java/com.orhanobut.logger/LoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@
import org.robolectric.shadows.ShadowLog;
import org.robolectric.shadows.ShadowLog.LogItem;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.truth.Truth.assertThat;
import static com.orhanobut.logger.Logger.ASSERT;
import static com.orhanobut.logger.Logger.DEBUG;
import static com.orhanobut.logger.Logger.ERROR;
import static com.orhanobut.logger.Logger.INFO;
import static com.orhanobut.logger.Logger.VERBOSE;
import static com.orhanobut.logger.Logger.WARN;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class LoggerTest {

public static final int DEBUG = 3;
public static final int ERROR = 6;
public static final int ASSERT = 7;
public static final int INFO = 4;
public static final int VERBOSE = 2;
public static final int WARN = 5;

String threadName;

@Before public void setup() {
Expand All @@ -34,7 +38,13 @@ public class LoggerTest {
}

@After public void tearDown() {
Logger.clear();
Logger.resetSettings();
}

@Test public void log() {
Logger.log(ASSERT, "message %s", "1");

assertLog(ASSERT).hasMessageWithDefaultSettings("message 1");
}

@Test public void debugLog() {
Expand Down Expand Up @@ -102,6 +112,45 @@ public class LoggerTest {
assertLog(ASSERT).hasMessageWithDefaultSettings("message");
}

@Test public void logArray() {
double[][] doubles = {{1.2, 1.6, 1.7, 30, 33},
{1.2, 1.6, 1.7, 30, 33},
{1.2, 1.6, 1.7, 30, 33},
{1.2, 1.6, 1.7, 30, 33}};

Logger.d(doubles);

String message = Arrays.deepToString(doubles);
assertLog(DEBUG).hasMessageWithDefaultSettings(message);
}

@Test public void logList() {
List<String> list = Arrays.asList("foo", "bar");
Logger.d(list);

assertLog(DEBUG).hasMessageWithDefaultSettings(list.toString());
}

@Test public void logMap() {
Map<String, String> map = new HashMap<>();
map.put("key", "value");
map.put("key2", "value2");

Logger.d(map);

assertLog(DEBUG).hasMessageWithDefaultSettings(map.toString());
}

@Test public void logSet() {
Set<String> set = new HashSet<>();
set.add("key");
set.add("key1");

Logger.d(set);

assertLog(DEBUG).hasMessageWithDefaultSettings(set.toString());
}

@Test public void jsonLObjectLog() {
String[] messages = new String[]{"{", " \"key\": 3", "}"};

Expand Down Expand Up @@ -151,6 +200,12 @@ public class LoggerTest {
assertLog(DEBUG).hasMessageWithDefaultSettings(messages);
}

@Test public void invalidXmlLog() {
Logger.xml("xml>Test</xml>");

assertLog(ERROR).hasMessageWithDefaultSettings("Invalid xml");
}

@Test public void xmlLogNullOrEmpty() {
Logger.xml(null);
assertLog(DEBUG).hasMessageWithDefaultSettings("Empty/Null xml content");
Expand Down

0 comments on commit 30f7322

Please sign in to comment.