Skip to content

Commit

Permalink
example: add javadoc to JULHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0rg committed Sep 29, 2014
1 parent ab60ccd commit 3f67eba
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions example/src/de/duenndns/mtmexample/JULHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
/**
* A <code>java.util.logging</code> (JUL) Handler for Android.
* <p>
* In order to initialize JULHandler, call {@link #initialize()}.
* If you want fine-grained control over MTM's logging, you can copy this
* class to your code base and call the static {@link #initialize()} method.
* </p>
* <p>
* This JUL Handler passes log messages sent to JUL to the Android log, while
* keeping the format and stack traces from eventually existing Exceptions. It
* keeping the format and stack traces of optionally supplied Exceptions. It
* further allows to install a {@link DebugLogSettings} class via
* {@link #setDebugLogSettings(DebugLogSettings)} that determines whenever JUL log messages of
* {@link #setDebugLogSettings(DebugLogSettings)} that determines whether JUL log messages of
* level {@link java.util.logging.Level#FINE} or lower are logged. This gives
* the application developer more control over the logged messages, while
* allowing a library developer to place debug log messages without risking to
* spam the Android log.
* </p>
* <p>
* If there are no {@code DebugLogSettings} configured, then all messages send
* If there are no {@code DebugLogSettings} configured, then all messages sent
* to JUL will be logged.
* </p>
*
Expand All @@ -40,6 +41,8 @@
@SuppressWarnings("deprecation")
public class JULHandler extends Handler {

/** Implement this interface to toggle debug logging.
*/
public interface DebugLogSettings {
public boolean isDebugLogEnabled();
}
Expand All @@ -64,13 +67,15 @@ public interface DebugLogSettings {
);
// @formatter:on

// Constants for Android vs. JUL debug level comparisons
private static final int FINE_INT = Level.FINE.intValue();
private static final int INFO_INT = Level.INFO.intValue();
private static final int WARN_INT = Level.WARNING.intValue();
private static final int SEVE_INT = Level.SEVERE.intValue();

private static final Logger LOGGER = Logger.getLogger(CLASS_NAME);

/** A formatter that creates output similar to Android's Log.x. */
private static final Formatter FORMATTER = new Formatter() {
@Override
public String format(LogRecord logRecord) {
Expand Down Expand Up @@ -131,6 +136,7 @@ public boolean isLoggable(LogRecord record) {
return true;
}

/** JUL method that forwards log records to Android's LogCat. */
@Override
public void publish(LogRecord record) {
if (!isLoggable(record)) return;
Expand All @@ -142,6 +148,7 @@ public void publish(LogRecord record) {
Log.println(priority, tag, msg);
}

/** Helper to convert JUL verbosity levels to Android's Log. */
private static int getAndroidPriority(Level level) {
int value = level.intValue();
if (value >= SEVE_INT) {
Expand All @@ -155,6 +162,7 @@ private static int getAndroidPriority(Level level) {
}
}

/** Helper to extract short class names. */
private static String substringAfterLastDot(String s) {
return s.substring(s.lastIndexOf('.') + 1).trim();
}
Expand Down

0 comments on commit 3f67eba

Please sign in to comment.