An open API service indexing awesome lists of open source software.

https://github.com/ech0s7r/androidlog

A simple and lightweight android logger library
https://github.com/ech0s7r/androidlog

android crash-reporting extendable file java kotlin library lint logcat logger pretty-logger

Last synced: 26 days ago
JSON representation

A simple and lightweight android logger library

Awesome Lists containing this project

README

          

# Android Logger Library

[ ![Download](https://api.bintray.com/packages/ech0s7r/Android-Lib/androidlog/images/download.svg?version=1.2.11) ](https://bintray.com/ech0s7r/Android-Lib/androidlog/1.2.10/link)

Logger library created for simple integration of logger capability to android applications providing APIs to write logs with different level of verbosity and avoiding to write boilerplate code like declaring TAG for each class or use static functions to get the logger instance every time as implemented in the most logger library on the market. This library makes the logger process simple to use, pretty and powerful.

The library is distributed as Android Studio project named *loggerlib* that depends on the module *lintrules*.

The order in terms of verbosity, from the least to the most is: ASSERT, ERROR, WARN, INFO, DEBUG and VERBOSE. Verbose and Debug should never be used in production. Assert should be used only to log error that should never happens (eg. App crash).

### Features

- Minimum performance impact
- Logcat integration
- Log file writing
- File rotation
- CSV format output
- Editable current timestamp
- Crash system detection
- Compile time correctness detection
- Log level supports
- String format args
- Customizable *Layout* and *Appenders*

### (optional) Log in a dedicated process

The library is fully thread-safe, it can be configured for creating a separate process for the logger and communicate with it over AIDL, each library function returns immediately to the caller and the file writing is managed in a separate process with a MIN PRIORITY daemon thread.

### Download

Repository available on [jCenter](http://jcenter.bintray.com/com/ech0s7r/android/loggerlib/) and [Bintray](https://bintray.com/ech0s7r/Android-Lib/androidlog)

In your build.gradle, add:

```
implementation 'com.ech0s7r.android:loggerlib:1.2.11'
```

### *[Optional] Enable text report*

If you wish to enable the text report, add the following in your app module build.gradle:

```groovy
lintOptions {
textReport true
textOutput 'stdout'
}
```

### Setting up

1. Add *loggerlib* library into the project

2. Initialize the logger using the LoggerConfigurator class

```java
LoggerConfigurator.init(
Logger.Level.WARN, /* Minimum level to log */
123, /* Application ID */
"MyApp", /* Application name */
BuildConfig.VERSION_NAME, /* Application version */
getDeviceId(), /* Device ID */
"MyAppAndroid.log"); /* File name prefix */
```

3. Add *Layouts* with *Appenders*

```java
LoggerConfigurator.addAppender(new LogcatAppender(new LogcatLayout()));
LoggerConfigurator.addAppender(new FileAppender(getApplicationContext(), new CsvLayout()));
```

The supported appenders, for now, are writing on Logcat and writing on File with Csv layout and Logcat layout.

### How to use

The Logger usage is pretty simple, all that you need is to call the right static method from the Logger class, as following:

##### Verbose

```java
Logger.v("verbose");
```
##### Debug
```java
Logger.d("debug");
```

##### Info
```java
Logger.i("info");
```

##### Warning
```java
Logger.w("warning");
```

##### Error
```java
Logger.e("error");
```

##### Wtf
```java
Logger.wtf("What a Terrible Failure");
```

##### String format arguments are supported
```java
Logger.w("Hello %s %d", "world", 1);
```

##### How to log Exceptions
```java
try {
...
} catch (Exception e){
Logger.e(e);
// or
Logger.e("description", e);
}
```

### Compilation time check (Lint integration)

The library implements 4 kinds of custom Lint rules to avoid common error and promote the usage of this library instead of the android built-in logger library. These rules are checked at compile time and if not respected an error will be generated during the build process. The Lint scanning tools, in case of build error, generate an HTML file that can be controlled to see where the following rules are not observed.

- ##### SystemOutDetector

All the usages of the System.out.print/ln() or System.err.print/ln() are forbidden.

- ##### PrintStackTraceDetector

The usage of Throwable.printStackTrace() is forbidden, see How to log Exception.

- ##### AndroidLogDetector

The usage of android.utils.Log is forbidden

- ##### NoLoggedException

Every Exception should be logged

### Customization

New *Appenders* and *Layout*s can be added by developers, extending the *LogAppender* and *LogLayout* classes.

New Lint rules are more than welcome and can be added in the *lintrules* module.

### Ouptut

##### Logcat

![logcat example](art/logcat_example.PNG)

Each line logged on Android logcat contains the following elements:

1. Date and Time
2. Process ID
3. App package
4. Log level
5. Thread ID
6. Class.Method (File name : code line)
7. Message

##### File

The log files are stored into the sd-card in a new directory with the same name of the application, into the folder “logs”. The file name is generated adding the current date (format yyyyMMdd) to file prefix used during the logger configuration.

*Example:*
If the application name is MyApp and the file prefix is MyAppAndroid.log, the generated files are:

> ​ …
>
> ​ /sdcard/logs/MyApp/MyAppAndroid.log20170803
>
> ​ /sdcard/logs/MyApp/MyAppAndroid.log20170804
>
> ​ …

![file_example](art/file_example.PNG)

License
-------

Copyright 2012 ech0s7r

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.