Skip to content

Commit

Permalink
Merge pull request #38 from flipkart-incubator/logging
Browse files Browse the repository at this point in the history
Enable/disable logging support
  • Loading branch information
thekirankumar authored Aug 10, 2016
2 parents a65a313 + 1a5a99a commit 8350838
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,6 +50,7 @@ private NetworkInterceptor(Builder builder) {
throw new IllegalStateException("NetworkInterpreter cannot be null");
}
mInterpreter = builder.mInterpreter;
Utils.setIsLoggingEnabled(builder.mIsLoggingEnabled);
}

/**
Expand Down Expand Up @@ -94,6 +96,7 @@ public static class TimeInfo {
*/
public static class Builder {
private boolean mEnabled = true;
private boolean mIsLoggingEnabled = false;
private NetworkInterpreter mInterpreter;

/**
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/com/flipkart/okhttpstats/toolbox/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 8350838

Please sign in to comment.