Java Code Examples for org.apache.log4j.FileAppender#activateOptions()

The following examples show how to use org.apache.log4j.FileAppender#activateOptions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActiveActor.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
private void addHostToFilename(FileAppender fileApp) {
    String hostID = Math.abs(UniqueID.getCurrentVMID().hashCode() % 100000) + "-" +
                    ProActiveInet.getInstance().getHostname();
    String fileName = fileApp.getFile();
    File filePath = new File(fileName);
    String hostFileName;
    String name = filePath.getName();
    String pathToFile = filePath.getParent();
    int point = name.indexOf('.');
    if (point == -1) {
        hostFileName = fileName + "-" + hostID;
    } else {
        String extension = name.substring(point + 1);
        String nameNoExtension = name.substring(0, point);
        hostFileName = (pathToFile != null ? pathToFile : "") + File.separator + nameNoExtension + "-" + hostID +
                       "." + extension;
    }
    System.out.println("New output file:" + hostFileName);
    fileApp.setFile(hostFileName);
    fileApp.activateOptions();
}
 
Example 2
Source File: AbstractRipper.java    From ripme with MIT License 5 votes vote down vote up
/**
 * Sets ripper's:
 *      Working directory
 *      Logger (for debugging)
 *      FileAppender
 *      Threadpool
 * @throws IOException 
 *      Always be prepared.
 */
public void setup() throws IOException {
    setWorkingDir(this.url);
    Logger rootLogger = Logger.getRootLogger();
    FileAppender fa = (FileAppender) rootLogger.getAppender("FILE");
    if (fa != null) {
        fa.setFile(this.workingDir + File.separator + "log.txt");
        fa.activateOptions();
    }

    this.threadPool = new DownloadThreadPool();
}
 
Example 3
Source File: AbstractRipper.java    From ripme with MIT License 5 votes vote down vote up
/**
 * Notifies observers and updates state if all files have been ripped.
 */
void checkIfComplete() {
    if (observer == null) {
        LOGGER.debug("observer is null");
        return;
    }

    if (!completed) {
        completed = true;
        LOGGER.info("   Rip completed!");

        RipStatusComplete rsc = new RipStatusComplete(workingDir, getCount());
        RipStatusMessage msg = new RipStatusMessage(STATUS.RIP_COMPLETE, rsc);
        observer.update(this, msg);

        Logger rootLogger = Logger.getRootLogger();
        FileAppender fa = (FileAppender) rootLogger.getAppender("FILE");
        if (fa != null) {
            LOGGER.debug("Changing log file back to 'ripme.log'");
            fa.setFile("ripme.log");
            fa.activateOptions();
        }
        if (Utils.getConfigBoolean("urls_only.save", false)) {
            String urlFile = this.workingDir + File.separator + "urls.txt";
            try {
                Desktop.getDesktop().open(new File(urlFile));
            } catch (IOException e) {
                LOGGER.warn("Error while opening " + urlFile, e);
            }
        }
    }
}
 
Example 4
Source File: LoggerInit.java    From diamond with Apache License 2.0 5 votes vote down vote up
private static void setFileAppender(FileAppender bizFileAppender, String logName) {
    FileAppender fileAppender = getFileAppender(Logger.getLogger(logName));
    String bizLogDir = new File(bizFileAppender.getFile()).getParent();

    File newLogFile = new File(bizLogDir, fileAppender.getFile());

    fileAppender.setFile(newLogFile.getAbsolutePath());
    fileAppender.activateOptions(); // 很重要,否则原有日志内容会被清空
    log.warn("成功为" + logName + "添加Appender. 输出路径:" + newLogFile.getAbsolutePath());
}
 
Example 5
Source File: LoggerInit.java    From diamond with Apache License 2.0 5 votes vote down vote up
private static void setFileAppender(FileAppender bizFileAppender, String logName) {
    FileAppender fileAppender = getFileAppender(Logger.getLogger(logName));
    String bizLogDir = new File(bizFileAppender.getFile()).getParent();

    File newLogFile = new File(bizLogDir, fileAppender.getFile());

    fileAppender.setFile(newLogFile.getAbsolutePath());
    fileAppender.activateOptions(); // 很重要,否则原有日志内容会被清空
    log.warn("成功为" + logName + "添加Appender. 输出路径:" + newLogFile.getAbsolutePath());
}
 
Example 6
Source File: Log4JLogger.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public void updateFilePath(IgniteClosure<String, String> filePathClos) {
    A.notNull(filePathClos, "filePathClos");

    for (FileAppender a : fileAppenders) {
        if (a instanceof Log4jFileAware) {
            ((Log4jFileAware)a).updateFilePath(filePathClos);

            a.activateOptions();
        }
    }
}
 
Example 7
Source File: GridTestLog4jLogger.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public void setNodeId(UUID nodeId) {
    A.notNull(nodeId, "nodeId");

    this.nodeId = nodeId;

    for (FileAppender a : fileAppenders) {
        if (a instanceof LoggerNodeIdAware) {
            ((LoggerNodeIdAware)a).setNodeId(nodeId);

            a.activateOptions();
        }
    }
}
 
Example 8
Source File: CoasterService.java    From swift-k with Apache License 2.0 5 votes vote down vote up
public static void configureLogName() {
    FileAppender fa = (FileAppender) getFileAppender();
    if (fa != null) {
        fa.setFile(Bootstrap.LOG_DIR + File.separator + makeLogFileName());
        fa.activateOptions();

        AsyncAppender aa = new AsyncAppender();
        aa.addAppender(fa);
        replaceAppender(fa, aa);
    }
}
 
Example 9
Source File: AstorMain.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setupLogging() throws IOException {

		String patternLayout = "";
		if (ConfigurationProperties.getPropertyBool("disablelog")) {
			patternLayout = "%m%n";
		} else {
			patternLayout = ConfigurationProperties.getProperty("logpatternlayout");
		}

		Logger.getRootLogger().getLoggerRepository().resetConfiguration();
		ConsoleAppender console = new ConsoleAppender();
		console.setLayout(new PatternLayout(patternLayout));
		console.activateOptions();
		Logger.getRootLogger().addAppender(console);

		String loglevelSelected = ConfigurationProperties.properties.getProperty("loglevel");
		if (loglevelSelected != null)
			LogManager.getRootLogger().setLevel(Level.toLevel(loglevelSelected));

		if (ConfigurationProperties.hasProperty("logfilepath")) {
			FileAppender fa = new FileAppender();
			String filePath = ConfigurationProperties.getProperty("logfilepath");
			File fileLog = new File(filePath);
			if (!fileLog.exists()) {
				fileLog.getParentFile().mkdirs();
				fileLog.createNewFile();
			}

			fa.setName("FileLogger");
			fa.setFile(fileLog.getAbsolutePath());
			fa.setLayout(new PatternLayout(patternLayout));
			fa.setThreshold(LogManager.getRootLogger().getLevel());
			fa.setAppend(true);
			fa.activateOptions();
			Logger.getRootLogger().addAppender(fa);
			this.log.info("Log file at: " + filePath);
		}
	}
 
Example 10
Source File: Log4jContainer.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender) a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (!path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 11
Source File: Log4jContainer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 12
Source File: Log4jContainer.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 13
Source File: Log4jContainer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: Log4jContainer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 15
Source File: Loader.java    From swift-k with Apache License 2.0 4 votes vote down vote up
protected static String setupLogging(ArgumentParser ap, SwiftConfig config, String projectName,
        String runID) throws IOException {
    String logfile;
    if (ap.isPresent(ARG_LOGFILE)) {
        logfile = ap.getStringValue(ARG_LOGFILE);
    }
    else {
        logfile = projectName + "-" + runID + ".log";
    }
    
    config.setProperty("logfile", logfile);
    
    File f = new File(logfile);

    FileAppender fa = (FileAppender) getAppender(FileAppender.class);
    AsyncAppender aa = new AsyncAppender();
    aa.addAppender(fa);
    
    replaceAppender(fa, aa);
    if (fa == null) {
        logger.warn("Failed to configure log file name");
    }
    else {
        fa.setFile(f.getAbsolutePath());
        if (fa instanceof LazyFileAppender) {
            ((LazyFileAppender) fa).fileNameConfigured();
        }
        fa.activateOptions();
    }
    Level level = Level.WARN;
    if (ap.isPresent(ARG_VERBOSE)) {
        level = Level.INFO;
    }
    if (ap.isPresent(ARG_DEBUG)) {
        level = Level.DEBUG;
    }
    ConsoleAppender ca = (ConsoleAppender) getAppender(ConsoleAppender.class);
    if (ca == null) {
        logger.warn("Failed to configure console log level");
    }
    else {
        ca.setThreshold(level);
        ca.activateOptions();
    }
    Logger.getLogger(Log.class).setLevel(Level.INFO);
    if (ap.isPresent(ARG_UI) && !"summary".equals(ap.getStringValue(ARG_UI))) {
        String ui = ap.getStringValue(ARG_UI);
        if ("none".equals(ui)) {
            // config should be loaded now
            SwiftConfig.getDefault().set(SwiftConfig.Key.TICKER_ENABLED, false);
        }
        else {
            ma = new MonitorAppender(projectName, ui);
            Logger.getRootLogger().addAppender(ma);
            Logger.getLogger(Log.class).setLevel(Level.DEBUG);
            Logger.getLogger(AbstractGridNode.class).setLevel(Level.DEBUG);
            Logger.getLogger(Execute.class).setLevel(Level.DEBUG);
            Logger.getLogger(SwiftExecutor.class).setLevel(Level.INFO);
            Logger.getLogger(WeightedHostScoreScheduler.class).setLevel(
                Level.INFO);
        }
        if ("TUI".equals(ui) || "ANSI".equals(ui)) {
            ca.setThreshold(Level.FATAL);
        }
    }
    else if (ap.isPresent(ARG_MINIMAL_LOGGING)) {
        Logger.getLogger("swift").setLevel(Level.WARN);
        Logger.getRootLogger().setLevel(Level.WARN);
    }
    else if (ap.isPresent(ARG_REDUCED_LOGGING)) {
        Logger.getLogger(AbstractDataNode.class).setLevel(Level.WARN);
        Logger.getLogger(New.class).setLevel(Level.WARN);
        Logger.getLogger("org.globus.cog.karajan.workflow.service").setLevel(Level.WARN);
        Logger.getLogger("swift").setLevel(Level.INFO);
    }
    return logfile;
}