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();
}
}
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);
}
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);
}
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);
}
}
Aggregations