Code Monkey home page Code Monkey logo

slf4android's Introduction

slf4android

A simple implementation of slf4j api using android java.util.logging.*. This means you can easily hook in any existing java.util.logging.Handler implementations.

To use this little gem you'll need to add http://bright.github.io/maven-repo/ to your repositories:

repositories {
    maven {
        url "http://bright.github.io/maven-repo/"
    }
}

and then declare a dependency inside a module:

dependencies {
    compile('pl.brightinventions:slf4android:0.0.11@aar'){
      transitive = true
    }
    //other dependencies
}

As with any slf4j compatible implementation using slf4android looks like this:

class HomeActivity extends Activity {
    private static final Logger LOG = LoggerFactory.getLogger(HomeActivity.class.getSimpleName());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOG.debug("Hello from {} saved instance state {}", this, savedInstanceState);
    }
}

Logging to Crashlytics

Crashlytics has a nice feature of attaching log entries that were issued just before a crash. With slf4android it's easy to add a handler so that whenever a crash happens you get more insight as to what happened just before it.

To achieve that you need a custom handler:

import com.crashlytics.android.Crashlytics;
import java.util.logging.Handler;
import pl.brightinventions.slf4android.LogRecord;
import pl.brightinventions.slf4android.MessageValueSupplier;

public class CrashlyticsLoggerHandler extends Handler {
    MessageValueSupplier messageValueSupplier = new MessageValueSupplier();

    @Override
    public void publish(java.util.logging.LogRecord record) {
        LogRecord logRecord = LogRecord.fromRecord(record);
        StringBuilder messageBuilder = new StringBuilder();
        messageValueSupplier.append(logRecord, messageBuilder);
        String tag = record.getLoggerName();
        int androidLogLevel = logRecord.getLogLevel().getAndroidLevel();
        Crashlytics.log(androidLogLevel, tag, messageBuilder.toString());
    }

    @Override
    public void close() {
    }

    @Override
    public void flush() {
    }
}

That is added to root logger:

LoggerConfiguration.configuration()
        .removeRootLogcatHandler()
        .addHandlerToRootLogger(new CrashlyticsLoggerHandler());

Note that we remove a default logcat handler since Crashlytics will push messages to logcat too.

Logging to a file

To print messages to a separate file just add:

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
String logFileName = fileHandler.getCurrentFileName();
// logFileName contains full path to logged file

inside your custom android.app.Application onCreate method. This will create rotated log files inside context.getApplicationInfo().dataDir with a name derived from context.getPackageName() and a default message pattern %date %level [%thread] %name - %message%newline

To change the location of log file you can use:

FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);

fileHandler.setFullFilePathPattern("/sdcard/your.package/my_log.%g.%u.log");

LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);

slf4android's People

Contributors

miensol avatar mklimek avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.