Search in sources :

Example 1 with DefaultConfiguration

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

the class StringBuilderPool method createFormatters.

/**
     */
private static PatternFormatter[] createFormatters() {
    final Configuration config = new DefaultConfiguration();
    final PatternParser parser = new PatternParser(config, "Converter", LogEventPatternConverter.class);
    final List<PatternFormatter> result = parser.parse(LOG4JPATTERN, false, true);
    return result.toArray(new PatternFormatter[result.size()]);
}
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) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter)

Example 2 with DefaultConfiguration

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

the class ScriptConditionTest method testSelectFilesToDelete3.

@Test
@Category(Scripts.Groovy.class)
public void testSelectFilesToDelete3() {
    final Configuration config = new DefaultConfiguration();
    // creates the ScriptManager
    config.initialize();
    final List<PathWithAttributes> pathList = new ArrayList<>();
    pathList.add(new PathWithAttributes(Paths.get("/path/1/abc/a.txt"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/2/abc/bbb.txt"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/3/abc/c.txt"), new DummyFileAttributes()));
    final String scriptText = //
    "" + //
    "import java.nio.file.*;" + //
    "def pattern = ~/(\\d*)[\\/\\\\]abc[\\/\\\\].*\\.txt/;" + //
    "assert pattern.getClass() == java.util.regex.Pattern;" + "def copy = pathList.collect{it};" + //
    "pathList.each { pathWithAttribs -> \n" + //
    "  def relative = basePath.relativize pathWithAttribs.path;" + //
    "  println 'relative path: ' + relative;" + "  def str = relative.toString();" + //
    "  def m = pattern.matcher(str);" + //
    "  if (m.find()) {" + //
    "    def index = m.group(1) as int;" + //
    "    println 'extracted index: ' + index;" + "    def isOdd = (index % 2) == 1;" + //
    "    println 'is odd: ' + isOdd;" + "    if (isOdd) { copy.remove pathWithAttribs}" + //
    "  }" + //
    "}" + "println copy;" + "copy;";
    final Script script = new Script("test", "groovy", scriptText);
    final ScriptCondition condition = new ScriptCondition(script, config);
    final Path base = Paths.get("/path");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertEquals(1, result.size());
    assertEquals(Paths.get("/path/2/abc/bbb.txt"), result.get(0).getPath());
}
Also used : Path(java.nio.file.Path) Script(org.apache.logging.log4j.core.script.Script) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 3 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration 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 = tempDir.resolve("testfile");
    final long timeStamp = Instant.now().minus(Duration.ofDays(1)).toEpochMilli();
    final String expectedDate = formatter.format(timeStamp);
    final Path rolled = tempDir.resolve("test1-" + expectedDate + "-1.log");
    final long copied;
    try (final InputStream is = new ByteArrayInputStream(TEST_DATA.getBytes(StandardCharsets.UTF_8))) {
        copied = Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
    }
    final long size = Files.size(target);
    assertTrue(size > 0);
    assertEquals(copied, size);
    final FileTime fileTime = FileTime.fromMillis(timeStamp);
    final BasicFileAttributeView attrs = Files.getFileAttributeView(target, BasicFileAttributeView.class);
    attrs.setTimes(fileTime, fileTime, fileTime);
    final PatternLayout layout = PatternLayout.newBuilder().setPattern("%msg").setConfiguration(configuration).build();
    final RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder().setCompressionLevelStr("0").setStopCustomActionsOnError(true).setConfig(configuration).build();
    final OnStartupTriggeringPolicy policy = OnStartupTriggeringPolicy.createPolicy(1);
    try (final RollingFileManager manager = RollingFileManager.getFileManager(target.toString(), tempDir.toString() + TARGET_PATTERN, true, false, policy, strategy, null, layout, 8192, true, false, null, null, null, configuration)) {
        manager.initialize();
        final String files;
        try (Stream<Path> contents = Files.list(tempDir)) {
            files = contents.map(Path::toString).collect(Collectors.joining(", ", "[", "]"));
        }
        assertTrue(Files.exists(target), target.toString() + ", files = " + files);
        assertEquals(0, Files.size(target), target.toString());
        assertTrue(Files.exists(rolled), "Missing: " + rolled.toString() + ", files on disk = " + files);
        assertEquals(size, Files.size(rolled), rolled.toString());
    }
}
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) InputStream(java.io.InputStream) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultConfiguration

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

the class AsyncLoggerConfigTest method testIncludeLocationDefaultsToFalse.

@Test
public void testIncludeLocationDefaultsToFalse() {
    final LoggerConfig rootLoggerConfig = AsyncLoggerConfig.RootLogger.createLogger(null, Level.INFO, null, new AppenderRef[0], null, new DefaultConfiguration(), null);
    assertFalse("Include location should default to false for async loggers", rootLoggerConfig.isIncludeLocation());
    final LoggerConfig loggerConfig = AsyncLoggerConfig.createLogger(false, Level.INFO, "com.foo.Bar", null, new AppenderRef[0], null, new DefaultConfiguration(), null);
    assertFalse("Include location should default to false for async loggers", loggerConfig.isIncludeLocation());
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.Test)

Example 5 with DefaultConfiguration

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

the class JpaAppenderBenchmark method setup.

@Setup
public void setup() throws Exception {
    connectionHSQLDB = getConnectionHSQLDB();
    connectionH2 = getConnectionH2();
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-jpa-appender.xml");
    final LoggerContext context = LoggerContext.getContext(false);
    if (context.getConfiguration() instanceof DefaultConfiguration) {
        context.reconfigure();
    }
    StatusLogger.getLogger().reset();
    loggerH2 = LogManager.getLogger("H2Logger");
    loggerHSQLDB = LogManager.getLogger("HSQLDBLogger");
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)28 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