Search in sources :

Example 6 with PatternLayout

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));
    }
}
Also used : Path(java.nio.file.Path) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) File(java.io.File) Test(org.junit.Test)

Example 7 with PatternLayout

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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) Layout(org.apache.logging.log4j.core.Layout) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 8 with PatternLayout

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);
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 9 with PatternLayout

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);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 10 with PatternLayout

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());
}
Also used : PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) Test(org.junit.Test)

Aggregations

PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)17 Test (org.junit.Test)12 LoggerContext (org.apache.logging.log4j.core.LoggerContext)5 Configuration (org.apache.logging.log4j.core.config.Configuration)5 Appender (org.apache.logging.log4j.core.Appender)4 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)3 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)3 File (java.io.File)2 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)2 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)2 InMemoryAppender (org.apache.logging.log4j.test.appender.InMemoryAppender)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 Layout (org.apache.logging.log4j.core.Layout)1 Logger (org.apache.logging.log4j.core.Logger)1 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)1 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)1 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)1