Puree


Source link: https://github.com/cookpad/puree-android

Puree

Description

Puree is a log collector which provides the following features:

  • Filtering: Enable to interrupt process before sending log. You can add common params to logs, or the sampling of logs.
  • Buffering: Store logs to buffers and send them later.
  • Batching: Send logs in a single request with PureeBufferedOutput.
  • Retrying: Retry to send logs after backoff time if sending logs fails.

Puree helps you unify your logging infrastructure.

Installation

This is published on jcenter and you can use Puree as:

// build.gradle buildscript {

  repositories {

jcenter()
  
}

  ... 
}
  // app/build.gradle dependencies {

  compile 'com.cookpad.puree:puree:4.1.1' 
}

Usage

Initialize

Configure Puree with PureeConfiguration in Application#onCreate(), which registers pairs of what and where.

public class MyApplication extends Application {

  @Override
  public void onCreate() {

Puree.initialize(buildConfiguration(this));

  
}

public static PureeConfiguration buildConfiguration(Context context) {

PureeFilter addEventTimeFilter = new AddEventTimeFilter();

return new PureeConfiguration.Builder(context)

  .executor(Executors.newScheduledThreadPool(1)) // optional

  .register(ClickLog.class, new OutLogcat())

  .register(ClickLog.class, new OutBufferedLogcat().withFilters(addEventTimeFilter))

  .build();

  
}
 
}

See also: demo/PureeConfigurator.java

Definition of PureeLog objects

A log class is required to implement PureeLog, which is a marker interface just like as Serializable, to serialize logs with Gson.

public class ClickLog implements PureeLog {

  @SerializedName("page")
  private String page;
  @SerializedName("label")
  private String label;

public ClickLog(String page, String label) {

this.page = page;

this.label = label;
  
}
 
}

You can use Puree.send() to send these logs to registered output plugins:

Puree.send(new ClickLog("MainActivity", "Hello"));
 // => {
"page":"MainActivity","label":"Hello"
}

Definition of PureeOutput plugins

There are two types of output plugins: non-buffered and buffered.

  • PureeOutput: Non-buffered output plugins write logs immediately.
  • PureeBufferedOutput: Buffered output plugins enqueue logs to a local storage and then flush them in background tasks.

If you don't need buffering, you can use PureeOutput.

public class OutLogcat extends PureeOutput {

  private static final String TYPE = "out_logcat";

@Override
  public String type() {

return TYPE;
  
}

@Override
  public OutputConfiguration configure(OutputConfiguration conf) {

return conf;
  
}

@Override
  public void emit(JsonObject jsonLog) {

Log.d(TYPE, jsonLog.toString());

  
}
 
}

If you need buffering, you can use PureeBufferedOutput.

public class OutFakeApi extends PureeBufferedOutput {

  private static final String TYPE = "out_fake_api";

private static final FakeApiClient CLIENT = new FakeApiClient();

@Override
  public String type() {

return TYPE;
  
}

@Override
  public OutputConfiguration configure(OutputConfiguration conf) {

// you can change settings of this plugin

// set interval of sending logs. defaults to 2 * 60 * 1000 (2 minutes).

conf.setFlushIntervalMillis(1000);

// set num of logs per request. defaults to 100.

conf.setLogsPerRequest(10);

// set retry count. if fail to send logs, logs will be sending at next time. defaults to 5.

conf.setMaxRetryCount(3);

return conf;
  
}

@Override
  public void emit(JsonArray jsonArray, final AsyncResult result) {

// you have to call result.success or result.fail()

// to notify whether if puree can clear logs from buffer

CLIENT.sendLog(jsonArray, new FakeApiClient.Callback() {

 @Override

 public void success() {

  result.success();

 
}

  @Override

 public void fail() {

  result.fail();

 
}

}
);

  
}
 
}

Definition of Filters

If you need to add common params to each logs, you can use PureeFilter:

public class AddEventTimeFilter implements PureeFilter {

  public JsonObject apply(JsonObject jsonLog) {

jsonLog.addProperty("event_time", System.currentTimeMillis());

return jsonLog;
  
}
 
}

You can make PureeFilter#apply() to return null to skip sending logs:

public class SamplingFilter implements PureeFilter {

  private final float samplingRate;

public SamplingFilter(float samplingRate) {

this.samplingRate = samplingRate;
  
}

@Override
  public JsonObject apply(JsonObject jsonLog) {

return (samplingRate < Math.random() ? null : jsonLog);

  
}
 
}

Then register filters to output plugins on initializing Puree.

new PureeConfiguration.Builder(context)

.register(ClickLog.class, new OutLogcat())

.register(ClickLog.class, new OutFakeApi().withFilters(addEventTimeFilter, samplingFilter)

.build();

Testing

If you want to mock or ignore Puree.send() and Puree.flush(), you can use Puree.setPureeLogger() to replace the internal logger. See PureeTest.java for details.

Release Engineering

Set bintrayUser and bintrayKey in ~/.gradle/gradle.properties

bintrayUser=BINTRAY_USER bintrayKey=BINTRAY_API_KEY

and run the following tasks:

./gradlew clean connectedCheck bintrayUpload --info # dry-run ./gradlew bintrayUpload -PdryRun=false 

See Also

Copyright

Copyright (c) 2014 Cookpad Inc. https://github.com/cookpad

See LICENSE.txt for the license.

Resources

A small tool to convert your app's png&jpg image files into WebP when possible.

An Android List View implementation with support for drawable cells and many other swipe related features.

TableFixHeaders is an Android widget to display tables with headers.

Wrappers for Android adapters to simply its api at a minimum.

Simplify usage of RecyclerView with various types of adapters.

A sample of how to mimic the way that the contacts app handles a Fast-Scroller for a RecyclerView.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes