Description and features will be explained later.
In process of development.
TBE (To be explained)
To start logging add next lines in your Application
class in onCreate()
method. In fact, you can start the logger in any other place in your app, just take into consideration, that the logger will log only after starting it. Do it only once.
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.start(this);
...
}
}
The idea is not to make any difference between android.util.Log
usage and this logger usage. But it still have few very minor differences to make it even more simpler for coding:
Log levels:
- TRACE
- DEBUG
- INFO
- WARNING
- ERROR
Example:
// simple text
Log.e(this, "Your error text message");
// text with parameters
Log.e(this, "Your {} text {} number {}", param1, param2, param3);
// simple text with exception
Log.e(this, "Your error text message", throwable);
Logger has many options of configuration. In addition, you can add your own implementations and adjust the logger for your needs. Let's go over all configuration options and what will you get for each of them.
Set the output format of the logs. If you need your own structure, because you want to analyze or do any other action on the final logs, you would probably prefer them in specific structure.
Implement LogEntryFormatter
and set in LogConfiguration
.
Out of the box formatters:
SimpleLogEntryFormatter
JsonLogEntryFormatter
Example:
// create logger configuration
LogConfiguration logConfiguration = new LogConfiguration.Builder(this)
.setLogEntryFormatter(new JsonLogEntryFormatter())
...
.build();
// set configuration and start
Log.setConfiguration(logConfiguration);
Log.start(this);
To have a better picture of what's going on in your application when something goes wrong, you would be glad to have information about battery, screen, wifi, network and many other device changes. This logger gives this option and will listen to a system intents.
Implement SystemReceiver
and set in LogConfiguration
.
Out of the box receivers:
BatteryReceiver
ScreenReceiver
Example:
// create logger configuration
LogConfiguration logConfiguration = new LogConfiguration.Builder(this)
.addSystemReceiver(new BatteryReceiver())
.addSystemReceiver(new ScreenReceiver())
...
.build();
// set configuration and start
Log.setConfiguration(logConfiguration);
Log.start(this);
To stop events from being logged once you leave the app. Add this line into main Activity
in onDestroy()
method, or any other place where you decide to stop the logger.
Log.stop();
Report and send your logs if crash happened or just by demand. This logger library allows you to implement the dispatcher that will take the Report
and send/share it to your server or any other place.
Implement ReportDispatcher
Out of the box dispatchers:
EmailReportDispatcher
DriveReportDispatcher
Once the crash happened, you would like to report it. Prepare the Report
you want to send and set it in LogConfiguration
.
Prepare Report
example:
// create filter for crash report
LogsFilter logsFilter = new LogsFilter();
logsFilter.setLogLevel(Level.TRACE);
logsFilter.setLogTypes(Types.APP | Types.RECEIVER);
logsFilter.setFromTime(Calendar.getInstance().getTimeInMillis() - 1000 * 60 * 60);
// create crash report definition
Report crashReport = new Report.Builder()
.setIncludeDeviceInfo(true)
.setMergeLogs(true)
.setLogsFilter(logsFilter)
.build(this);
Set crash configuration:
// create logger configuration
LogConfiguration logConfiguration = new LogConfiguration.Builder(this)
.addCrashDispatcher(new EmailReportDispatcher("roman@everything.me"))
.setCrashReport(crashReport)
...
.build();
// set configuration and start
Log.setConfiguration(logConfiguration);
Log.start(this);
- Set thread priority - The thread priority of the logger
- Set cache memory buffer log size - The size of number of logs in-memory
- Set cache target type:
- Memory only - logs will be persisted in-memory only
- Internal - logs will be flushed into internal disk
- External - logs will be flushed into external disk if such exists
- Set history max days - The max number of history days of logs on disk
Example:
// create logger configuration
LogConfiguration logConfiguration = new LogConfiguration.Builder(this)
.setLogPriority(Thread.MIN_PRIORITY)
.setMemoryBufferSize(100)
.setCacheTargetType(CacheTargetType.EXTERNAL)
.setMaxHistoryDays(7)
...
.build();