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

Feature/structured logging support #303

Open
wants to merge 4 commits into
base: trunk
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
20 changes: 20 additions & 0 deletions timber-sample/src/main/java/com/example/timber/ExampleApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.app.Application;
import android.support.annotation.NonNull;
import android.util.Log;

import java.util.Map;

import timber.log.Timber;

import static timber.log.Timber.DebugTree;
Expand All @@ -15,6 +18,7 @@ public class ExampleApp extends Application {
Timber.plant(new DebugTree());
} else {
Timber.plant(new CrashReportingTree());
Timber.plant(new StructuredLoggingTree());
}
}

Expand All @@ -36,4 +40,20 @@ private static class CrashReportingTree extends Timber.Tree {
}
}
}

/** A tree which logs important events in a structured format. */
private static class StructuredLoggingTree extends Timber.Tree {
@Override protected void log(int priority, String tag, @NonNull String message, Throwable t) {
log(priority, tag, message, t, null);
}

@Override protected void log(int priority, String tag, @NonNull String message, Throwable t,
Map<String, Object> metadata) {
if (priority == Log.VERBOSE || priority == Log.DEBUG) {
return;
}

FakeCrashLibrary.log(priority, message, metadata);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.example.timber;

import java.util.Map;

/** Not a real crash reporting library! */
public final class FakeCrashLibrary {
public static void log(int priority, String tag, String message) {
// TODO add log entry to circular buffer.
}

public static void log(int priority, String message, Map<String, Object> metadata) {
// TODO add log entry with metadata
}

public static void logWarning(Throwable t) {
// TODO report non-fatal warning.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.widget.Button;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;
import com.example.timber.R;
Expand All @@ -24,6 +27,10 @@ public class DemoActivity extends Activity {
@OnClick({ R.id.hello, R.id.hey, R.id.hi })
public void greetingClicked(Button button) {
Timber.i("A button with ID %s was clicked to say '%s'.", button.getId(), button.getText());
Map<String, Object> eventMetadata = new HashMap<>();
eventMetadata.put("event-type", "button-pressed");
eventMetadata.put("button-id", String.valueOf(button.getId()));
Timber.v(eventMetadata, "This is an example of a structured log");
Toast.makeText(this, "Check logcat for a greeting!", LENGTH_SHORT).show();
}
}
Loading