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

Add custom folder for csv #220

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions logger/src/main/java/com/orhanobut/logger/CsvFormatStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public class CsvFormatStrategy implements FormatStrategy {
private static final String NEW_LINE_REPLACEMENT = " <br> ";
private static final String SEPARATOR = ",";

@NonNull private final Date date;
@NonNull private final SimpleDateFormat dateFormat;
@NonNull private final LogStrategy logStrategy;
@Nullable private final String tag;
@NonNull
private final Date date;
@NonNull
private final SimpleDateFormat dateFormat;
@NonNull
private final LogStrategy logStrategy;
@Nullable
private final String tag;

private CsvFormatStrategy(@NonNull Builder builder) {
checkNotNull(builder);
Expand All @@ -38,11 +42,13 @@ private CsvFormatStrategy(@NonNull Builder builder) {
tag = builder.tag;
}

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

@Override public void log(int priority, @Nullable String onceOnlyTag, @NonNull String message) {
@Override
public void log(int priority, @Nullable String onceOnlyTag, @NonNull String message) {
checkNotNull(message);

String tag = formatTag(onceOnlyTag);
Expand Down Expand Up @@ -80,7 +86,8 @@ private CsvFormatStrategy(@NonNull Builder builder) {
logStrategy.log(priority, tag, builder.toString());
}

@Nullable private String formatTag(@Nullable String tag) {
@Nullable
private String formatTag(@Nullable String tag) {
if (!Utils.isEmpty(tag) && !Utils.equals(this.tag, tag)) {
return this.tag + "-" + tag;
}
Expand All @@ -94,40 +101,58 @@ public static final class Builder {
SimpleDateFormat dateFormat;
LogStrategy logStrategy;
String tag = "PRETTY_LOGGER";
File customfolder;

private Builder() {
}

@NonNull public Builder date(@Nullable Date val) {
@NonNull
public Builder date(@Nullable Date val) {
date = val;
return this;
}

@NonNull public Builder dateFormat(@Nullable SimpleDateFormat val) {
@NonNull
public Builder dateFormat(@Nullable SimpleDateFormat val) {
dateFormat = val;
return this;
}

@NonNull public Builder logStrategy(@Nullable LogStrategy val) {
@NonNull
public Builder logStrategy(@Nullable LogStrategy val) {
logStrategy = val;
return this;
}

@NonNull public Builder tag(@Nullable String tag) {
@NonNull
public Builder tag(@Nullable String tag) {
this.tag = tag;
return this;
}

@NonNull public CsvFormatStrategy build() {
@NonNull
public Builder folder(@Nullable File folder) {
this.customfolder = folder;
return this;
}

@NonNull
public CsvFormatStrategy build() {
if (date == null) {
date = new Date();
}
if (dateFormat == null) {
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";
String folder;
if (customfolder != null) {
folder = customfolder.toString();
} else {
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
folder = diskPath + File.separatorChar + "logger";
}


HandlerThread ht = new HandlerThread("AndroidFileLogger." + folder);
ht.start();
Expand Down
115 changes: 62 additions & 53 deletions sample/src/main/java/com/orhanobut/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,91 @@
import android.util.Log;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.CsvFormatStrategy;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d("Tag", "I'm a log which you don't see easily, hehe");
Log.d("json content", "{ \"key\": 3, \n \"value\": something}");
Log.d("error", "There is a crash somewhere or any warning");

Logger.addLogAdapter(new AndroidLogAdapter());
Logger.d("message");

Logger.clearLogAdapters();


FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File folder = new File(getExternalFilesDir(null), "test"); //Creating log in sdcard/Android/data/com.orhanobut.sample/test folder
CsvFormatStrategy csvFormatStrategy = CsvFormatStrategy.newBuilder()
.folder(folder) // (Optional) If you want to save different folder
.tag("PRETTY_LOGGER")
.build();
Logger.addLogAdapter(new DiskLogAdapter(csvFormatStrategy));
Log.d("Tag", "I'm a log which you don't see easily, hehe");
Log.d("json content", "{ \"key\": 3, \n \"value\": something}");
Log.d("error", "There is a crash somewhere or any warning");

Logger.addLogAdapter(new AndroidLogAdapter());
Logger.d("message");

Logger.clearLogAdapters();


FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
// .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
.tag("My custom tag") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
.build();
.tag("My custom tag") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
.build();

Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Logger.addLogAdapter(new AndroidLogAdapter() {
@Override
public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
}
});

Logger.addLogAdapter(new AndroidLogAdapter() {
@Override public boolean isLoggable(int priority, String tag) {
return BuildConfig.DEBUG;
}
});
Logger.addLogAdapter(new DiskLogAdapter());

Logger.addLogAdapter(new DiskLogAdapter());

Logger.w("no thread info and only 1 method");

Logger.w("no thread info and only 1 method");
Logger.clearLogAdapters();
formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)
.methodCount(0)
.build();

Logger.clearLogAdapters();
formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)
.methodCount(0)
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Logger.i("no thread info and method info");

Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Logger.i("no thread info and method info");
Logger.t("tag").e("Custom tag for only one use");

Logger.t("tag").e("Custom tag for only one use");
Logger.json("{ \"key\": 3, \"value\": something}");

Logger.json("{ \"key\": 3, \"value\": something}");
Logger.d(Arrays.asList("foo", "bar"));

Logger.d(Arrays.asList("foo", "bar"));
Map<String, String> map = new HashMap<>();
map.put("key", "value");
map.put("key1", "value2");

Map<String, String> map = new HashMap<>();
map.put("key", "value");
map.put("key1", "value2");
Logger.d(map);

Logger.d(map);
Logger.clearLogAdapters();
formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)
.methodCount(0)
.tag("MyTag")
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

Logger.clearLogAdapters();
formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)
.methodCount(0)
.tag("MyTag")
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

Logger.w("my log message with my tag");
}
}
Logger.w("my log message with my tag");
}
}