Home

Awesome

jlog

Android Arsenal License Download Build Status

jlog is a useful log tool for android developers which was inspired by these projects:

Combining with work experience, i made this libriary.

Hope you enjoy it. ( ^_^ )

中文文档

Features

jlog sample

Dependency

Add the repository to the root's build.gradle.

allprojects {
 repositories {
    jcenter()
    maven { url "https://jitpack.io" }
 }

And add dependencies to the module's build.gradle.

dependencies {
     compile 'com.github.JiongBull:jlog:0.1.0'
}

Configuration

Initialization

It is recommend that initializing jlog's global configuration in your application's onCreate() method, then we can use it everywhere.

public class RootApp extends Application {

    private static Logger sLogger;

    @Override
    public void onCreate() {
        super.onCreate();
        List<String> logLevels = new ArrayList<>();
        logLevels.add(LogLevel.ERROR);
        logLevels.add(LogLevel.WTF);

        sLogger = Logger.Builder.newBuilder(getApplicationContext(), "jlog")
                /* properties below are default value, you can modify them or not. */
                .setDebug(true)
                .setWriteToFile(false)
                .setLogDir("jlog")
                .setLogPrefix("")
                .setLogSegment(LogSegment.TWELVE_HOURS)
                .setLogLevelsForFile(logLevels)
                .setZoneOffset(TimeUtils.ZoneOffset.P0800)
                .setTimeFormat("yyyy-MM-dd HH:mm:ss")
                .setPackagedLevel(0)
                .setStorage(null)
                .build();
    }

    public static Logger getLogger() {
        return sLogger;
    }
}

All properties are saved in Logger's object after build . Modify it's properties, it will work next time.

For example:

logger.setWriteToFile(true);

If your app's targetSdkVersion is 23+, don't forget to apply for android.permission.WRITE_EXTERNAL_STORAGE permission in your splash or main activity.

setDebug(boolean)

Default is true, logs will be outputed to the console. Pls set this variable as false when release your app.

logger.setDebug(false);

or

logger.setDebug(BuildConfig.DEBUG);

writeToFile(boolean)

If true, logs will output to file, default is false.

  logger.writeToFile(true);

setLogDir(String)

Configure the directory which saving logs, the directory is located in external sdcard and default name is jlog.

You can use your app's name as directory's name.

logger.setLogDir(getString(R.string.app_name));

Sub directory is supported as well, you can use some unique words as sub directory's name, such as user id.

logger.setLogDir(getString(R.string.app_name) + File.separator + ${userid});

setLogPrefix(String)

If you don't want use sub directory for logs, you may try prefix for log file.

logger.setLogPrefix("JiongBull");

setLogSegment(LogSegment)

Logs are divied into separate files by time segment, default is 24H, file's name is like 2016-01-19.log, if it is setted to LogSegment.ONE_HOUR, file's name is like 2016-01-19_0203, which means logs were recorded from 2:00 to 3:00.

logger.setLogSegment(LogSegment.ONE_HOUR);

setLogLevelsForFile(List<String>)

This method decides logs in which level can be outputted to file. Default are LogLevel.ERROR and LogLevel.WTF.

List<LogLevel> logLevels = new ArrayList<>();
logLevels.add(LogLevel.INFO);
logLevels.add(LogLevel.ERROR);
logger.setLogLevelsForFile(logLevels);

setZoneOffset(ZoneOffset)

We can specify log's time zone no matter where user from, this method make it easy to find bugs, default is ZoneOffset.P0800(+0800), which means "Beijing time".

logger.setZoneOffset(ZoneOffset.P0800);

setTimeFormat(String)

Default time format is yyyy-MM-dd HH:mm:ss, you can use this method to make it easy to understand.

logger.setTimeFormat("yyyy年MM月dd日 HH时mm分ss秒");

setPackagedLevel(int)

If you want to extend jlog, please set package's level(hierarchy), otherwise, jlog can't get invoker's info.

logger.setPackagedLevel(1);

setStorage(IStorage)

You can implement IStorage interface, upload logs to remote server in upload method, it will be invoked every fifteen minutes.

logger.setStorage(new IStorage() {
    @Override
    public void upload(@NonNull Logger logger) {
        // upload logs to remote server
    }
})

There are two predefined IStorage implement, you can use them directly.

Usage

logger.v(String)

Jlog will use caller's class name as default tag.

logger.v(TAG, String)

You can specify TAG as well.

logger.json(json)

Jlog formats json content in a pretty way

About me

GitHub WeiBo Blog