Android Logger Refactor - Encapsulate

Android Logger Refactor - Encapsulate

This is a simple refactor to bring encapsulation to the current log level and controlling if the output should go to android.util.Log or System.out.

As part 4 mentioned; this is super simple. The change to FyzLog is to take the following

    /**
     * Write Log Message to {@link System#out#println}
     */
    @VisibleForTesting
    public static boolean doPrint = false;

    /**
     * Controls the level of logging.
     * This can be configured during build to limit logging messages to {@link Log#ERROR} or other levels.
     * Default value is {@link Log#VERBOSE the lowest setting}
     */
    @VisibleForTesting
    public static LogLevel logLevel = LogLevel.VERBOSE;

and wrap the public variables in setters. Yes; setters are bad; so let's do it by behavior.

If we want to toggle to write to System.out it seems an appropriate name would be writeToSystem.

When setting the log level; it's really updating the current log level... so updateCurrentLogLevel.
With the comments deleted for smelling; we're left with

    /* package */ static void writeToSystem(){
        doPrint = true;
    }
    /* package */ static void writeToLog(){
        doPrint = false;
    }
    private static LogLevel logLevel = LogLevel.VERBOSE;
    /* package */ static void updateCurrentLogLevel(final LogLevel logLevel){
        FyzLog.logLevel = logLevel;
    }

I have them as package protected because; while heavy for test purpose; they are no longer 'for testing'. They are behavior methods that can or cannot be used. They just happen to be most useful for tests. heh - yea; weak justification. I'll go with; This is a boundary layer; they have exceptions to "proper" behavior.

In TheFyzLogTests we need to go through and update all instances of either

        FyzLog.logLevel = LogLevel.VERBOSE;
        FyzLog.doPrint = false;

to work with the appropriate methods.


From those changes; encapsulation is created and all tests still pass!

A quick and easy update.

Next will be extracting a Logger instance for the Android Log functionality!

I can't wait!!!!

Part 1 - Part 2 - Part 3 - Part 4 - [Part 5]

Show Comments