Skip to content

Logging via Ln

stephanenicolas edited this page Oct 16, 2014 · 1 revision

Android applications typically use the built-in android.util.Log facility to log information to the android console. RoboGuice provides an alternate logger which you may want to consider using.

RoboGuice's Ln logger is similar to Log, but has the following advantages:

  • Debug and verbose logging are automatically disabled for release builds.
  • Your app name, file and line of the log message, time stamp, thread, and other useful information is automatically logged for you. (Some of this information is disabled for release builds to improve performance).
  • Performance of disabled logging is faster than Log due to the use of the varargs. Since your most expensive logging will often be debug or verbose logging, this can lead to a minor performance win.
  • You can override where the logs are written to and the format of the logging.

A Few Examples

    Ln.v("hello there");
    Ln.d("%s %s", "hello", "there");                             // varargs
    Ln.e( exception, "Error during some operation");             // Throwables go at the FRONT!
    Ln.w( exception, "Error during %s operation", "some other");
    Ln.v("hello there, ", "Mr. Invisible");                      // ERROR, Mr. Invisible will never display

Common Pitfalls

Ln's syntax is slightly different than Log's. Be aware of the following:

  • Make sure you put the exception FIRST in the call. A common mistake is to place it last as is the android.util.Log convention, but then it will get treated as varargs parameter.
  • vararg parameters are not appended to the log message! You must insert them into the log message using %s or another similar format parameter. If you forget, the parameter will be dropped.

Changing the logging behavior

Some apps may want to consider logging to alternate locations than the default android console. For example, you might want to log a copy of all log statements to a file for easy crash reporting.

To do this, simply implement a subclass of Ln.Print and make sure to bind your subclass in your module implementation.

Be aware that Ln does not do any log rolling for you, so be careful not to accidentally fill up your device's memory if you write to a file!

You can also override Print to change the format of the logging.

Be aware that overriding Print is a vm-wide change! Any other code that relies on Ln logging (which is likely RoboGuice, your own app, and any libraries that depend on RoboGuice) will also use your alternate Print.

Clone this wiki locally