Android Open Source - AGOGCyberStat Rolling File Appender






From Project

Back to project page AGOGCyberStat.

License

The source code is released under:

MIT License

If you think the Android project AGOGCyberStat 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 me.allenz.androidapplog;
//  w w  w  .  j a  v  a2s. c om
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RollingFileAppender extends AsyncAppender {

    private File logDir;

    private String packageName;

    private int rolling;

    private long rollSize;

    private File logFile;

    private FileOutputStream out;

    public RollingFileAppender(final File logDir, final long rollSize){
        this.logDir = logDir;
        this.rollSize = rollSize;
        packageName = LoggerFactory.getPackageName();
    }

    @Override
    protected boolean doStart() {
        final boolean created = createLogDir();
        if (!created) {
            LoggerFactory.getInternalLogger().verbose("can not create folder %s", logDir.getPath());
            return false;
        }
        rolling = getRollingNumber();
        logFile = getLogFile(rolling);
        try {
            out = createOrOpenLogFile(logFile);
        } catch (final IOException e) {
            e.printStackTrace();
        }
        return super.doStart();
    }

    @Override
    protected boolean doStop() {
        try {
            out.close();
        } catch (final IOException e) {
        }
        return super.doStop();
    }

    private boolean createLogDir() {
        if (!logDir.exists()) {
            return logDir.mkdirs();
        }
        return true;
    }

    private int getRollingNumber() {
        final Pattern fileNamePattern = Pattern.compile("^" +
                                                        packageName.replace(".", "\\.").replace("$", "\\$") +
                                                        "\\.(\\d+)\\.log(\\.gz)+$");
        int number = 0;
        final File[] logFiles = logDir.listFiles();
        for (int i = 0; i < logFiles.length; i++) {
            final String logFileName = logFiles[i].getName();
            final Matcher matcher = fileNamePattern.matcher(logFileName);
            if (matcher.find()) {
                try {
                    final Integer currentNumber = Integer.valueOf(matcher.group(1));
                    if (number < currentNumber) {
                        number = currentNumber;
                    }
                } catch (final NumberFormatException e) {
                }
            }
        }
        return number;
    }

    private File getLogFile(final int rolling) {
        String logFilename = packageName;
        if (rolling > 0) {
            logFilename += "." +
                           rolling;
        }
        logFilename += ".log";
        return new File(logDir, logFilename);
    }

    private FileOutputStream createOrOpenLogFile(final File logFile) throws IOException {
        if (!logFile.exists()) {
            logFile.createNewFile();
            return new FileOutputStream(logFile);
        } else {
            return new FileOutputStream(logFile, true);
        }
    }

    @Override
    protected void handleEventQueue() throws InterruptedException {
        try {
            final LogEvent event = logEventQueue.take();
            final byte[] logBytes = event.toString().getBytes();
            if (!logFile.exists()) {
                out.close();
                out = createOrOpenLogFile(logFile);
            }
            final long logFileSize = out.getChannel().size();
            if (logFileSize +
                logBytes.length > rollSize) {
                out.close();
                Runtime.getRuntime().exec("gzip " +
                                          logFile.getPath());
                rolling++;
                logFile = getLogFile(rolling);
                out = createOrOpenLogFile(logFile);
            }
            out.write(logBytes);
            out.flush();
            out.getFD().sync();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}




Java Source Code List

com.agog.cyberstat.BrR.java
com.agog.cyberstat.JSONSettings.java
com.agog.cyberstat.MainActivity.java
com.agog.cyberstat.MotisonNetTask.java
com.agog.cyberstat.MotisonXML.java
com.agog.cyberstat.MyPrefs.java
com.agog.cyberstat.NetR.java
com.agog.cyberstat.Trigger.java
me.allenz.androidapplog.AbstractAppender.java
me.allenz.androidapplog.AbstractLogger.java
me.allenz.androidapplog.AppenderSupportLogger.java
me.allenz.androidapplog.Appender.java
me.allenz.androidapplog.AsyncAppender.java
me.allenz.androidapplog.Configure.java
me.allenz.androidapplog.InternalLogger.java
me.allenz.androidapplog.LogCatAppender.java
me.allenz.androidapplog.LogEvent.java
me.allenz.androidapplog.LogLevel.java
me.allenz.androidapplog.LogTextView.java
me.allenz.androidapplog.LoggerConfig.java
me.allenz.androidapplog.LoggerFactory.java
me.allenz.androidapplog.Logger.java
me.allenz.androidapplog.PropertiesParser.java
me.allenz.androidapplog.ReflectUtils.java
me.allenz.androidapplog.Repository.java
me.allenz.androidapplog.RollingFileAppender.java
me.allenz.androidapplog.TextViewAppender.java