Search in sources :

Example 1 with FileAppender

use of ch.qos.logback.core.FileAppender in project sonarqube by SonarSource.

the class LogbackHelperTest method createRollingPolicy_defaults.

@Test
public void createRollingPolicy_defaults() {
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    FileAppender appender = policy.createAppender("SONAR_FILE");
    assertThat(appender).isInstanceOf(RollingFileAppender.class);
    // max 5 daily files
    RollingFileAppender fileAppender = (RollingFileAppender) appender;
    TimeBasedRollingPolicy triggeringPolicy = (TimeBasedRollingPolicy) fileAppender.getTriggeringPolicy();
    assertThat(triggeringPolicy.getMaxHistory()).isEqualTo(7);
    assertThat(triggeringPolicy.getFileNamePattern()).endsWith("sonar.%d{yyyy-MM-dd}.log");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Example 2 with FileAppender

use of ch.qos.logback.core.FileAppender in project sonarqube by SonarSource.

the class WebServerProcessLoggingTest method log_to_web_file.

@Test
public void log_to_web_file() {
    LoggerContext ctx = underTest.configure(props);
    Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
    Appender<ILoggingEvent> appender = root.getAppender("file_web");
    assertThat(appender).isInstanceOf(FileAppender.class);
    FileAppender fileAppender = (FileAppender) appender;
    assertThat(fileAppender.getFile()).isEqualTo(new File(logDir, "web.log").getAbsolutePath());
    assertThat(fileAppender.getEncoder()).isInstanceOf(PatternLayoutEncoder.class);
    PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
    assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level web[%X{HTTP_REQUEST_ID}][%logger{20}] %msg%n");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.Test)

Example 3 with FileAppender

use of ch.qos.logback.core.FileAppender in project openhab1-addons by openhab.

the class LoggingPersistenceService method createNewAppender.

protected FileAppender<ILoggingEvent> createNewAppender(String alias) {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern(pattern);
    encoder.start();
    FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
    appender.setAppend(true);
    appender.setFile(LOG_FOLDER + File.separator + alias + LOG_FILEEXT);
    appender.setEncoder(encoder);
    appender.setContext(context);
    appender.start();
    return appender;
}
Also used : 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)

Example 4 with FileAppender

use of ch.qos.logback.core.FileAppender in project sling by apache.

the class SlingConfigurationPrinter method dumpLogFileSummary.

private void dumpLogFileSummary(PrintWriter pw, Collection<Appender<ILoggingEvent>> appenders) {
    pw.println("Summary");
    pw.println("=======");
    pw.println();
    int counter = 0;
    final String rootDir = logbackManager.getRootDir();
    for (Appender<ILoggingEvent> appender : appenders) {
        if (appender instanceof FileAppender) {
            File file = new File(((FileAppender) appender).getFile());
            final File dir = file.getParentFile();
            final String baseName = file.getName();
            String absolutePath = dir.getAbsolutePath();
            String displayName = ((FileAppender) appender).getFile();
            if (absolutePath.startsWith(rootDir)) {
                displayName = baseName;
            }
            pw.printf("%d. %s %n", ++counter, displayName);
            final File[] files = getRotatedFiles((FileAppender) appender, -1);
            for (File f : files) {
                pw.printf("  - %s, %s, %s %n", f.getName(), humanReadableByteCount(f.length()), getModifiedDate(f));
            }
        }
    }
    pw.println();
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) File(java.io.File)

Example 5 with FileAppender

use of ch.qos.logback.core.FileAppender in project sling by apache.

the class SlingConfigurationPrinter method printConfiguration.

/**
     * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
     */
@SuppressWarnings("UnusedDeclaration")
public void printConfiguration(PrintWriter printWriter, String mode) {
    LogbackManager.LoggerStateContext ctx = logbackManager.determineLoggerState();
    int numOfLines = getNumOfLines();
    Tailer tailer = new Tailer(printWriter, numOfLines);
    dumpLogFileSummary(printWriter, ctx.getAllAppenders());
    if (!MODE_ZIP.equals(mode)) {
        for (Appender<ILoggingEvent> appender : ctx.getAllAppenders()) {
            if (appender instanceof FileAppender) {
                final File file = new File(((FileAppender) appender).getFile());
                if (file.exists()) {
                    printWriter.print("Log file ");
                    printWriter.println(file.getAbsolutePath());
                    printWriter.println("--------------------------------------------------");
                    if (numOfLines < 0) {
                        includeWholeFile(printWriter, file);
                    } else {
                        try {
                            tailer.tail(file);
                        } catch (IOException e) {
                            logbackManager.getLogConfigManager().internalFailure("Error occurred " + "while processing log file " + file, e);
                        }
                    }
                    printWriter.println();
                }
            }
        }
    }
    dumpLogbackStatus(logbackManager, printWriter);
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) IOException(java.io.IOException) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) File(java.io.File)

Aggregations

FileAppender (ch.qos.logback.core.FileAppender)44 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)31 LoggerContext (ch.qos.logback.classic.LoggerContext)25 File (java.io.File)22 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)19 Logger (ch.qos.logback.classic.Logger)15 Appender (ch.qos.logback.core.Appender)14 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)12 Test (org.junit.Test)8 Path (java.nio.file.Path)6 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)5 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)5 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)4 FileSize (ch.qos.logback.core.util.FileSize)4 IOException (java.io.IOException)4 Field (java.lang.reflect.Field)4 SyslogAppender (ch.qos.logback.classic.net.SyslogAppender)3 SizeAndTimeBasedRollingPolicy (ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)3 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)3