|
|
|
# Overview
|
|
|
|
|
|
|
|
Android has a build-in logging utility, android.util.Log, which is a jni connect to a c-compiler logging system. The system uses a rotation of files of a designated size. Both the size and the number of log files in rotation are configurable.
|
|
|
|
|
|
|
|
The Log calls take a String for a Tag, a String for the message and an optional Exception object. There is an isLoggable() call which can be used to eliminate costly string concatenation calls when the log calls should be ignored. Unfortunately this optimazation is often overlooked by devs. It is both cumbersome and it clutters up code even further.
|
|
|
|
|
|
|
|
SLF4j is a logger implementation that is designed to resolve some of the working issues with loggers. The implementation consists of a standardized api and an android specific implementation.
|
|
|
|
|
|
|
|
The android impl still uses the base android Logging utility, but with a few key improvements. The calls automatically respect the isLoggable() status, and the messages can be formatted in a printf style, where arguments are referred to positionally with '{}' and the actual argument objects are tacked onto the end of the log call. SLF4j checks the isLoggable() status and if the message should not be logged, all string processing is avoided.
|
|
|
|
|
|
|
|
isLoggable(TAG, LEVEL) translates to an OS level check of a system property named as log.tag.<TAG>, where <TAG> is the under 23 char tag to be checked for. Through adb the tags and their levels can be modified using `set prop log.tag.<TAG> D|I|V|S`
|
|
|
|
|
|
|
|
In the event the message should be logged, each object that is not null has it's toString() function called and is inserted into the format string. After processing, the message is sent to the android log util for stock processing.
|
|
|
|
|
|
|
|
In practice, this looks like :
|
|
|
|
`static Logger logger = LoggerFactory.getLogger("TAG");`
|
|
|
|
...
|
|
|
|
`logger.debug( "log event= arg1:{}, arg2:{}, arg3:{}", arg1, arg2, arg3 );`
|
|
|
|
|
|
|
|
|