https://github.com/everythingme/android-logger
https://github.com/everythingme/android-logger
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/everythingme/android-logger
- Owner: EverythingMe
- License: apache-2.0
- Created: 2013-12-01T13:54:22.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-02-26T23:12:01.000Z (over 12 years ago)
- Last Synced: 2025-06-24T22:42:29.468Z (12 months ago)
- Language: Java
- Size: 1.02 MB
- Stars: 6
- Watchers: 16
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
android-logger
==============
Description and features will be explained later.
## Features
In process of development.
## Setup Project
TBE (To be explained)
## Usage
### Start Logger
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.
``` java
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.start(this);
...
}
}
```
### Log
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:**
``` java
// 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);
```
## Configuration
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.
### 1. Format the output
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:**
``` java
// create logger configuration
LogConfiguration logConfiguration = new LogConfiguration.Builder(this)
.setLogEntryFormatter(new JsonLogEntryFormatter())
...
.build();
// set configuration and start
Log.setConfiguration(logConfiguration);
Log.start(this);
```
### 2. System events
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:**
``` java
// 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.
``` java
Log.stop();
```
### 3. Reporting
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`
#### Crash report
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:**
``` java
// 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:**
``` java
// 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);
```
### 4. More
- 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:**
``` java
// create logger configuration
LogConfiguration logConfiguration = new LogConfiguration.Builder(this)
.setLogPriority(Thread.MIN_PRIORITY)
.setMemoryBufferSize(100)
.setCacheTargetType(CacheTargetType.EXTERNAL)
.setMaxHistoryDays(7)
...
.build();
```
[](https://bitdeli.com/free "Bitdeli Badge")