Search in sources :

Example 6 with RollingFileAppender

use of org.apache.log4j.RollingFileAppender in project ignite by apache.

the class GridAbstractTest method resetLog4j.

/**
     * Resets log4j programmatically.
     *
     * @param log4jLevel Level.
     * @param logToFile If {@code true}, then log to file under "work/log" folder.
     * @param cat Category.
     * @param cats Additional categories.
     */
@SuppressWarnings({ "deprecation" })
protected void resetLog4j(Level log4jLevel, boolean logToFile, String cat, String... cats) throws IgniteCheckedException {
    for (String c : F.concat(false, cat, F.asList(cats))) Logger.getLogger(c).setLevel(log4jLevel);
    if (logToFile) {
        Logger log4j = Logger.getRootLogger();
        log4j.removeAllAppenders();
        // Console appender.
        ConsoleAppender c = new ConsoleAppender();
        c.setName("CONSOLE_ERR");
        c.setTarget("System.err");
        c.setThreshold(Priority.WARN);
        c.setLayout(new PatternLayout("[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"));
        c.activateOptions();
        log4j.addAppender(c);
        // File appender.
        RollingFileAppender file = new RollingFileAppender();
        file.setName("FILE");
        file.setThreshold(log4jLevel);
        file.setFile(home() + "/work/log/ignite.log");
        file.setAppend(false);
        file.setMaxFileSize("10MB");
        file.setMaxBackupIndex(10);
        file.setLayout(new PatternLayout("[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"));
        file.activateOptions();
        log4j.addAppender(file);
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) NullLogger(org.apache.ignite.logger.NullLogger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.log4j.Logger)

Example 7 with RollingFileAppender

use of org.apache.log4j.RollingFileAppender in project ignite by apache.

the class GridCacheAbstractLoadTest method initLogger.

/**
     * Initializes logger.
     *
     * @param log Log file name.
     * @return Logger.
     * @throws IgniteCheckedException If file initialization failed.
     */
protected IgniteLogger initLogger(String log) throws IgniteCheckedException {
    Logger impl = Logger.getRootLogger();
    impl.removeAllAppenders();
    String fileName = U.getIgniteHome() + "/work/log/" + log;
    // Configure output that should go to System.out
    RollingFileAppender fileApp;
    String fmt = "[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n";
    try {
        fileApp = new RollingFileAppender(new PatternLayout(fmt), fileName);
        fileApp.setMaxBackupIndex(0);
        fileApp.setAppend(false);
        // fileApp.rollOver();
        fileApp.activateOptions();
    } catch (IOException e) {
        throw new IgniteCheckedException("Unable to initialize file appender.", e);
    }
    LevelRangeFilter lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.DEBUG);
    fileApp.addFilter(lvlFilter);
    impl.addAppender(fileApp);
    // Configure output that should go to System.out
    ConsoleAppender conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
    lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.DEBUG);
    lvlFilter.setLevelMax(Level.INFO);
    conApp.addFilter(lvlFilter);
    conApp.activateOptions();
    impl.addAppender(conApp);
    // Configure output that should go to System.err
    conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_ERR);
    conApp.setThreshold(Level.WARN);
    conApp.activateOptions();
    impl.addAppender(conApp);
    impl.setLevel(Level.INFO);
    return new GridTestLog4jLogger(false);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) IOException(java.io.IOException) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.log4j.Logger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)

Example 8 with RollingFileAppender

use of org.apache.log4j.RollingFileAppender in project ignite by apache.

the class GridSingleExecutionTest method initLogger.

/**
     * Initializes logger.
     *
     * @param log Log file name.
     * @return Logger.
     * @throws IgniteCheckedException If file initialization failed.
     */
private static IgniteLogger initLogger(String log) throws IgniteCheckedException {
    Logger impl = Logger.getRootLogger();
    impl.removeAllAppenders();
    String fileName = U.getIgniteHome() + "/work/log/" + log;
    // Configure output that should go to System.out
    RollingFileAppender fileApp;
    String fmt = "[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n";
    try {
        fileApp = new RollingFileAppender(new PatternLayout(fmt), fileName);
        fileApp.setMaxBackupIndex(0);
        fileApp.rollOver();
    } catch (IOException e) {
        throw new IgniteCheckedException("Unable to initialize file appender.", e);
    }
    LevelRangeFilter lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.DEBUG);
    fileApp.addFilter(lvlFilter);
    impl.addAppender(fileApp);
    // Configure output that should go to System.out
    ConsoleAppender conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
    lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.INFO);
    lvlFilter.setLevelMax(Level.INFO);
    conApp.addFilter(lvlFilter);
    impl.addAppender(conApp);
    // Configure output that should go to System.err
    conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_ERR);
    conApp.setThreshold(Level.WARN);
    impl.addAppender(conApp);
    impl.setLevel(Level.INFO);
    Logger.getLogger("org.apache.ignite").setLevel(Level.DEBUG);
    return new GridTestLog4jLogger(false);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) IOException(java.io.IOException) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.log4j.Logger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)

Example 9 with RollingFileAppender

use of org.apache.log4j.RollingFileAppender in project JMRI by JMRI.

the class Log4JUtil method startupInfo.

@SuppressWarnings("unchecked")
public static String startupInfo(String program) {
    log.info(jmriLog);
    Enumeration<org.apache.log4j.Logger> e = org.apache.log4j.Logger.getRootLogger().getAllAppenders();
    while (e.hasMoreElements()) {
        Appender a = (Appender) e.nextElement();
        if (a instanceof RollingFileAppender) {
            log.info("This log is appended to file: " + ((RollingFileAppender) a).getFile());
        } else if (a instanceof FileAppender) {
            log.info("This log is stored in file: " + ((FileAppender) a).getFile());
        }
    }
    return (program + " version " + jmri.Version.name() + " starts under Java " + System.getProperty("java.version", "<unknown>") + " on " + System.getProperty("os.name", "<unknown>") + " " + System.getProperty("os.arch", "<unknown>") + " v" + System.getProperty("os.version", "<unknown>") + " at " + (new java.util.Date()));
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) FileAppender(org.apache.log4j.FileAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Logger(org.slf4j.Logger)

Example 10 with RollingFileAppender

use of org.apache.log4j.RollingFileAppender in project dhis2-core by dhis2.

the class Log4JLogConfigInitializer method getRollingFileAppender.

/**
     * Returns a rolling file appender.
     * 
     * @param file the file to output to, including path and filename.
     */
private RollingFileAppender getRollingFileAppender(String file) {
    RollingFileAppender appender = new RollingFileAppender();
    appender.setThreshold(Level.INFO);
    appender.setFile(file);
    appender.setMaxFileSize(MAX_FILE_SIZE);
    appender.setLayout(PATTERN_LAYOUT);
    appender.activateOptions();
    return appender;
}
Also used : RollingFileAppender(org.apache.log4j.RollingFileAppender)

Aggregations

RollingFileAppender (org.apache.log4j.RollingFileAppender)16 PatternLayout (org.apache.log4j.PatternLayout)8 Logger (org.apache.log4j.Logger)6 File (java.io.File)4 IOException (java.io.IOException)4 IgniteLogger (org.apache.ignite.IgniteLogger)3 GridTestLog4jLogger (org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)3 ConsoleAppender (org.apache.log4j.ConsoleAppender)3 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 Appender (org.apache.log4j.Appender)2 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)2 Test (org.junit.Test)2 RandomAccessFile (java.io.RandomAccessFile)1 NullLogger (org.apache.ignite.logger.NullLogger)1 FileAppender (org.apache.log4j.FileAppender)1 BeforeClass (org.junit.BeforeClass)1 Logger (org.slf4j.Logger)1