Search in sources :

Example 21 with FileAppender

use of ch.qos.logback.core.FileAppender in project gocd by gocd.

the class DefaultPluginLoggingService method getCurrentLogDirectory.

String getCurrentLogDirectory() {
    try {
        FileAppender fileAppender = getGoServerLogFileAppender();
        String fileName = fileAppender.rawFileProperty();
        return new File(fileName).getAbsoluteFile().getParent();
    } catch (Exception e) {
        return ".";
    }
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) File(java.io.File)

Example 22 with FileAppender

use of ch.qos.logback.core.FileAppender in project gocd by gocd.

the class FileLocationProvider method asJson.

@Override
public Map<String, Object> asJson() {
    LinkedHashMap<String, Object> json = new LinkedHashMap<>();
    json.put("loc.config.dir", systemEnvironment.configDir().getAbsolutePath());
    List<Logger> loggers = LOGGER_CONTEXT.getLoggerList();
    Appender[] appenders = getAppenders(loggers);
    for (int i = 0; i < appenders.length; i++) {
        Appender appender = appenders[i];
        if (!isFileAppender(appender)) {
            continue;
        }
        FileAppender fileAppender = (FileAppender) appender;
        File logFile = new File(fileAppender.rawFileProperty());
        json.put("loc.log.root." + i, new File(logFile.getAbsolutePath()).getParent());
        json.put("loc.log.basename." + i, logFile.getName());
    }
    return json;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) FileAppender(ch.qos.logback.core.FileAppender) Logger(ch.qos.logback.classic.Logger) File(java.io.File)

Example 23 with FileAppender

use of ch.qos.logback.core.FileAppender in project jmxtrans by jmxtrans.

the class NagiosWriter method initLogger.

/**
 * Initializes the logger. This is called when we need to create a new
 * logger for the given file name.
 *
 * @param fileStr
 * @return a new Logger instance for the given fileStr
 * @throws IOException
 */
protected Logger initLogger(String fileStr) throws IOException {
    String loggerName = "NagiosWriter" + this.hashCode();
    final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern(LOG_PATTERN);
    encoder.start();
    final FileAppender appender = new FileAppender();
    appender.setContext(loggerContext);
    appender.setName(loggerName + "File");
    appender.setAppend(true);
    appender.setBufferSize(new FileSize(LOG_IO_BUFFER_SIZE_BYTES));
    appender.setFile(fileStr);
    appender.setEncoder(encoder);
    appender.start();
    Logger logger = loggerContext.getLogger(loggerName);
    logger.addAppender(appender);
    logger.setLevel(Level.INFO);
    logger.setAdditive(false);
    return logger;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) Logger(ch.qos.logback.classic.Logger) FileSize(ch.qos.logback.core.util.FileSize)

Example 24 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 25 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(org.sonar.process.logging.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)

Aggregations

FileAppender (ch.qos.logback.core.FileAppender)46 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)33 LoggerContext (ch.qos.logback.classic.LoggerContext)27 File (java.io.File)23 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)18 Logger (ch.qos.logback.classic.Logger)17 Appender (ch.qos.logback.core.Appender)14 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)12 Test (org.junit.Test)9 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)6 Path (java.nio.file.Path)6 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)5 FileSize (ch.qos.logback.core.util.FileSize)5 Test (org.junit.jupiter.api.Test)5 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)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