Search in sources :

Example 26 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class WatchHttpTest method testWatchManager.

@Test
public void testWatchManager() throws Exception {
    BlockingQueue<String> queue = new LinkedBlockingQueue<>();
    List<ConfigurationListener> listeners = new ArrayList<>();
    listeners.add(new TestConfigurationListener(queue, "log4j-test1.xml"));
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    Calendar now = Calendar.getInstance(timeZone);
    Calendar previous = now;
    previous.add(Calendar.MINUTE, -5);
    Configuration configuration = new DefaultConfiguration();
    Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
    URL url = new URL("http://localhost:" + wireMockRule.port() + "/log4j-test1.xml");
    StubMapping stubMapping = stubFor(get(urlPathEqualTo("/log4j-test1.xml")).willReturn(aResponse().withBodyFile(file).withStatus(200).withHeader("Last-Modified", formatter.format(previous) + " GMT").withHeader("Content-Type", XML)));
    final ConfigurationScheduler scheduler = new ConfigurationScheduler();
    scheduler.incrementScheduledItems();
    final WatchManager watchManager = new WatchManager(scheduler);
    watchManager.setIntervalSeconds(1);
    scheduler.start();
    watchManager.start();
    try {
        watchManager.watch(new Source(url.toURI()), new HttpWatcher(configuration, null, listeners, previous.getTimeInMillis()));
        final String str = queue.poll(2, TimeUnit.SECONDS);
        assertNotNull("File change not detected", str);
    } finally {
        removeStub(stubMapping);
        watchManager.stop();
        scheduler.stop();
    }
}
Also used : HttpWatcher(org.apache.logging.log4j.core.config.HttpWatcher) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) URL(java.net.URL) ConfigurationListener(org.apache.logging.log4j.core.config.ConfigurationListener) TimeZone(java.util.TimeZone) Test(org.junit.Test)

Example 27 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class RollingRandomAccessFileManagerTest method testRolloverRetainsFileAttributes.

@Test
public void testRolloverRetainsFileAttributes() throws Exception {
    // Short-circuit if host doesn't support file attributes.
    if (!FileUtils.isFilePosixAttributeViewSupported()) {
        return;
    }
    // Create the initial file.
    final File file = File.createTempFile("log4j2", "test");
    // 1 millisec
    LockSupport.parkNanos(1000000);
    // Set the initial file attributes.
    final String filePermissionsString = "rwxrwxrwx";
    final Set<PosixFilePermission> filePermissions = PosixFilePermissions.fromString(filePermissionsString);
    FileUtils.defineFilePosixAttributeView(file.toPath(), filePermissions, null, null);
    // Create the manager.
    final RolloverStrategy rolloverStrategy = DefaultRolloverStrategy.newBuilder().setMax("7").setMin("1").setFileIndex("max").setStopCustomActionsOnError(false).setConfig(new DefaultConfiguration()).build();
    final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager(file.getAbsolutePath(), Strings.EMPTY, true, true, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE, new SizeBasedTriggeringPolicy(Long.MAX_VALUE), rolloverStrategy, null, null, filePermissionsString, null, null, null);
    assertNotNull(manager);
    manager.initialize();
    // Trigger a rollover.
    manager.rollover();
    // Verify the rolled over file attributes.
    final Set<PosixFilePermission> actualFilePermissions = Files.getFileAttributeView(Paths.get(manager.getFileName()), PosixFileAttributeView.class).readAttributes().permissions();
    assertEquals(filePermissions, actualFilePermissions);
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 28 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class ThreadLocalVsPoolBenchmark method createFormatters.

private static List<PatternFormatter> createFormatters() {
    final Configuration config = new DefaultConfiguration();
    final PatternParser parser = new PatternParser(config, "Converter", LogEventPatternConverter.class);
    return parser.parse("%d %5p [%t] %c{1} %X{transactionId} - %m%n", false, true);
}
Also used : PatternParser(org.apache.logging.log4j.core.pattern.PatternParser) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration)

Example 29 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project neo4j by neo4j.

the class RotatingLogFileWriter method setupLogFile.

private static Neo4jLoggerContext setupLogFile(FileSystemAbstraction fileSystemAbstraction, Path logPath, long rotationThreshold, int maxArchives, String fileSuffix, String header) {
    try {
        Closeable additionalCloseable = null;
        Configuration configuration = new DefaultConfiguration() {

            @Override
            protected void setToDefault() {
            // no defaults
            }
        };
        // Just adds a header to the beginning of each file - no transformation will be done on the log messages.
        PatternLayout layout = PatternLayout.newBuilder().withConfiguration(configuration).withHeader(header).build();
        Appender appender;
        if (fileSystemAbstraction instanceof DefaultFileSystemAbstraction) {
            appender = RollingFileAppender.newBuilder().setName(APPENDER_NAME).setLayout(layout).withFileName(logPath.toString()).withFilePattern(logPath + ".%i" + fileSuffix).withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(rotationThreshold))).withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(maxArchives)).withFileIndex("min").build()).build();
        } else {
            // When using a different file system than DefaultFileSystemAbstraction for tests, we cannot use log4j file appenders since
            // it will create files directly in the real filesystem ignoring our abstraction.
            fileSystemAbstraction.mkdirs(logPath.getParent());
            OutputStream outputStream = fileSystemAbstraction.openAsOutputStream(logPath, true);
            additionalCloseable = outputStream;
            appender = ((OutputStreamAppender.Builder<?>) OutputStreamAppender.newBuilder().setName(APPENDER_NAME).setLayout(layout)).setTarget(outputStream).build();
        }
        appender.start();
        configuration.addAppender(appender);
        LoggerConfig rootLogger = configuration.getRootLogger();
        rootLogger.addAppender(appender, null, null);
        rootLogger.setLevel(Level.DEBUG);
        LoggerContext context = new LoggerContext("loggercontext");
        context.setConfiguration(configuration);
        return new Neo4jLoggerContext(context, additionalCloseable);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) OutputStreamAppender(org.apache.logging.log4j.core.appender.OutputStreamAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) Closeable(java.io.Closeable) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) OutputStream(java.io.OutputStream) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)29 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 Path (java.nio.file.Path)7 Test (org.junit.jupiter.api.Test)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)6 PathWithAttributes (org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes)3 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)3 Script (org.apache.logging.log4j.core.script.Script)3 ScriptPlugin (org.apache.logging.log4j.script.ScriptPlugin)3 Setup (org.openjdk.jmh.annotations.Setup)3 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)2 File (java.io.File)2 URL (java.net.URL)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Appender (org.apache.logging.log4j.core.Appender)2 LogEvent (org.apache.logging.log4j.core.LogEvent)2