Android Open Source - slf4android File Log Handler






From Project

Back to project page slf4android.

License

The source code is released under:

MIT License

If you think the Android project slf4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.brightinventions.slf4android;
/*from   w  ww .  ja  v  a 2 s .c o  m*/
import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

class FileLogHandler extends FileLogHandlerConfiguration {
    private static String TAG = FileLogHandler.class.getSimpleName();
    private FileHandler fileHandler;
    private FileHandlerConfigParams config;
    private FileHandlerExpose fileHandlerExpose;

    public FileLogHandler(Context context, LogRecordFormatter formatter) {
        this.config = FileHandlerConfigParams.defaults(context);
        this.config.formatter = formatter;
        this.fileHandlerExpose = new FileHandlerExpose();
    }

    @Override
    public Filter getFilter() {
        ensureInitialized();
        return fileHandler.getFilter();
    }

    private void ensureInitialized() {
        if (fileHandler == null) {
            synchronized (this) {
                if (fileHandler == null) {
                    try {
                        fileHandler = new FileHandler(config.fileName, config.limit, config.count, config.append);
                        fileHandler.setFormatter(config.getFormatterAdapter());
                    } catch (IOException e) {
                        Log.e(TAG, "Could not create FileHandler", e);
                    }
                }
            }
        }
    }

    @Override
    public Formatter getFormatter() {
        ensureInitialized();
        return fileHandler.getFormatter();
    }

    @Override
    public void close() {
        ensureInitialized();
        fileHandler.close();
    }

    @Override
    public void flush() {
        ensureInitialized();
        fileHandler.flush();
    }

    @Override
    public void publish(LogRecord record) {
        ensureInitialized();
        fileHandler.publish(record);
    }

    @Override
    public FileLogHandlerConfiguration setFullFilePathPattern(String fullPathPattern) {
        ensureNotInitialized();
        config.fileName = fullPathPattern;
        return this;
    }

    private void ensureNotInitialized() {
        if (fileHandler != null) {
            throw new IllegalStateException("You can only change configuration before file handler is added to logger");
        }
    }

    @Override
    public String getCurrentFileName() {
        ensureInitialized();
        return fileHandlerExpose.getCurrentFileName(fileHandler);
    }

    private static class FileHandlerConfigParams {
        String fileName;
        int limit;
        int count;
        boolean append;
        LogRecordFormatter formatter;

        static FileHandlerConfigParams defaults(Context context) {
            FileHandlerConfigParams configParams = new FileHandlerConfigParams();
            configParams.limit = 1024 * 512; //512KB
            configParams.count = 5;
            configParams.fileName = new File(context.getApplicationInfo().dataDir, context.getPackageName() + ".%g.%u.log").getAbsolutePath();
            configParams.append = true;
            return configParams;
        }

        LogRecordFormatterUtilFormatterAdapter getFormatterAdapter() {
            return new LogRecordFormatterUtilFormatterAdapter(formatter);
        }
    }
}




Java Source Code List

com.squareup.seismic.ShakeDetector.java
org.slf4j.impl.StaticLoggerBinder.java
pl.brightinventions.slf4android.ActivityStateListener.java
pl.brightinventions.slf4android.AndroidLoggerAdapter.java
pl.brightinventions.slf4android.AndroidLoggerFactory.java
pl.brightinventions.slf4android.AtLeastFilter.java
pl.brightinventions.slf4android.ConstLoggerValueSupplier.java
pl.brightinventions.slf4android.DateValueSupplier.java
pl.brightinventions.slf4android.Disposable.java
pl.brightinventions.slf4android.EmailErrorReport.java
pl.brightinventions.slf4android.FileHandlerExpose.java
pl.brightinventions.slf4android.FileLogHandlerConfiguration.java
pl.brightinventions.slf4android.FileLogHandler.java
pl.brightinventions.slf4android.HandlerFormatterCompiler.java
pl.brightinventions.slf4android.LevelValueSupplier.java
pl.brightinventions.slf4android.ListLogRecordFormatter.java
pl.brightinventions.slf4android.Lists.java
pl.brightinventions.slf4android.LogLevel.java
pl.brightinventions.slf4android.LogRecordFormatterUtilFormatterAdapter.java
pl.brightinventions.slf4android.LogRecordFormatter.java
pl.brightinventions.slf4android.LogRecord.java
pl.brightinventions.slf4android.LogcatHandler.java
pl.brightinventions.slf4android.LoggerConfiguration.java
pl.brightinventions.slf4android.LoggerNameValueSupplier.java
pl.brightinventions.slf4android.LoggerPatternConfiguration.java
pl.brightinventions.slf4android.LoggerPatternValueSupplier.java
pl.brightinventions.slf4android.LoggerPattern.java
pl.brightinventions.slf4android.MakeScreenShotAsyncTask.java
pl.brightinventions.slf4android.MessageValueSupplier.java
pl.brightinventions.slf4android.NotifyDeveloperDialogDisplayActivity.java
pl.brightinventions.slf4android.NotifyDeveloperHandler.java
pl.brightinventions.slf4android.ReadLogcatEntriesAsyncTask.java
pl.brightinventions.slf4android.ThreadValueSupplier.java
pl.brightinventions.slf4android.androidTest.FileHandlerTests.java
pl.brightinventions.slf4android.androidTest.LoggerAdapterTests.java
pl.brightinventions.slf4android.androidTest.NotifyDeveloperHandlerTests.java
pl.brightinventions.slf4android.androidTest.TestActivity.java
pl.brightinventions.slf4android.roboelectric.HandlerFormatterCompilerTests.java
pl.brightinventions.slf4android.roboelectric.MessageValueSupplierTests.java
pl.brightinventions.slf4android.roboelectric.RoboelectricTest.java
pl.brightinventions.slf4android.roboelectric.RobolectricTestRunner.java