use of org.apache.logging.log4j.core.layout.PatternLayout in project logging-log4j2 by apache.
the class OnStartupTriggeringPolicyTest method testPolicy.
@Test
public void testPolicy() throws Exception {
final Configuration configuration = new DefaultConfiguration();
final Path target = Paths.get(TARGET_FILE);
target.toFile().getParentFile().mkdirs();
final long timeStamp = System.currentTimeMillis() - (1000 * 60 * 60 * 24);
final String expectedDate = formatter.format(timeStamp);
final String rolledFileName = ROLLED_FILE_PREFIX + expectedDate + ROLLED_FILE_SUFFIX;
final Path rolled = Paths.get(rolledFileName);
final long copied;
try (final InputStream is = new ByteArrayInputStream(TEST_DATA.getBytes("UTF-8"))) {
copied = Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
}
final long size = Files.size(target);
assertTrue(size > 0);
assertEquals(copied, size);
Assert.assertTrue(target.toFile().setLastModified(timeStamp));
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%msg").withConfiguration(configuration).build();
final RolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(null, null, null, "0", null, true, configuration);
final OnStartupTriggeringPolicy policy = OnStartupTriggeringPolicy.createPolicy(1);
try (final RollingFileManager manager = RollingFileManager.getFileManager(TARGET_FILE, TARGET_PATTERN, true, false, policy, strategy, null, layout, 8192, true, false, configuration)) {
manager.initialize();
final String files = Arrays.toString(new File(TARGET_FOLDER).listFiles());
assertTrue(target.toString() + ", files = " + files, Files.exists(target));
assertEquals(target.toString(), 0, Files.size(target));
assertTrue("Missing: " + rolled.toString() + ", files on disk = " + files, Files.exists(rolled));
assertEquals(rolled.toString(), size, Files.size(rolled));
}
}
use of org.apache.logging.log4j.core.layout.PatternLayout in project Anserini by castorini.
the class TrainingDataGenerator method createNewLoggerConfig.
/**
* Dynamically creates a logger configuration with an appender that writes to a file.
* This logger is used to write training data.
*
* @param loggerName the name of the logger to create
* @param outputFilePath the file path to write logs to
* @param patternLayout layout for the logger, if null just display
* the message using DEFAULT_CONVERSION_PATTERN
*/
private static void createNewLoggerConfig(String loggerName, String outputFilePath, String patternLayout) {
// Ignore null output files
if (outputFilePath == null)
return;
// Create a logger to write the training data
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Layout layout = PatternLayout.createLayout(patternLayout == null ? PatternLayout.DEFAULT_CONVERSION_PATTERN : patternLayout, config, null, StandardCharsets.UTF_8, false, false, null, null);
Appender appender = FileAppender.createAppender(outputFilePath, "false", "false", loggerName, "true", "false", "false", "2000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
// Adding reference to the appender
AppenderRef ref = AppenderRef.createAppenderRef(loggerName, null, null);
AppenderRef[] refs = new AppenderRef[] { ref };
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.TRACE, loggerName, "true", refs, null, config, null);
// Adding appender to logger, and adding logger to context
loggerConfig.addAppender(appender, null, null);
config.addLogger(loggerName, loggerConfig);
ctx.updateLoggers();
}
use of org.apache.logging.log4j.core.layout.PatternLayout in project geode by apache.
the class DUnitLauncher method addSuspectFileAppender.
/**
* Add an appender to Log4j which sends all INFO+ messages to a separate file which will be used
* later to scan for suspect strings. The pattern of the messages conforms to the original log
* format so that hydra will be able to parse them.
*/
private static void addSuspectFileAppender(final String workspaceDir) {
final String suspectFilename = new File(workspaceDir, SUSPECT_FILENAME).getAbsolutePath();
final LoggerContext appenderContext = ((org.apache.logging.log4j.core.Logger) LogManager.getLogger(LogService.BASE_LOGGER_NAME)).getContext();
final PatternLayout layout = PatternLayout.createLayout("[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid] %message%n%throwable%n", null, null, null, Charset.defaultCharset(), true, false, "", "");
final FileAppender fileAppender = FileAppender.createAppender(suspectFilename, "true", "false", DUnitLauncher.class.getName(), "true", "false", "false", "0", layout, null, null, null, appenderContext.getConfiguration());
fileAppender.start();
LoggerConfig loggerConfig = appenderContext.getConfiguration().getLoggerConfig(LogService.BASE_LOGGER_NAME);
loggerConfig.addAppender(fileAppender, Level.INFO, null);
}
use of org.apache.logging.log4j.core.layout.PatternLayout in project logging-log4j2 by apache.
the class WriterAppenderTest method addAppender.
private void addAppender(final Writer writer, final String writerName) {
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
final PatternLayout layout = PatternLayout.createDefaultLayout(config);
final Appender appender = WriterAppender.createAppender(layout, null, writer, writerName, false, true);
appender.start();
config.addAppender(appender);
ConfigurationTestUtils.updateLoggers(appender, config);
}
use of org.apache.logging.log4j.core.layout.PatternLayout in project logging-log4j2 by apache.
the class Log4j1ConfigurationFactoryTest method testFileSimpleLayout.
@Test
public void testFileSimpleLayout() throws Exception {
final PatternLayout layout = (PatternLayout) testFile("config-1.2/log4j-file-SimpleLayout.properties");
assertEquals("%level - %m%n", layout.getConversionPattern());
}
Aggregations