xref: /aosp_15_r20/external/okhttp/okhttp-logging-interceptor/README.md (revision ab625e417e7950f52094c019474d2a9e9f14a68c)
1*ab625e41SAndroid Build Coastguard WorkerLogging Interceptor
2*ab625e41SAndroid Build Coastguard Worker===================
3*ab625e41SAndroid Build Coastguard Worker
4*ab625e41SAndroid Build Coastguard WorkerAn [OkHttp interceptor][1] which logs HTTP request and response data.
5*ab625e41SAndroid Build Coastguard Worker
6*ab625e41SAndroid Build Coastguard Worker```java
7*ab625e41SAndroid Build Coastguard WorkerOkHttpClient client = new OkHttpClient();
8*ab625e41SAndroid Build Coastguard WorkerHttpLoggingInterceptor logging = new HttpLoggingInterceptor();
9*ab625e41SAndroid Build Coastguard Workerlogging.setLevel(Level.BASIC);
10*ab625e41SAndroid Build Coastguard Workerclient.interceptors().add(logging);
11*ab625e41SAndroid Build Coastguard Worker```
12*ab625e41SAndroid Build Coastguard Worker
13*ab625e41SAndroid Build Coastguard WorkerYou can change the log level at any time by calling `setLevel`.
14*ab625e41SAndroid Build Coastguard Worker
15*ab625e41SAndroid Build Coastguard WorkerTo log to a custom location, pass a `Logger` instance to the constructor.
16*ab625e41SAndroid Build Coastguard Worker```java
17*ab625e41SAndroid Build Coastguard WorkerHttpLoggingInterceptor logging = new HttpLoggingInterceptor(new Logger() {
18*ab625e41SAndroid Build Coastguard Worker  @Override public void log(String message) {
19*ab625e41SAndroid Build Coastguard Worker    Timber.tag("OkHttp").d(message);
20*ab625e41SAndroid Build Coastguard Worker  }
21*ab625e41SAndroid Build Coastguard Worker});
22*ab625e41SAndroid Build Coastguard Worker```
23*ab625e41SAndroid Build Coastguard Worker
24*ab625e41SAndroid Build Coastguard Worker**Warning**: The logs generated by this interceptor when using the `HEADERS` or `BODY` levels has
25*ab625e41SAndroid Build Coastguard Workerthe potential to leak sensitive information such as "Authorization" or "Cookie" headers and the
26*ab625e41SAndroid Build Coastguard Workercontents of request and response bodies. This data should only be logged in a controlled way or in
27*ab625e41SAndroid Build Coastguard Workera non-production environment.
28*ab625e41SAndroid Build Coastguard Worker
29*ab625e41SAndroid Build Coastguard Worker
30*ab625e41SAndroid Build Coastguard WorkerDownload
31*ab625e41SAndroid Build Coastguard Worker--------
32*ab625e41SAndroid Build Coastguard Worker
33*ab625e41SAndroid Build Coastguard WorkerGet via Maven:
34*ab625e41SAndroid Build Coastguard Worker```xml
35*ab625e41SAndroid Build Coastguard Worker<dependency>
36*ab625e41SAndroid Build Coastguard Worker  <groupId>com.squareup.okhttp</groupId>
37*ab625e41SAndroid Build Coastguard Worker  <artifactId>logging-interceptor</artifactId>
38*ab625e41SAndroid Build Coastguard Worker  <version>(insert latest version)</version>
39*ab625e41SAndroid Build Coastguard Worker</dependency>
40*ab625e41SAndroid Build Coastguard Worker```
41*ab625e41SAndroid Build Coastguard Worker
42*ab625e41SAndroid Build Coastguard Workeror via Gradle
43*ab625e41SAndroid Build Coastguard Worker```groovy
44*ab625e41SAndroid Build Coastguard Workercompile 'com.squareup.okhttp:logging-interceptor:(insert latest version)'
45*ab625e41SAndroid Build Coastguard Worker```
46*ab625e41SAndroid Build Coastguard Worker
47*ab625e41SAndroid Build Coastguard Worker
48*ab625e41SAndroid Build Coastguard Worker
49*ab625e41SAndroid Build Coastguard Worker [1]: https://github.com/square/okhttp/wiki/Interceptors
50