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 Sep 17, 2017
1 parent eb20ae8 commit 2d8bf99
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
15 changes: 1 addition & 14 deletions logger/src/main/java/com/orhanobut/logger/CsvFormatStrategy.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.orhanobut.logger;

import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -80,8 +75,6 @@ private String formatTag(String tag) {
}

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 @@ -118,13 +111,7 @@ public CsvFormatStrategy build() {
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
47 changes: 47 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;

Expand All @@ -16,6 +18,10 @@ public class DiskLogStrategy implements LogStrategy {

private final Handler handler;

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

public DiskLogStrategy(Handler handler) {
this.handler = handler;
}
Expand Down Expand Up @@ -100,4 +106,45 @@ private File getLogFile(String folderName, 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() {
}

public Builder directory(String directory) {
this.directory = directory;
return this;
}

public Builder handlerThread(HandlerThread ht) {
this.ht = ht;
return this;
}

public Builder handler(Handler handler) {
this.handler = handler;
return this;
}

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

0 comments on commit 2d8bf99

Please sign in to comment.