Search in sources :

Example 31 with PatternLayoutEncoder

use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project jlineup by otto-de.

the class Util method logToFile.

static void logToFile(String workingDir) {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setContext(lc);
    ple.start();
    FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
    fileAppender.setFile(workingDir + "/jlineup.log");
    fileAppender.setEncoder(ple);
    fileAppender.setContext(lc);
    fileAppender.start();
    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
    logger.addAppender(fileAppender);
    logger.setLevel(Level.DEBUG);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Logger(org.slf4j.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 32 with PatternLayoutEncoder

use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project pravega by pravega.

the class SelfTestRunner method setupLogging.

private static void setupLogging(TestConfig testConfig) {
    val logFile = new java.io.File(testConfig.getTestLogPath());
    if (logFile.delete()) {
        TestLogger.log("Main", "Deleted log file %s.", logFile.getAbsolutePath());
    }
    // Configure slf4j to not log anything (console or whatever). This interferes with the console interaction.
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.getLoggerList().get(0).detachAndStopAllAppenders();
    val fa = new FileAppender<ILoggingEvent>();
    fa.setContext(context);
    fa.setName("selftest");
    fa.setFile(logFile.getAbsolutePath());
    val encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern("%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level - %msg%n");
    encoder.start();
    fa.setEncoder(encoder);
    fa.start();
    context.getLoggerList().get(0).addAppender(fa);
    context.getLoggerList().get(0).setLevel(Level.INFO);
// context.reset();
}
Also used : lombok.val(lombok.val) FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) File(java.io.File) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 33 with PatternLayoutEncoder

use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project engineblock by engineblock.

the class ScenarioLogger method start.

public ScenarioLogger start() {
    if (!loggerDir.exists()) {
        if (!loggerDir.mkdirs()) {
            throw new RuntimeException("Unable to create logger directory:" + loggerDir.getPath());
        }
    }
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    // List<LoggerContextListener> copyOfListenerList = loggerContext.getCopyOfListenerList();
    ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<>();
    ca.setContext(loggerContext);
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    // ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setContext(loggerContext);
    ple.start();
    String scenarioLog = loggerDir.getPath() + File.separator + scenario.getName() + ".log";
    scenarioLog = scenarioLog.replaceAll("\\s", "_");
    FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
    fileAppender.setFile(scenarioLog);
    fileAppender.setEncoder(ple);
    fileAppender.setContext(loggerContext);
    System.err.println("Logging to " + scenarioLog);
    fileAppender.start();
    Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
    logger.addAppender(fileAppender);
    logger.setLevel(Level.INFO);
    logger.setAdditive(true);
    /* set to true if root should log too */
    purgeOldFiles(logger);
    return this;
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 34 with PatternLayoutEncoder

use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project engineblock by engineblock.

the class ConsoleLogging method enableConsoleLogging.

public static void enableConsoleLogging(Level level, String loggingPattern) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    // List<LoggerContextListener> copyOfListenerList = loggerContext.getCopyOfListenerList();
    ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<>();
    ca.setContext(loggerContext);
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern(loggingPattern);
    ple.setContext(loggerContext);
    ple.start();
    ca.setEncoder(ple);
    LevelFilter levelFilter = new LevelFilter(level);
    levelFilter.start();
    ca.addFilter(levelFilter);
    ca.start();
    Logger root = loggerContext.getLogger("ROOT");
    root.addAppender(ca);
    root.setLevel(Level.TRACE);
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)34 LoggerContext (ch.qos.logback.classic.LoggerContext)24 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)23 Logger (ch.qos.logback.classic.Logger)14 FileAppender (ch.qos.logback.core.FileAppender)11 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)10 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)9 Test (org.junit.Test)9 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)8 File (java.io.File)7 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)3 Logger (org.slf4j.Logger)3 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)2 Appender (ch.qos.logback.core.Appender)2 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 LogcatAppender (ch.qos.logback.classic.android.LogcatAppender)1 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)1 BroadcastSession (com.cgbystrom.sockjs.test.BroadcastSession)1 JmxReporter (com.codahale.metrics.JmxReporter)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1