Skip to content

Commit 8350838

Browse files
Merge pull request #38 from flipkart-incubator/logging
Enable/disable logging support
2 parents a65a313 + 1a5a99a commit 8350838

4 files changed

Lines changed: 31 additions & 6 deletions

File tree

library/src/main/java/com/flipkart/okhttpstats/NetworkInterceptor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.flipkart.okhttpstats;
2525

2626
import com.flipkart.okhttpstats.interpreter.NetworkInterpreter;
27+
import com.flipkart.okhttpstats.toolbox.Utils;
2728

2829
import java.io.IOException;
2930
import java.util.concurrent.atomic.AtomicInteger;
@@ -49,6 +50,7 @@ private NetworkInterceptor(Builder builder) {
4950
throw new IllegalStateException("NetworkInterpreter cannot be null");
5051
}
5152
mInterpreter = builder.mInterpreter;
53+
Utils.setIsLoggingEnabled(builder.mIsLoggingEnabled);
5254
}
5355

5456
/**
@@ -94,6 +96,7 @@ public static class TimeInfo {
9496
*/
9597
public static class Builder {
9698
private boolean mEnabled = true;
99+
private boolean mIsLoggingEnabled = false;
97100
private NetworkInterpreter mInterpreter;
98101

99102
/**
@@ -119,6 +122,17 @@ public Builder setNetworkInterpreter(NetworkInterpreter interpreter) {
119122
return this;
120123
}
121124

125+
/**
126+
* To enable/disable logging. By default logging is disabled.
127+
*
128+
* @param isLoggingEnabled : boolean
129+
* @return {@link Builder}
130+
*/
131+
public Builder setLoggingEnabled(boolean isLoggingEnabled) {
132+
this.mIsLoggingEnabled = isLoggingEnabled;
133+
return this;
134+
}
135+
122136
public NetworkInterceptor build() {
123137
return new NetworkInterceptor(this);
124138
}

library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.flipkart.okhttpstats.model.RequestStats;
3535
import com.flipkart.okhttpstats.toolbox.NetworkStat;
3636
import com.flipkart.okhttpstats.toolbox.PreferenceManager;
37+
import com.flipkart.okhttpstats.toolbox.Utils;
3738

3839
import org.slf4j.Logger;
3940
import org.slf4j.LoggerFactory;
@@ -146,7 +147,7 @@ public float getAverageNetworkSpeed() {
146147

147148
@Override
148149
public void onResponseReceived(final RequestStats requestStats) {
149-
if (mLogger.isDebugEnabled()) {
150+
if (Utils.isLoggingEnabled()) {
150151
mLogger.debug("Response Received : {}", requestStats);
151152
}
152153

@@ -178,7 +179,7 @@ private float calculateNewSpeed(float currentAvgSpeed) {
178179

179180
@Override
180181
public void onHttpExchangeError(RequestStats requestStats, IOException e) {
181-
if (mLogger.isDebugEnabled()) {
182+
if (Utils.isLoggingEnabled()) {
182183
mLogger.debug("Response Received With Http Exchange Error : {}", requestStats);
183184
}
184185

@@ -191,7 +192,7 @@ public void onHttpExchangeError(RequestStats requestStats, IOException e) {
191192

192193
@Override
193194
public void onResponseInputStreamError(RequestStats requestStats, Exception e) {
194-
if (mLogger.isDebugEnabled()) {
195+
if (Utils.isLoggingEnabled()) {
195196
mLogger.debug("Response Received With InputStream Error : {}", requestStats);
196197
}
197198

@@ -208,7 +209,7 @@ public void onResponseInputStreamError(RequestStats requestStats, Exception e) {
208209
* @param currentAvgSpeed : float
209210
*/
210211
private void saveToSharedPreference(float currentAvgSpeed) {
211-
if (mLogger.isDebugEnabled()) {
212+
if (Utils.isLoggingEnabled()) {
212213
mLogger.debug("avg speed", "saveToSharedPreference: " + mNetworkStat.getCurrentAvgSpeed());
213214
}
214215
String networkKey = getNetworkKey(getActiveNetworkInfo());

library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeIn
7171
try {
7272
responseStream = body.byteStream();
7373
} catch (Exception e) {
74-
if (logger.isDebugEnabled()) {
74+
if (Utils.isLoggingEnabled()) {
7575
logger.debug("Error received while reading input stream {}", e.getMessage());
7676
}
7777

@@ -101,7 +101,7 @@ public void onEOF(long bytesRead) {
101101

102102
@Override
103103
public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, IOException e) {
104-
if (logger.isDebugEnabled()) {
104+
if (Utils.isLoggingEnabled()) {
105105
logger.debug("Error received while proceeding response {}", e.getMessage());
106106
}
107107
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME));

library/src/main/java/com/flipkart/okhttpstats/toolbox/Utils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929

3030
public class Utils {
3131

32+
private static boolean isLoggingEnabled = false;
33+
34+
public static boolean isLoggingEnabled() {
35+
return isLoggingEnabled;
36+
}
37+
38+
public static void setIsLoggingEnabled(boolean isLoggingEnabled) {
39+
Utils.isLoggingEnabled = isLoggingEnabled;
40+
}
41+
3242
public static long contentLength(Request request) {
3343
return contentLength(request.headers());
3444
}

0 commit comments

Comments
 (0)