From 26a8f77dd1da572c88cd41c0e00636bc22734852 Mon Sep 17 00:00:00 2001 From: "anirudh.r" Date: Thu, 21 Jul 2016 17:42:34 +0530 Subject: [PATCH 1/2] builder method to enable disable logging --- .../flipkart/okhttpstats/NetworkInterceptor.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/src/main/java/com/flipkart/okhttpstats/NetworkInterceptor.java b/library/src/main/java/com/flipkart/okhttpstats/NetworkInterceptor.java index a5792d2..98bd898 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/NetworkInterceptor.java +++ b/library/src/main/java/com/flipkart/okhttpstats/NetworkInterceptor.java @@ -24,6 +24,7 @@ package com.flipkart.okhttpstats; import com.flipkart.okhttpstats.interpreter.NetworkInterpreter; +import com.flipkart.okhttpstats.toolbox.Utils; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; @@ -49,6 +50,7 @@ private NetworkInterceptor(Builder builder) { throw new IllegalStateException("NetworkInterpreter cannot be null"); } mInterpreter = builder.mInterpreter; + Utils.setIsLoggingEnabled(builder.mIsLoggingEnabled); } /** @@ -94,6 +96,7 @@ public static class TimeInfo { */ public static class Builder { private boolean mEnabled = true; + private boolean mIsLoggingEnabled = false; private NetworkInterpreter mInterpreter; /** @@ -119,6 +122,17 @@ public Builder setNetworkInterpreter(NetworkInterpreter interpreter) { return this; } + /** + * To enable/disable logging. By default logging is disabled. + * + * @param isLoggingEnabled : boolean + * @return {@link Builder} + */ + public Builder setLoggingEnabled(boolean isLoggingEnabled) { + this.mIsLoggingEnabled = isLoggingEnabled; + return this; + } + public NetworkInterceptor build() { return new NetworkInterceptor(this); } From 3cd9c687cc7a77cd996f472b68ea258303eed454 Mon Sep 17 00:00:00 2001 From: "anirudh.r" Date: Thu, 21 Jul 2016 17:42:55 +0530 Subject: [PATCH 2/2] added if clause before logging --- .../okhttpstats/handler/PersistentStatsHandler.java | 9 +++++---- .../okhttpstats/interpreter/DefaultInterpreter.java | 4 ++-- .../java/com/flipkart/okhttpstats/toolbox/Utils.java | 10 ++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java b/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java index 171f6cb..2796a18 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java +++ b/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java @@ -34,6 +34,7 @@ import com.flipkart.okhttpstats.model.RequestStats; import com.flipkart.okhttpstats.toolbox.NetworkStat; import com.flipkart.okhttpstats.toolbox.PreferenceManager; +import com.flipkart.okhttpstats.toolbox.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -146,7 +147,7 @@ public float getAverageNetworkSpeed() { @Override public void onResponseReceived(final RequestStats requestStats) { - if (mLogger.isDebugEnabled()) { + if (Utils.isLoggingEnabled()) { mLogger.debug("Response Received : {}", requestStats); } @@ -178,7 +179,7 @@ private float calculateNewSpeed(float currentAvgSpeed) { @Override public void onHttpExchangeError(RequestStats requestStats, IOException e) { - if (mLogger.isDebugEnabled()) { + if (Utils.isLoggingEnabled()) { mLogger.debug("Response Received With Http Exchange Error : {}", requestStats); } @@ -191,7 +192,7 @@ public void onHttpExchangeError(RequestStats requestStats, IOException e) { @Override public void onResponseInputStreamError(RequestStats requestStats, Exception e) { - if (mLogger.isDebugEnabled()) { + if (Utils.isLoggingEnabled()) { mLogger.debug("Response Received With InputStream Error : {}", requestStats); } @@ -208,7 +209,7 @@ public void onResponseInputStreamError(RequestStats requestStats, Exception e) { * @param currentAvgSpeed : float */ private void saveToSharedPreference(float currentAvgSpeed) { - if (mLogger.isDebugEnabled()) { + if (Utils.isLoggingEnabled()) { mLogger.debug("avg speed", "saveToSharedPreference: " + mNetworkStat.getCurrentAvgSpeed()); } String networkKey = getNetworkKey(getActiveNetworkInfo()); diff --git a/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java b/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java index df47bc7..7cbfa45 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java +++ b/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java @@ -71,7 +71,7 @@ public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeIn try { responseStream = body.byteStream(); } catch (Exception e) { - if (logger.isDebugEnabled()) { + if (Utils.isLoggingEnabled()) { logger.debug("Error received while reading input stream {}", e.getMessage()); } @@ -101,7 +101,7 @@ public void onEOF(long bytesRead) { @Override public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, IOException e) { - if (logger.isDebugEnabled()) { + if (Utils.isLoggingEnabled()) { logger.debug("Error received while proceeding response {}", e.getMessage()); } final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME)); diff --git a/library/src/main/java/com/flipkart/okhttpstats/toolbox/Utils.java b/library/src/main/java/com/flipkart/okhttpstats/toolbox/Utils.java index e4ab4b8..3ebd240 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/toolbox/Utils.java +++ b/library/src/main/java/com/flipkart/okhttpstats/toolbox/Utils.java @@ -29,6 +29,16 @@ public class Utils { + private static boolean isLoggingEnabled = false; + + public static boolean isLoggingEnabled() { + return isLoggingEnabled; + } + + public static void setIsLoggingEnabled(boolean isLoggingEnabled) { + Utils.isLoggingEnabled = isLoggingEnabled; + } + public static long contentLength(Request request) { return contentLength(request.headers()); }