Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change disk log destination #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
Logger.addLogAdapter(new DiskLogAdapter(formatStrategy));
```

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

FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
.logStrategy(logStrategy)
.build();

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

### How it works
<img src='https://github.com/orhanobut/logger/blob/master/art/how_it_works.png'/>

Expand Down
14 changes: 1 addition & 13 deletions logger/src/main/java/com/orhanobut/logger/CsvFormatStrategy.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.orhanobut.logger;

import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -88,8 +84,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 +120,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