Search in sources :

Example 6 with DailyRollingFileAppender

use of org.apache.log4j.DailyRollingFileAppender in project voltdb by VoltDB.

the class VoltLog4jLogger method setFileLoggerRoot.

/**
     * Static method to change the log directory root
     * @param logRootDH log directory root
     */
public static void setFileLoggerRoot(File logRootDH) {
    if (System.getProperty("log4j.configuration", "").toLowerCase().contains("/voltdb/tests/")) {
        return;
    }
    if (Boolean.parseBoolean(System.getProperty("DISABLE_LOG_RECONFIGURE", "false"))) {
        return;
    }
    checkArgument(logRootDH != null, "log root directory is null");
    File logDH = new File(logRootDH, "log");
    File napFH = new File(logDH, "volt.log");
    Logger rootLogger = LogManager.getRootLogger();
    DailyRollingFileAppender oap = null;
    @SuppressWarnings("unchecked") Enumeration<Appender> appen = rootLogger.getAllAppenders();
    while (appen.hasMoreElements()) {
        Appender appndr = appen.nextElement();
        if (!(appndr instanceof DailyRollingFileAppender))
            continue;
        oap = (DailyRollingFileAppender) appndr;
        File logFH = new File(oap.getFile());
        if (!logFH.isAbsolute())
            break;
        oap = null;
    }
    if (oap == null) {
        return;
    }
    DailyRollingFileAppender nap = null;
    try {
        if (!logDH.exists() && !logDH.mkdirs()) {
            throw new IllegalArgumentException("failed to create directory " + logDH);
        }
        if (!logDH.isDirectory() || !logDH.canRead() || !logDH.canWrite() || !logDH.canExecute()) {
            throw new IllegalArgumentException("Cannot access " + logDH);
        }
        nap = new DailyRollingFileAppender(oap.getLayout(), napFH.getPath(), oap.getDatePattern());
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to instantiate a DailyRollingFileAppender for file " + napFH, e);
    }
    nap.setName(oap.getName());
    rootLogger.removeAppender(oap.getName());
    rootLogger.addAppender(nap);
    File oldFH = new File(oap.getFile());
    if (oldFH.exists() && oldFH.isFile() && oldFH.length() == 0L && oldFH.delete()) {
        File oldDH = oldFH.getParentFile();
        if (oldDH.list().length == 0) {
            oldDH.delete();
        }
    }
    @SuppressWarnings("unchecked") Enumeration<Logger> e = LogManager.getCurrentLoggers();
    while (e.hasMoreElements()) {
        Logger lgr = e.nextElement();
        Appender apndr = lgr.getAppender(oap.getName());
        if (apndr != null) {
            lgr.removeAppender(oap.getName());
            lgr.addAppender(nap);
        }
    }
}
Also used : DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) Appender(org.apache.log4j.Appender) IOException(java.io.IOException) CoreVoltLogger(org.voltcore.logging.VoltLogger.CoreVoltLogger) Logger(org.apache.log4j.Logger) File(java.io.File) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender)

Aggregations

DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)6 IOException (java.io.IOException)4 File (java.io.File)3 PatternLayout (org.apache.log4j.PatternLayout)3 Appender (org.apache.log4j.Appender)2 FileAppender (org.apache.log4j.FileAppender)2 Logger (org.apache.log4j.Logger)2 SizeRollingFileAppender (com.axway.ats.agentapp.standalone.log.appenders.SizeRollingFileAppender)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Field (java.lang.reflect.Field)1 SocketException (java.net.SocketException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Level (org.apache.log4j.Level)1 OnlyOnceErrorHandler (org.apache.log4j.helpers.OnlyOnceErrorHandler)1 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)1 JSONException (org.json_voltpatches.JSONException)1 CoreVoltLogger (org.voltcore.logging.VoltLogger.CoreVoltLogger)1 SettingsException (org.voltdb.settings.SettingsException)1