Skip to content

Commit

Permalink
Allow to change disk log destination
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed Apr 29, 2018
1 parent 4a85b78 commit eb00862
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ Logger.addLogAdapter(new AndroidLogAdapter() {
Logger.addLogAdapter(new DiskLogAdapter());
```

Set custom logging destination
```java
FormatStrategy formatStrategy = DiskLogStrategy.newBuilder()
.directory("/sdcard/my/awesome/destination")
.build();

Logger.addLogAdapter(new DiskLogAdapter(formatStrategy));
```

Add custom tag to Csv format strategy
```java
FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ private CsvFormatStrategy(@NonNull Builder builder) {
}

public static final class Builder {
private static final int MAX_BYTES = 500 * 1024; // 500K averages to a 4000 lines per file

Date date;
SimpleDateFormat dateFormat;
LogStrategy logStrategy;
Expand Down Expand Up @@ -126,13 +124,7 @@ private Builder() {
dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS", Locale.UK);
}
if (logStrategy == null) {
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String folder = diskPath + File.separatorChar + "logger";

HandlerThread ht = new HandlerThread("AndroidFileLogger." + folder);
ht.start();
Handler handler = new DiskLogStrategy.WriteHandler(ht.getLooper(), folder, MAX_BYTES);
logStrategy = new DiskLogStrategy(handler);
logStrategy = DiskLogStrategy.newBuilder().build();
}
return new CsvFormatStrategy(this);
}
Expand Down
63 changes: 63 additions & 0 deletions logger/src/main/java/com/orhanobut/logger/DiskLogStrategy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.orhanobut.logger;

import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
Expand All @@ -22,6 +24,11 @@ public class DiskLogStrategy implements LogStrategy {

@NonNull private final Handler handler;

@NonNull
public static Builder newBuilder() {
return new Builder();
}

public DiskLogStrategy(@NonNull Handler handler) {
this.handler = checkNotNull(handler);
}
Expand Down Expand Up @@ -113,4 +120,60 @@ private File getLogFile(@NonNull String folderName, @NonNull String fileName) {
return newFile;
}
}

public static final class Builder {
private static final int MAX_BYTES = 500 * 1024; // 500K averages to a 4000 lines per file

String directory;
HandlerThread ht;
Handler handler;

private Builder() {
}

/**
* Set the log destination directory
* The default destination will be used if not supplied
*/
@NonNull public Builder directory(@NonNull String directory) {
this.directory = directory;
return this;
}

/**
* Set the {@link HandlerThread} which handles this strategy
* A default {@link HandlerThread} will be created if not supplied
*/
@NonNull public Builder handlerThread(@NonNull HandlerThread ht) {
this.ht = ht;
return this;
}

/**
* Set the {@link Handler}
* A default {@link Handler} will be created if not supplied
*/
@NonNull public Builder handler(@NonNull Handler handler) {
this.handler = handler;
return this;
}

/**
* Create the {@link DiskLogStrategy} given the configuration
*/
@NonNull public DiskLogStrategy build() {
if (directory == null) {
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
directory = diskPath + File.separatorChar + "logger";
}
if (ht == null) {
ht = new HandlerThread("AndroidFileLogger." + directory);
ht.start();
}
if (handler == null) {
handler = new DiskLogStrategy.WriteHandler(ht.getLooper(), directory, MAX_BYTES);
}
return new DiskLogStrategy(handler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class DiskLogStrategyTest {

@Test fun log() {
val handler = mock(Handler::class.java)
val logStrategy = DiskLogStrategy(handler)

val logStrategy = DiskLogStrategy.newBuilder().handler(handler).build()

logStrategy.log(DEBUG, "tag", "message")

Expand Down

0 comments on commit eb00862

Please sign in to comment.