Search in sources :

Example 21 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project spring-boot by spring-projects.

the class Log4J2LoggingSystem method getLoggerConfigurations.

@Override
public List<LoggerConfiguration> getLoggerConfigurations() {
    List<LoggerConfiguration> result = new ArrayList<>();
    Configuration configuration = getLoggerContext().getConfiguration();
    for (LoggerConfig loggerConfig : configuration.getLoggers().values()) {
        result.add(convertLoggerConfiguration(loggerConfig));
    }
    Collections.sort(result, CONFIGURATION_COMPARATOR);
    return result;
}
Also used : LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) ArrayList(java.util.ArrayList) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 22 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project torodb by torodb.

the class Log4jUtils method appendToLogFile.

public static void appendToLogFile(String logFile) {
    LoggerContext coreContext = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = coreContext.getConfiguration();
    final Layout<? extends Serializable> layout = PatternLayout.newBuilder().withConfiguration(configuration).withPattern("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n").build();
    final Appender appender = FileAppender.createAppender(logFile, "false", "false", "File", "true", "false", null, null, layout, null, "false", null, configuration);
    appender.start();
    for (LoggerConfig loggerConfig : configuration.getLoggers().values()) {
        configuration.addLoggerAppender(coreContext.getLogger(loggerConfig.getName()), appender);
    }
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 23 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project spring-boot by spring-projects.

the class Log4J2LoggingSystemTests method noFile.

@Test
public void noFile() throws Exception {
    this.loggingSystem.beforeInitialize();
    this.logger.info("Hidden");
    this.loggingSystem.initialize(null, null, null);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    Configuration configuration = this.loggingSystem.getConfiguration();
    assertThat(output).contains("Hello world").doesNotContain("Hidden");
    assertThat(new File(tmpDir() + "/spring.log").exists()).isFalse();
    assertThat(configuration.getConfigurationSource().getFile()).isNotNull();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 24 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project camel by apache.

the class LogBodyWithNewLineTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    writer = new StringWriter();
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    Appender appender = WriterAppender.newBuilder().setLayout(PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).build()).setTarget(writer).setName("Writer").build();
    appender.start();
    config.addAppender(appender);
    config.getRootLogger().removeAppender("Writer");
    config.getRootLogger().addAppender(appender, Level.INFO, null);
    ctx.updateLoggers();
}
Also used : WriterAppender(org.apache.logging.log4j.core.appender.WriterAppender) Appender(org.apache.logging.log4j.core.Appender) StringWriter(java.io.StringWriter) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 25 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project camel by apache.

the class LogVerifier method newAppender.

private Appender newAppender() {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    LogAppender appender = new LogAppender("cdi-rule");
    appender.start();
    config.addAppender(appender);
    config.getRootLogger().removeAppender("cdi-rule");
    config.getRootLogger().addAppender(appender, Level.TRACE, null);
    ctx.updateLoggers();
    return appender;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)210 LoggerContext (org.apache.logging.log4j.core.LoggerContext)120 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)71 Test (org.junit.Test)54 Appender (org.apache.logging.log4j.core.Appender)50 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)37 Test (org.junit.jupiter.api.Test)29 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)22 File (java.io.File)21 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)20 Path (java.nio.file.Path)14 Map (java.util.Map)14 ArrayList (java.util.ArrayList)13 Level (org.apache.logging.log4j.Level)13 ListAppender (org.apache.log4j.ListAppender)12 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)12 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)12 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)11 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)11 InputStream (java.io.InputStream)10