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

- folder name option added for DiskLogAdapter. #224

Open
wants to merge 5 commits 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
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
32 changes: 0 additions & 32 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
buildscript {
ext.kotlinVersion = '1.2.31'
ext.kotlinVersion = '1.3.31'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.android.tools.build:gradle:3.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
Expand Down Expand Up @@ -59,4 +59,4 @@ ext.deps = [
json : "org.json:json:20160810",
supportAnnotations: "com.android.support:support-annotations:27.1.0",
kotlin : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
]
]
28 changes: 25 additions & 3 deletions logger/src/main/java/com/orhanobut/logger/CsvFormatStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.annotation.Nullable;

import java.io.File;
import java.nio.file.InvalidPathException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -38,6 +39,10 @@ private CsvFormatStrategy(@NonNull Builder builder) {
tag = builder.tag;
}

@NonNull public static Builder newBuilder(String foldername, int maxsize) {
return new Builder(foldername, maxsize);
}

@NonNull public static Builder newBuilder() {
return new Builder();
}
Expand Down Expand Up @@ -88,14 +93,31 @@ 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
private static int MAX_BYTES = 500 * 1024; // default size 500K averages to a 4000 lines per file

Date date;
SimpleDateFormat dateFormat;
LogStrategy logStrategy;
String tag = "PRETTY_LOGGER";
String folderName = "logger"; //default folder name

// Default constructor, will use logger as folder name and 500K as default size
private Builder(){

}

private Builder() {
private Builder(String foldername, int maxsize) {
if (foldername == null || foldername.length() == 0) {
//folder name seems to be invalid, throw an exception (just basic check, there could be invalid characters for folder name those will throw exception at time of writing
throw new IllegalArgumentException("Folder name you have provided seems to be invalid. " + foldername);
}
if(maxsize <= 0)
{
throw new IllegalArgumentException("File max size must be greater than 0, you have provided " + maxsize);
}

folderName = foldername;
MAX_BYTES = maxsize;
}

@NonNull public Builder date(@Nullable Date val) {
Expand Down Expand Up @@ -127,7 +149,7 @@ private Builder() {
}
if (logStrategy == null) {
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String folder = diskPath + File.separatorChar + "logger";
String folder = diskPath + File.separatorChar + folderName;

HandlerThread ht = new HandlerThread("AndroidFileLogger." + folder);
ht.start();
Expand Down
6 changes: 6 additions & 0 deletions logger/src/main/java/com/orhanobut/logger/DiskLogAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ public class DiskLogAdapter implements LogAdapter {

@NonNull private final FormatStrategy formatStrategy;


public DiskLogAdapter() {
formatStrategy = CsvFormatStrategy.newBuilder().build();
}

public DiskLogAdapter(String foldername, int maxfilesize)
{
formatStrategy = CsvFormatStrategy.newBuilder(foldername, maxfilesize).build();
}

public DiskLogAdapter(@NonNull FormatStrategy formatStrategy) {
this.formatStrategy = checkNotNull(formatStrategy);
}
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orhanobut.sample">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@mipmap/ic_launcher"
android:label="Sample">
Expand Down
5 changes: 5 additions & 0 deletions sample/src/main/java/com/orhanobut/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ protected void onCreate(Bundle savedInstanceState) {
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

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

Logger.clearLogAdapters();
Logger.addLogAdapter(new DiskLogAdapter("customfoldername", 2097152)); //create a DiskLogAdapter with folder name customfoldername and 2MB max file size

Logger.i("Info log written in custom folder");
}
}