Search in sources :

Example 16 with PatternLayout

use of org.apache.log4j.PatternLayout 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{ISO8601}][%-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 17 with PatternLayout

use of org.apache.log4j.PatternLayout 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{ISO8601}][%-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{ISO8601}][%-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 18 with PatternLayout

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

the class GridTestLog4jLogger method createConsoleAppender.

/**
 * Creates console appender with some reasonable default logging settings.
 *
 * @param maxLevel Max logging level.
 * @return New console appender.
 */
private Appender createConsoleAppender(Level maxLevel) {
    String fmt = "[%d{ISO8601}][%-5p][%t][%c{1}] %m%n";
    // Configure output that should go to System.out
    Appender app = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
    LevelRangeFilter lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.TRACE);
    lvlFilter.setLevelMax(maxLevel);
    app.addFilter(lvlFilter);
    return app;
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter)

Example 19 with PatternLayout

use of org.apache.log4j.PatternLayout in project leopard by tanhaichao.

the class LogDaoLog4jImpl method getAppender.

protected Appender getAppender(String filename, org.apache.log4j.Level log4jLevel, boolean bufferedIO) {
    String key = filename + ":" + log4jLevel.toInt() + ":" + bufferedIO;
    DailyAutoRollingFileAppender appender = appenderMap.get(key);
    if (appender != null) {
        return appender;
    }
    String conversionPattern = "%d{yyyy-MM-dd HH:mm:ss} - [%c] - %m%n";
    org.apache.log4j.PatternLayout layout = new PatternLayout();
    layout.setConversionPattern(conversionPattern);
    appender = new DailyAutoRollingFileAppender();
    // DailyRollingFileAppender newAppender = new DailyRollingFileAppender();
    appender.setThreshold(log4jLevel);
    appender.setLayout(layout);
    appender.setDatePattern(".yyyyMMdd");
    appender.setFile(filename);
    // 8192
    appender.setBufferSize(10);
    appender.setBufferedIO(bufferedIO);
    appender.activateOptions();
    appenderMap.put(key, appender);
    return appender;
}
Also used : PatternLayout(org.apache.log4j.PatternLayout) PatternLayout(org.apache.log4j.PatternLayout)

Example 20 with PatternLayout

use of org.apache.log4j.PatternLayout in project metron by apache.

the class UnitTestHelper method verboseLogging.

public static void verboseLogging(String pattern, Level level) {
    // create appender
    ConsoleAppender console = new ConsoleAppender();
    // configure the appender
    console.setLayout(new PatternLayout(pattern));
    console.setThreshold(level);
    console.activateOptions();
    // add appender to any Logger (here is root)
    Logger.getRootLogger().addAppender(console);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout)

Aggregations

PatternLayout (org.apache.log4j.PatternLayout)106 ConsoleAppender (org.apache.log4j.ConsoleAppender)49 Logger (org.apache.log4j.Logger)30 IOException (java.io.IOException)20 Appender (org.apache.log4j.Appender)20 FileAppender (org.apache.log4j.FileAppender)17 File (java.io.File)14 Layout (org.apache.log4j.Layout)14 RollingFileAppender (org.apache.log4j.RollingFileAppender)12 Test (org.junit.Test)12 WriterAppender (org.apache.log4j.WriterAppender)9 CommandLineParser (org.apache.commons.cli.CommandLineParser)7 GnuParser (org.apache.commons.cli.GnuParser)7 ParseException (org.apache.commons.cli.ParseException)7 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)7 FileInputStream (java.io.FileInputStream)6 Properties (java.util.Properties)6 CommandLine (org.apache.commons.cli.CommandLine)6 Option (org.apache.commons.cli.Option)6 Options (org.apache.commons.cli.Options)6