We are using Java Util Logging
within Box Java SDK. You can read about details of this
approach here.
We are using only few of the logging levels:
FINE
- when logging debugging information, like success responses,WARNING
- when API returned 4xx response code,SEVERE
- when API returned 5XX response code.
By default, the Java Util Logging
prints to console output only messages that are logged at least at INFO
level. You
can limit what is being logged by setting desired level on com.box.sdk
logger like this:
import com.box.sdk.BoxLogger;
BoxLogger.defaultLogger().setLevelToError();
Example above will limit printed logs to SEVERE
level only.
By default FINE
level logs are not printed to the console. There are several ways to do that.
One way is to define custom handler:
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
Logger sdkLogger = BoxLogger.defaultLogger();
Handler systemOut = new ConsoleHandler();
// this handler will print any message to System.out
systemOut.setLevel(Level.ALL);
sdkLogger.addHandler(systemOut);
// allow sdk logger to print FINE logs
sdkLogger.setLevelToAll();
// prevent logs from being processed by default Console handler
sdkLogger.setUseParentHandlers(false);
you can do this in your aplication initialization.
To disable logging use BoxLogger.defaultLogger().turnLoggingOff()
.
To reset to default logging level use BoxLogger.defaultLogger().resetToDefaultLevel()
.
If you are using different logging solution like Log4J2 or Logstash you can use SLF4J and proper bridgge to get your logs into desired solution. Below is an example of adding SDK logs into Logstash. First you will need to add SLF4j and Logstash binaries:
implementation 'org.slf4j:jul-to-slf4j:1.7.32' // bridge from Java Util Logging to SLF4J API
implementation 'org.slf4j:slf4j-api:1.7.32' // define SLF4J API
implementation 'ch.qos.logback:logback-core:1.2.7' // use the Logback logging framework
implementation 'ch.qos.logback:logback-classic:1.2.7' // get bindings between SLF4J and Logback
Now when initializing application you will need to make Java Util Logging to use SLF4J. This sample is based on description provided here:
import org.slf4j.bridge.SLF4JBridgeHandler;
// Remove existing handlers attached to JUL root logger
SLF4JBridgeHandler.removeHandlersForRootLogger();
// add SLF4JBridgeHandler to JUL's root logger
SLF4JBridgeHandler.install();
Now all logs generated by SDK will be captured by Logback. If you want to see debug logs from SDK you will need to add this configuration to your logback.xml file:
<logger name="com.box.sdk" level="DEBUG"/>
Below is a list of log levels used by SDK and coresponding Logback levels:
SDK | Logback |
---|---|
ALL | ALL |
OFF | OFF |
FINE | DEBUG |
INFO | INFO |
WARN | WARN |
SEVERE | ERROR |
You can read more on java Util Logging to SLF4J bridge here.