Search in sources :

Example 36 with FileAppender

use of ch.qos.logback.core.FileAppender in project ha-bridge by bwssytems.

the class LoggingUtil method getLogFileInfos.

/**
 * Get the logfile information for the roor logger.
 *
 * @return List of LogFileInfo obejcts
 */
public static List<LogFileInfo> getLogFileInfos() {
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final List<LogFileInfo> logFileInfos = new ArrayList<LogFileInfo>();
    final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
    final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
    while (it.hasNext()) {
        final Appender<ILoggingEvent> appender = it.next();
        if (appender instanceof FileAppender) {
            final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;
            final File logFile = new File(fileAppender.getFile());
            final LogFileInfo logFileInfo = new LogFileInfo();
            logFileInfo.setFileName(logFile.getName());
            logFileInfo.setFileLastChanged(new Date(logFile.lastModified()));
            logFileInfo.setFileSize(logFile.length());
            logFileInfos.add(logFileInfo);
        }
    }
    return logFileInfos;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) FileAppender(ch.qos.logback.core.FileAppender) ArrayList(java.util.ArrayList) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) LogFileInfo(com.bwssystems.logservices.LogFileInfo) Date(java.util.Date) File(java.io.File)

Example 37 with FileAppender

use of ch.qos.logback.core.FileAppender in project ha-bridge by bwssytems.

the class LoggingUtil method getLogFile.

/**
 * Get the log file.
 *
 * @param logFileName The name of the log file
 * @return The actual file
 */
public static File getLogFile(final String logFileName) {
    if (logFileName == null) {
        throw new IllegalArgumentException("logFileName cannot be null.");
    }
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
    final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
    while (it.hasNext()) {
        final Appender<ILoggingEvent> appender = it.next();
        if (appender instanceof FileAppender) {
            final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;
            final File logFile = new File(fileAppender.getFile());
            if (logFile.getName().equalsIgnoreCase(logFileName)) {
                return logFile;
            }
        }
    }
    return null;
}
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) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File)

Example 38 with FileAppender

use of ch.qos.logback.core.FileAppender in project indy by Commonjava.

the class DiagnosticsManager method getDiagnosticBundle.

public File getDiagnosticBundle() throws IOException {
    File out = createTempFile("diags");
    Logger rootLogger = (Logger) LoggerFactory.getLogger("ROOT");
    logger.info("Writing diagnostic bundle to: '{}'", out);
    Appender<ILoggingEvent> appender = rootLogger.getAppender(FILE_LOGGER);
    if (appender != null && (appender instanceof FileAppender)) {
        try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(out))) {
            File dir = new File(((FileAppender) appender).getFile()).getParentFile();
            for (File f : dir.listFiles(file -> file.getName().endsWith(".log"))) {
                String name = LOGS_DIR + "/" + f.getName();
                logger.info("Adding {} to bundle zip: {}", name, out);
                zip.putNextEntry(new ZipEntry(name));
                try (InputStream in = new FileInputStream(f)) {
                    IOUtils.copy(in, zip);
                }
            }
            logger.info("Adding thread dump to bundle zip: {}", out);
            zip.putNextEntry(new ZipEntry(THREAD_DUMP_FILE));
            zip.write(getThreadDumpString().getBytes());
            zip.flush();
            zip.close();
        }
    } else {
        return null;
    }
    return out;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) ZipOutputStream(java.util.zip.ZipOutputStream) FileInputStream(java.io.FileInputStream) IOUtils.toInputStream(org.apache.commons.io.IOUtils.toInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 39 with FileAppender

use of ch.qos.logback.core.FileAppender 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 40 with FileAppender

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

the class DefaultPluginLoggingServiceIntegrationTest method shouldGetCurrentLogDirectoryByLookingAtFileAppenderOfRootLogger.

@Test
@DisabledOnOs(OS.WINDOWS)
void shouldGetCurrentLogDirectoryByLookingAtFileAppenderOfRootLogger() {
    FileAppender fileAppender = new FileAppender();
    fileAppender.setFile("/var/log/go-server/go-server.log");
    DefaultPluginLoggingService service = Mockito.spy(new DefaultPluginLoggingService(systemEnvironment));
    doReturn(fileAppender).when(service).getGoServerLogFileAppender();
    String currentLogDirectory = service.getCurrentLogDirectory();
    assertThat(currentLogDirectory).isEqualTo("/var/log/go-server");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.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