Package 

Class Log


  • 
    public class Log
    
                        

    Utility class for Android libraries, that simplifies the use of android.util.Log by improving the call to all log methods by supplying arguments as parameters {@code '%s'} instead of creating a string. This avoids useless memory allocations when not requested: the {@code StringBuilder} itself, the bufferand the {@code String} object. The new methods check in advance if the level is enabled and only after the string message with arguments is created.

    Note: This class can work in collaboration with Logger.

    Others features are:

    • Automatic TAG corresponding to the caller class name.
    • Runtime log level without the need of props file (useful for libraries).
    • Method name with line number.

    Use SUPPRESS to disable all logs. For instance:

    if (BuildConfig.DEBUG) {
        Log.setLevel(Level.INFO);
    } else {
        Log.setLevel(Level.SUPPRESS);
    }
    Log.i("Message arg1=%s, arg2=%s", arg1, arg2);
    
    • Method Detail

      • setLevel

         static void setLevel(int level)
      • logMethodName

         static void logMethodName(boolean method, boolean line)

        Allows to log method name and line number from where the log is called.

        - With method name: {@code [method] msg}.- With line number: {@code [method:line] msg}.

        Note: Line number needs method name enabled.
        Parameters:
        method - true to print method name at the beginning of the message, false otherwise
        line - true to print line number after the method name, false otherwise
      • v

         static void v(String msg, Array<Object> args)

        Sends a VERBOSE log message.

        Parameters:
        msg - the message you would like logged
        args - the extra arguments for the message
      • d

         static void d(String msg, Array<Object> args)

        Sends a DEBUG log message.

        Parameters:
        msg - the message you would like logged
        args - the extra arguments for the message
      • i

         static void i(String msg, Array<Object> args)

        Sends an INFO log message.

        Parameters:
        msg - the message you would like logged
        args - the extra arguments for the message
      • iTag

         static void iTag(String tag, String msg, Array<Object> args)

        As i but with custom tag for one call only.

        Parameters:
        tag - custom tag, used to identify the source of a log message
        msg - the message you would like logged
        args - the extra arguments for the message
      • w

         static void w(String msg, Array<Object> args)

        Sends a WARN log message.

        Parameters:
        msg - the message you would like logged
        args - the extra arguments for the message
      • w

         static void w(Throwable t, String msg, Array<Object> args)

        Sends a WARN log message with the Exception at the end of the message.

        Parameters:
        t - The exception to log
        msg - the message you would like logged
        args - the extra arguments for the message
      • e

         static void e(String msg, Array<Object> args)

        Sends an ERROR log message.

        Parameters:
        msg - the message you would like logged
        args - the extra arguments for the message
      • e

         static void e(Throwable t, String msg, Array<Object> args)

        Sends an ERROR log message with the Exception at the end of the message.

        Parameters:
        t - The exception to log
        msg - the message you would like logged
        args - the extra arguments for the message
      • wtf

         static void wtf(String msg, Array<Object> args)

        What a Terrible Failure: Report a condition that should never happen.

        Parameters:
        msg - the message you would like logged
        args - the extra arguments for the message
      • wtf

         static void wtf(Throwable t, String msg, Array<Object> args)

        What a Terrible Failure: Report a condition that should never happen with the Exceptionat the end of the message.

        Parameters:
        t - The exception to log
        msg - the message you would like logged
        args - the extra arguments for the message
      • useTag

         static void useTag(@Nullable() String customTag)

        Sets a general static custom tag for ALL log calls.

        Note: If a Logger instance is created, this custom tag will bereset to {@code null} and current filename will be used instead.

        Parameters:
        customTag - general static tag for ALL logs.