Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split off YAML configuration into its own module #2142

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions log4j-config-yaml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>${revision}</version>
<relativePath>../log4j-parent</relativePath>
</parent>

<artifactId>log4j-config-yaml</artifactId>

<dependencies>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-plugin-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.config;
package org.apache.logging.log4j.config.yaml;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -24,6 +24,7 @@
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.config.yaml;

import java.io.IOException;
import java.nio.file.Path;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.AbstractConfigurationFactoryTest;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;

class YamlConfigurationFactoryTest extends AbstractConfigurationFactoryTest {

@TempLoggingDir
private static Path loggingPath;

@Test
@LoggerContextSource("YamlConfigurationFactoryTest.yaml")
void yamlConfiguration(final LoggerContext context) throws IOException {
checkConfiguration(context);
final Path logFile = loggingPath.resolve("test-yaml.log");
checkFileLogger(context, logFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.config;

import static org.apache.logging.log4j.util.Unbox.box;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
import org.apache.logging.log4j.util.Strings;

/**
* Common base class for configuration factory tests.
*/
public abstract class AbstractConfigurationFactoryTest {

private static final String LOGGER_NAME = "org.apache.logging.log4j.test1.Test";
private static final String FILE_LOGGER_NAME = "org.apache.logging.log4j.test2.Test";
private static final String APPENDER_NAME = "STDOUT";

/**
* Runs various configuration checks on a configured LoggerContext that should match the equivalent configuration in
* {@code log4j-test1.xml}.
*/
protected static void checkConfiguration(final LoggerContext context) {
final Configuration configuration = context.getConfiguration();
final Map<String, Appender> appenders = configuration.getAppenders();
// these used to be separate tests
assertAll(
() -> assertNotNull(appenders),
() -> assertEquals(3, appenders.size()),
() -> assertNotNull(configuration.getLoggerContext()),
() -> assertEquals(configuration.getRootLogger(), configuration.getLoggerConfig(Strings.EMPTY)),
() -> assertThrows(NullPointerException.class, () -> configuration.getLoggerConfig(null)));

final Logger logger = context.getLogger(LOGGER_NAME);
assertEquals(Level.DEBUG, logger.getLevel());

assertEquals(1, logger.filterCount());
final Iterator<Filter> filterIterator = logger.getFilters();
assertTrue(filterIterator.hasNext());
assertTrue(filterIterator.next() instanceof ThreadContextMapFilter);

final Appender appender = appenders.get(APPENDER_NAME);
assertTrue(appender instanceof ConsoleAppender);
assertEquals(APPENDER_NAME, appender.getName());
}

protected static void checkFileLogger(final LoggerContext context, final Path logFile) throws IOException {
final long currentThreadId = Thread.currentThread().getId();
final Logger logger = context.getLogger(FILE_LOGGER_NAME);
logger.debug("Greetings from ConfigurationFactoryTest in thread#{}", box(currentThreadId));
final List<String> lines = Files.readAllLines(logFile);
assertEquals(1, lines.size());
assertTrue(lines.get(0).endsWith(Long.toString(currentThreadId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,19 @@
*/
package org.apache.logging.log4j.core.config;

import static org.apache.logging.log4j.util.Unbox.box;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class ConfigurationFactoryTest {

static final String LOGGER_NAME = "org.apache.logging.log4j.test1.Test";
static final String FILE_LOGGER_NAME = "org.apache.logging.log4j.test2.Test";
static final String APPENDER_NAME = "STDOUT";
class ConfigurationFactoryTest extends AbstractConfigurationFactoryTest {

@TempLoggingDir
private static Path loggingPath;

/**
* Runs various configuration checks on a configured LoggerContext that should match the equivalent configuration in
* {@code log4j-test1.xml}.
*/
static void checkConfiguration(final LoggerContext context) {
final Configuration configuration = context.getConfiguration();
final Map<String, Appender> appenders = configuration.getAppenders();
// these used to be separate tests
assertAll(
() -> assertNotNull(appenders),
() -> assertEquals(3, appenders.size()),
() -> assertNotNull(configuration.getLoggerContext()),
() -> assertEquals(configuration.getRootLogger(), configuration.getLoggerConfig(Strings.EMPTY)),
() -> assertThrows(NullPointerException.class, () -> configuration.getLoggerConfig(null)));

final Logger logger = context.getLogger(LOGGER_NAME);
assertEquals(Level.DEBUG, logger.getLevel());

assertEquals(1, logger.filterCount());
final Iterator<Filter> filterIterator = logger.getFilters();
assertTrue(filterIterator.hasNext());
assertTrue(filterIterator.next() instanceof ThreadContextMapFilter);

final Appender appender = appenders.get(APPENDER_NAME);
assertTrue(appender instanceof ConsoleAppender);
assertEquals(APPENDER_NAME, appender.getName());
}

static void checkFileLogger(final LoggerContext context, final Path logFile) throws IOException {
final long currentThreadId = Thread.currentThread().getId();
final Logger logger = context.getLogger(FILE_LOGGER_NAME);
logger.debug("Greetings from ConfigurationFactoryTest in thread#{}", box(currentThreadId));
final List<String> lines = Files.readAllLines(logFile);
assertEquals(1, lines.size());
assertTrue(lines.get(0).endsWith(Long.toString(currentThreadId)));
}

@Test
@LoggerContextSource("log4j-test1.xml")
void xml(final LoggerContext context) throws IOException {
Expand All @@ -113,15 +54,6 @@ void json(final LoggerContext context) throws IOException {
checkFileLogger(context, logFile);
}

@Test
@Tag("yaml")
@LoggerContextSource("log4j-test1.yaml")
void yaml(final LoggerContext context) throws IOException {
checkConfiguration(context);
final Path logFile = loggingPath.resolve("test-yaml.log");
checkFileLogger(context, logFile);
}

@Test
@LoggerContextSource("log4j-test1.properties")
void properties(final LoggerContext context) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("yaml")
@LoggerContextSource("log4j2-2134.yaml")
public class JiraLog4j2_2134Test {

@Test
Expand Down
Loading