JJD
JJD

Reputation: 51824

How to configure debug and release log level for Retrofit?

I use Retrofit in my Android project. While developing I enable full log output for the RestAdapter as follows:

new RestAdapter.Builder()
    .setLogLevel(RestAdapter.LogLevel.FULL);

When I bake a release the log level is set to RestAdapter.LogLevel.NONE for obvious reasons.

I would like to automatically switch this setting based on the build/folder architecture:

.
├── app
│   └── src
│       ├── debug
│       ├── main
│       └── release

How can I move RestAdapter.LogLevel.FULL into app/src/debug and RestAdapter.LogLevel.NONE into app/src/release ?

Upvotes: 3

Views: 3276

Answers (2)

xorgate
xorgate

Reputation: 2264

A better way to do this is via the build.gradle file as such:

buildTypes {
    debug {
        buildConfigField 'retrofit.RestAdapter.LogLevel', 'RETROFIT_LOG_LEVEL', 'retrofit.RestAdapter.LogLevel.FULL'
    }

    release {
        buildConfigField 'retrofit.RestAdapter.LogLevel', 'RETROFIT_LOG_LEVEL', 'retrofit.RestAdapter.LogLevel.NONE'
    }
}

Then in code do this:

mRestAdapter = new RestAdapter.Builder()
    .setEndpoint("http://something.com")
    .setLogLevel(BuildConfig.RETROFIT_LOG_LEVEL)
    .build();

This way there is no check at runtime, only at compiletime

Upvotes: 12

Bryan Herbst
Bryan Herbst

Reputation: 67189

The easiest way is to use the BuildConfig.DEBUG flag, which is automatically set to true for debug builds and false for release builds.

I do this like so:

RestAdapter adapter = new RestAdapter.Builder()
    .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)

Upvotes: 5

Related Questions