Reputation: 379
I use Retrofit in a java application to access api-rest.
I'm using simple code:
RestAdapter.Builder().setEndpoint(uri).setLogLevel(RestAdapter.LogLevel.HEADES_AND_ARGS).setConverter(new GsonConverter(gson)).setClient(httpClient).build();
is possible mapping retrofit log to log4j ?
Upvotes: 1
Views: 1060
Reputation: 362
you can also use the log from okhttp , the snapshot from the version 2.6.0 which is introduced in the beta2 version of retrofit and going live soon is currently working fine, you can see how to implement it on this guide https://futurestud.io/blog/retrofit-2-log-requests-and-responses/ pretty easy to use and easy to implement.
Upvotes: 1
Reputation: 4010
You can do something like below:
new RestAdapter.Builder()
.setLogLevel(LOGGER.isDebugEnabled() ? retrofit.LogLevel.FULL : retrofit.LogLevel.BASIC)
.setLog(LOGGER.isDebugEnabled() ? LOGGER::debug : LOGGER::info)
retrofit.LogLevel
is used to specify how much detailed logging you require from Retrofit.
An implementation of retrofit.RestAdapter.Log
is passed to the setLog()
of RestAdapter
. RestAdapter
delegates all its internal logging to log4j Logger in this case.
Here LOGGER
is something that you would initialize like below
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(SomeClass.class);
LOGGER::debug
in Java 8 is equivalent to the below anonymous class
new retrofit.RestAdapter.Log() {
@Override
public void log(String message) {
LOGGER.debug(message);
}
}
Upvotes: 0
Reputation: 12365
The RestAdapter.Builder
has method setLog(Log)
which get's RestAdapter.Log
interface as parameter. This interface looks like below:
/** Simple logging abstraction for debug messages. */
public interface Log {
/** Log a debug message to the appropriate console. */
void log(String message);
}
You can set LogLevel as f.e. FULL and create class which wrap log4j and implements Log
interface and log your logs in log(String)
method, but you will get all logs as string. There is no possible to check from which level, the log comes.
EXAMPLE WRAPPER
public class ExampleLog4J implements RestAdapter.Log {
private static final Logger logger = logger.getLogger(ExampleLogger.class)
ExampleLog4J(){
}
@Override
public void log(String message) {
log.info("Retrofit# "+message);
}
}
And in the place where you create RestAdapter use:
new RestAdapter.Builder().setLog(new ExampleLog4J()).
Upvotes: 0