Search in sources :

Example 51 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 52 with PatternLayoutEncoder

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

the class LogbackContainer method doInitializer.

/**
 * Initializer logback
 *
 * @param file
 * @param level
 * @param maxHistory
 */
private void doInitializer(String file, String level, int maxHistory) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.detachAndStopAllAppenders();
    // appender
    RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
    fileAppender.setContext(loggerContext);
    fileAppender.setName("application");
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    // policy
    TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
    policy.setContext(loggerContext);
    policy.setMaxHistory(maxHistory);
    policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
    policy.setParent(fileAppender);
    policy.start();
    fileAppender.setRollingPolicy(policy);
    // encoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
    encoder.start();
    fileAppender.setEncoder(encoder);
    fileAppender.start();
    rootLogger.addAppender(fileAppender);
    rootLogger.setLevel(Level.toLevel(level));
    rootLogger.setAdditive(false);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 53 with PatternLayoutEncoder

use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project coffeenet-starter by coffeenet.

the class CoffeeNetLoggingAutoConfiguration method rollingFileAppender.

private RollingFileAppender<ILoggingEvent> rollingFileAppender(LoggerContext loggerContext) {
    RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
    rfAppender.setName(COFFEENET_FILE_APPENDER);
    rfAppender.setContext(loggerContext);
    rfAppender.setFile(coffeeNetLoggingFileProperties.getFile());
    // TimeBasedRollingPolicy
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(rfAppender);
    rollingPolicy.setFileNamePattern(coffeeNetLoggingFileProperties.getFileNamePattern());
    rollingPolicy.setMaxHistory(coffeeNetLoggingFileProperties.getMaxHistory());
    rollingPolicy.start();
    rfAppender.setRollingPolicy(rollingPolicy);
    // PatternLayoutEncoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern(coffeeNetLoggingFileProperties.getPattern());
    encoder.start();
    rfAppender.setEncoder(encoder);
    rfAppender.start();
    return rfAppender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 54 with PatternLayoutEncoder

use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project spring-boot by spring-projects.

the class LogbackConfigurationTests method filePatternCanBeOverridden.

@Test
void filePatternCanBeOverridden() throws JoranException {
    JoranConfigurator configurator = new JoranConfigurator();
    LoggerContext context = new LoggerContext();
    configurator.setContext(context);
    configurator.doConfigure(new File("src/test/resources/custom-file-log-pattern.xml"));
    Appender<ILoggingEvent> appender = context.getLogger("ROOT").getAppender("FILE");
    assertThat(appender).isInstanceOf(FileAppender.class);
    Encoder<?> encoder = ((FileAppender<?>) appender).getEncoder();
    assertThat(encoder).isInstanceOf(PatternLayoutEncoder.class);
    assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("bar");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 55 with PatternLayoutEncoder

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

the class AbstractLocalDevBimServerStarter method fixLogging.

/**
 * Add a file appender to every logger we can find (the loggers should already have been configured via logback.xml)
 *
 * @throws IOException
 */
private void fixLogging(BimServerConfig config) throws IOException {
    Path logFolder = config.getHomeDir().resolve("logs");
    if (!Files.isDirectory(logFolder)) {
        Files.createDirectories(logFolder);
    }
    Path file = logFolder.resolve("bimserver.log");
    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<ILoggingEvent>();
    String filename = file.toAbsolutePath().toString();
    if (lc instanceof LoggerContext) {
        if (!lc.isStarted()) {
            lc.start();
        }
    }
    System.out.println("Logging to " + filename);
    fileAppender.setFile(filename);
    fileAppender.setEncoder(ple);
    fileAppender.setContext(lc);
    fileAppender.start();
    for (ch.qos.logback.classic.Logger log : lc.getLoggerList()) {
        if (log.getLevel() != null) {
            log.addAppender(fileAppender);
        }
    }
}
Also used : Path(java.nio.file.Path) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)65 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)44 LoggerContext (ch.qos.logback.classic.LoggerContext)37 FileAppender (ch.qos.logback.core.FileAppender)21 Logger (ch.qos.logback.classic.Logger)20 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)20 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)17 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)13 File (java.io.File)10 Test (org.junit.Test)10 Logger (org.slf4j.Logger)6 Appender (ch.qos.logback.core.Appender)5 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)5 Path (java.nio.file.Path)5 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)4 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)3 SyslogAppender (ch.qos.logback.classic.net.SyslogAppender)3 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)3 Encoder (ch.qos.logback.core.encoder.Encoder)2