Log Providers
IHLog work with Android Log API out of the box. On the other hand, we may configure it to use different
targets with ihlog.properties
file or LogSettings
class object. You may even implement your own
target.
Each of the target is handled by a class that implemented LogProvider
interface. It is responsible
for creating object that implements IHLog
interface which keep the state of log specific state.
To change LogProvider in use:
There are also some classes in the library that warp up other IHLog objects to augment their functionalities.
AndroidLogProvider
#
It can accept one parameter, which is the Android API level. The default value
is obtained from the Build.VERSION.SDK_INT
constant of API framework.
AndroidLogProvider
maps the log call to standard Android Log API. There are a few things to note:
- There is no
trace
log level for Android Log, so bothtrace
andinfo
level are mapped toinfo
level of Android Log. - Before Android N, tag string is limited to 23 characters. There is no such limitation for newer OS. IHLog library tries to accept longer tag string on pre-N devices, by moving the part of tag string over 23 characters to log body. For example, for the call
the logcat output for pre-N devices will look like this:
NullLogProvider
#
It is a dummy log provider to discard all log output. It output nothing, no matter what priority of log item is.
PrintWriterLogProvider
#
It output log lines to a specific PrintWriter
object. It is a building
block of ConsoleLogProvider.
It accepts two parameters.
Parameter | Meaning |
---|---|
printWriter | PrintWriter object to accept log lines. Mandatory. |
timeSource | Object that implements TimeSource interface to provide real time clock. The default implementation is invoking System.currentTimeMillis() . In unit test scenarios, we may provide a fake implementation that return fix value as time, so the output is totally reproducible. |
StdioLogProvider
#
It is a simplified PrintWriterLogProvider that output all log lines to standard output.
ConsoleLogProvider
#
It diverts the log to a log for error and the other for non-error. This is an abstraction of having nornal log and error log in different streams or files.
It accepts three parameters, all are optional:
Parameter | Meaning |
---|---|
timeSource | Object that implements TimeSource interface to provide real time clock. The default implementation is invoking System.currentTimeMillis() . In unit test scenarios, we may provide a fake implementation that return fix value as time, so the output is totally reproducible. |
outLogProviderr | A log provider that dedicate for log line with Priority Info , Trace and Debug . The default provider output log lines to standard output. |
errLogProviderr | A log provider that dedicate for log line with Priority Warn , Error and Fatal . The default provider output log lines to standard error. |
StringLogProvider
#
It output log lines to StringWriter object, so that we can review the log lines
as String
in memory. It is designed for captuing log in unit test.
It accepts two parameters, both are optional:
Parameter | Meaning |
---|---|
stringWriter | The StringWriter object that capture the log lines |
timeSource | Object that implements TimeSource interface to provide real time clock. The default implementation is invoking System.currentTimeMillis() . In unit test scenarios, we may provide a fake implementation that return fix value as time, so the output is totally reproducible. |
For example, we could set up a unit test friendly log like this:
#
Write your own Log ProvidersIt is easy to create your own implementation of LogProvider
interface. It is
basically a factory to create an implementation of IHLog
interface.
Let's create a simple fancy Log Provider as an example.
We want to send log to standard out, with fancy format:
The essense is to implement two methods IHLogProvider.getLog(defaultTag)
and
IHLog.log(priority, tag, message)