Search in sources :

Example 21 with FileAppender

use of org.apache.log4j.FileAppender in project apex-core by apache.

the class LoggerUtil method getFileAppender.

private static FileAppender getFileAppender(Logger logger) {
    Enumeration e = logger.getAllAppenders();
    FileAppender fileAppender = null;
    while (e.hasMoreElements()) {
        Object appender = e.nextElement();
        if (appender instanceof FileAppender) {
            if (fileAppender == null) {
                fileAppender = (FileAppender) appender;
            } else {
                // skip fetching log file information if we have multiple file Appenders
                fileAppender = null;
                break;
            }
        }
    }
    /*
     * We should return log file information only if,
     * we have single file Appender, the logging level of appender is set to level Error or above and immediateFlush is set to true.
     * In future we should be able to enhance this feature to support multiple file appenders.
     */
    if (fileAppender == null || !fileAppender.getImmediateFlush() || !fileAppender.isAsSevereAsThreshold(Level.ERROR)) {
        LoggerUtil.logger.warn("Log information is unavailable. To enable log information log4j/logging should be configured with single FileAppender that has immediateFlush set to true and log level set to ERROR or greater.");
        return null;
    }
    return fileAppender;
}
Also used : FileAppender(org.apache.log4j.FileAppender) Enumeration(java.util.Enumeration)

Example 22 with FileAppender

use of org.apache.log4j.FileAppender in project symmetric-ds by JumpMind.

the class AbstractCommandLauncher method configureLogging.

protected void configureLogging(CommandLine line) throws MalformedURLException {
    /* Optionally remove existing handlers attached to j.u.l root logger */
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    /*
         * Add SLF4JBridgeHandler to j.u.l's root logger, should be done once
         * during the initialization phase of your application
         */
    SLF4JBridgeHandler.install();
    URL log4jUrl = new URL(System.getProperty("log4j.configuration", "file:" + AppUtils.getSymHome() + "/conf/log4j-blank.xml"));
    File log4jFile = new File(new File(log4jUrl.getFile()).getParent(), "log4j.xml");
    if (line.hasOption(OPTION_DEBUG)) {
        log4jFile = new File(log4jFile.getParent(), "log4j-debug.xml");
    }
    if (log4jFile.exists()) {
        DOMConfigurator.configure(log4jFile.getAbsolutePath());
    }
    if (line.hasOption(OPTION_VERBOSE_CONSOLE)) {
        Appender consoleAppender = org.apache.log4j.Logger.getRootLogger().getAppender("CONSOLE");
        if (consoleAppender != null) {
            Layout layout = consoleAppender.getLayout();
            if (layout instanceof PatternLayout) {
                ((PatternLayout) layout).setConversionPattern("%d %-5p [%c{2}] [%t] %m%n");
            }
        }
    }
    if (line.hasOption(OPTION_NO_LOG_CONSOLE)) {
        org.apache.log4j.Logger.getRootLogger().removeAppender("CONSOLE");
    }
    if (line.hasOption(OPTION_NO_LOG_FILE)) {
        org.apache.log4j.Logger.getRootLogger().removeAppender("ROLLING");
    } else {
        Appender appender = org.apache.log4j.Logger.getRootLogger().getAppender("ROLLING");
        if (appender instanceof FileAppender) {
            FileAppender fileAppender = (FileAppender) appender;
            if (line.hasOption(OPTION_PROPERTIES_FILE)) {
                File file = new File(line.getOptionValue(OPTION_PROPERTIES_FILE));
                String name = file.getName();
                int index = name.lastIndexOf(".");
                if (index > 0) {
                    name = name.substring(0, index);
                }
                fileAppender.setFile(fileAppender.getFile().replace("symmetric.log", name + ".log"));
                fileAppender.activateOptions();
            }
            System.err.println(String.format("Log output will be written to %s", fileAppender.getFile()));
        }
    }
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) PatternLayout(org.apache.log4j.PatternLayout) File(java.io.File) URL(java.net.URL)

Example 23 with FileAppender

use of org.apache.log4j.FileAppender in project incubator-atlas by apache.

the class FailedMessagesLogger method getRootLoggerDirectory.

/**
 * Get the root logger file location under which the failed log messages will be written.
 *
 * Since this class is used in Hooks which run within JVMs of other components like Hive,
 * we want to write the failed messages file under the same location as where logs from
 * the host component are saved. This method attempts to get such a location from the
 * root logger's appenders. It will work only if at least one of the appenders is a {@link FileAppender}
 *
 * @return directory under which host component's logs are stored.
 */
private String getRootLoggerDirectory() {
    String rootLoggerDirectory = null;
    org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
    Enumeration allAppenders = rootLogger.getAllAppenders();
    if (allAppenders != null) {
        while (allAppenders.hasMoreElements()) {
            Appender appender = (Appender) allAppenders.nextElement();
            if (appender instanceof FileAppender) {
                FileAppender fileAppender = (FileAppender) appender;
                String rootLoggerFile = fileAppender.getFile();
                rootLoggerDirectory = new File(rootLoggerFile).getParent();
                break;
            }
        }
    }
    return rootLoggerDirectory;
}
Also used : DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) FileAppender(org.apache.log4j.FileAppender) Logger(org.apache.log4j.Logger) Enumeration(java.util.Enumeration) File(java.io.File)

Example 24 with FileAppender

use of org.apache.log4j.FileAppender in project dubbo by alibaba.

the class Log4jContainer method start.

@Override
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigurationUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigurationUtils.getProperty(LOG4J_LEVEL);
        if (StringUtils.isEmpty(level)) {
            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 = ConfigurationUtils.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();
                        }
                    }
                }
            }
        }
    }
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender) Properties(java.util.Properties)

Example 25 with FileAppender

use of org.apache.log4j.FileAppender in project gocd by gocd.

the class DefaultPluginLoggingService method initializeLoggerForPluginId.

private void initializeLoggerForPluginId(String pluginId) {
    if (alreadyInitialized(pluginId)) {
        return;
    }
    synchronized (pluginId.intern()) {
        if (alreadyInitialized(pluginId)) {
            return;
        }
        FileAppender pluginAppender = getAppender(pluginId);
        Logger logger = Logger.getLogger(PLUGIN_LOGGER_PREFIX + "." + pluginId);
        logger.setAdditivity(false);
        logger.setLevel(systemEnvironment.pluginLoggingLevel(pluginId));
        logger.addAppender(pluginAppender);
        loggingServiceLogger.debug("Plugin with ID: " + pluginId + " will log to: " + pluginAppender.getFile());
    }
}
Also used : FileAppender(org.apache.log4j.FileAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Logger(org.apache.log4j.Logger)

Aggregations

FileAppender (org.apache.log4j.FileAppender)47 Appender (org.apache.log4j.Appender)18 File (java.io.File)15 IOException (java.io.IOException)14 PatternLayout (org.apache.log4j.PatternLayout)14 Logger (org.apache.log4j.Logger)12 ConsoleAppender (org.apache.log4j.ConsoleAppender)8 Layout (org.apache.log4j.Layout)5 Enumeration (java.util.Enumeration)4 Properties (java.util.Properties)4 FileNotFoundException (java.io.FileNotFoundException)3 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)3 RollingFileAppender (org.apache.log4j.RollingFileAppender)3 SimpleLayout (org.apache.log4j.SimpleLayout)3 InputStream (java.io.InputStream)2 HTMLLayout (org.apache.log4j.HTMLLayout)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 LoggerChannel (com.biglybt.pif.logging.LoggerChannel)1 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)1 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)1