Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.waitAtMost;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.util.Source;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDir;

public class MonitorResourcesTest {

@Test
void test_reconfiguration(@TempDir(cleanup = CleanupMode.ON_SUCCESS) final Path tempDir) throws IOException {
final ConfigurationBuilder<PropertiesConfiguration> configBuilder =
ConfigurationBuilderFactory.newConfigurationBuilder(PropertiesConfiguration.class);
final Path configFile = tempDir.resolve("log4j.xml");
final Path externalResourceFile1 = tempDir.resolve("external-resource-1.txt");
final Path externalResourceFile2 = tempDir.resolve("external-resource-2.txt");
final ConfigurationSource configSource = new ConfigurationSource(new Source(configFile), new byte[] {}, 0);
final int monitorInterval = 3;
final Configuration config = configBuilder
.setConfigurationSource(configSource)
.setMonitorInterval(String.valueOf(monitorInterval))
.add(configBuilder.newMonitorResource(
externalResourceFile1.toUri().toString()))
.add(configBuilder.newMonitorResource(
externalResourceFile2.toUri().toString()))
.build();

try (final LoggerContext loggerContext = Configurator.initialize(config)) {
assertMonitorResourceFileNames(
loggerContext,
configFile.getFileName().toString(),
externalResourceFile1.getFileName().toString(),
externalResourceFile2.getFileName().toString());
Files.write(externalResourceFile2, Collections.singletonList("a change"));
waitAtMost(2 * monitorInterval, TimeUnit.SECONDS).until(() -> loggerContext.getConfiguration() != config);
}
}

@Test
@LoggerContextSource("config/MonitorResource/log4j.xml")
void test_config_of_type_XML(final LoggerContext loggerContext) {
assertMonitorResourceFileNames(loggerContext, "log4j.xml");
}

@Test
@LoggerContextSource("config/MonitorResource/log4j.json")
void test_config_of_type_JSON(final LoggerContext loggerContext) {
assertMonitorResourceFileNames(loggerContext, "log4j.json");
}

@Test
@LoggerContextSource("config/MonitorResource/log4j.yaml")
void test_config_of_type_YAML(final LoggerContext loggerContext) {
assertMonitorResourceFileNames(loggerContext, "log4j.yaml");
}

@Test
@LoggerContextSource("config/MonitorResource/log4j.properties")
void test_config_of_type_properties(final LoggerContext loggerContext) {
assertMonitorResourceFileNames(loggerContext, "log4j.properties");
}

private static void assertMonitorResourceFileNames(final LoggerContext loggerContext, final String configFileName) {
assertMonitorResourceFileNames(loggerContext, configFileName, "external-file-1.txt", "external-file-2.txt");
}

private static void assertMonitorResourceFileNames(
final LoggerContext loggerContext, final String configFileName, final String... externalResourceFileNames) {
final Set<Source> sources = loggerContext
.getConfiguration()
.getWatchManager()
.getConfigurationWatchers()
.keySet();
final Set<String> actualFileNames =
sources.stream().map(source -> source.getFile().getName()).collect(Collectors.toSet());
final Set<String> expectedFileNames = new LinkedHashSet<>();
expectedFileNames.add(configFileName);
expectedFileNames.addAll(Arrays.asList(externalResourceFileNames));
assertThat(actualFileNames).as("watch manager sources: %s", sources).isEqualTo(expectedFileNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Configuration": {
"monitorInterval": "30",
"MonitorResources": {
"MonitorResource": [
{
"uri": "file://path/to/external-file-1.txt"
},
{
"uri": "file://path/to/external-file-2.txt"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# 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.
#
monitorInterval = 30
monitorResources.0.uri = file://path/to/external-file-1.txt
monitorResources.1.uri = file://path/to/external-file-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.
-->
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-2.xsd"
monitorInterval="30">
<MonitorResources>
<MonitorResource uri="file://path/to/external-file-1.txt"/>
<MonitorResource uri="file://path/to/external-file-2.txt"/>
</MonitorResources>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# 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.
#
Configuration:
monitorInterval: '30'
MonitorResources:
MonitorResource:
- uri: "file://path/to/external-file-1.txt"
- uri: "file://path/to/external-file-2.txt"
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement
private ConcurrentMap<String, Appender> appenders = new ConcurrentHashMap<>();
private ConcurrentMap<String, LoggerConfig> loggerConfigs = new ConcurrentHashMap<>();
private List<CustomLevelConfig> customLevels = Collections.emptyList();
private Set<MonitorResource> monitorResources = Collections.emptySet();
private final ConcurrentMap<String, String> propertyMap = new ConcurrentHashMap<>();
private final Interpolator tempLookup = new Interpolator(propertyMap);
private final StrSubstitutor runtimeStrSubstitutor = new RuntimeStrSubstitutor(tempLookup);
Expand Down Expand Up @@ -267,6 +268,7 @@ public void initialize() {
setup();
setupAdvertisement();
doConfigure();
watchMonitorResources();
setState(State.INITIALIZED);
LOGGER.debug("Configuration {} initialized", this);
}
Expand Down Expand Up @@ -325,7 +327,7 @@ public void start() {
if (watchManager.getIntervalSeconds() >= 0) {
LOGGER.info(
"Start watching for changes to {} every {} seconds",
getConfigurationSource(),
watchManager.getConfigurationWatchers().keySet(),
watchManager.getIntervalSeconds());
watchManager.start();
}
Expand All @@ -347,6 +349,17 @@ public void start() {
LOGGER.info("Configuration {} started.", this);
}

private void watchMonitorResources() {
if (this instanceof Reconfigurable && watchManager.getIntervalSeconds() >= 0) {
monitorResources.forEach(monitorResource -> {
Source source = new Source(monitorResource.getUri());
final ConfigurationFileWatcher watcher = new ConfigurationFileWatcher(
this, (Reconfigurable) this, listeners, source.getFile().lastModified());
watchManager.watch(source, watcher);
});
}
}

private boolean hasAsyncLoggers() {
if (root instanceof AsyncLoggerConfig) {
return true;
Expand Down Expand Up @@ -729,9 +742,16 @@ protected void doConfigure() {
} else if (child.isInstanceOf(AsyncWaitStrategyFactoryConfig.class)) {
final AsyncWaitStrategyFactoryConfig awsfc = child.getObject(AsyncWaitStrategyFactoryConfig.class);
asyncWaitStrategyFactory = awsfc.createWaitStrategyFactory();
} else if (child.isInstanceOf(MonitorResources.class)) {
monitorResources = child.getObject(MonitorResources.class).getResources();
} else {
final List<String> expected = Arrays.asList(
"\"Appenders\"", "\"Loggers\"", "\"Properties\"", "\"Scripts\"", "\"CustomLevels\"");
"\"Appenders\"",
"\"Loggers\"",
"\"Properties\"",
"\"Scripts\"",
"\"CustomLevels\"",
"\"MonitorResources\"");
LOGGER.error(
"Unknown object \"{}\" of type {} is ignored: try nesting it inside one of: {}.",
child.getName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 java.util.Objects.requireNonNull;

import java.net.URI;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

/**
* Container for the {@code MonitorResource} element.
*/
@Plugin(name = "MonitorResource", category = Core.CATEGORY_NAME, printObject = true)
public final class MonitorResource {

private final URI uri;

private MonitorResource(final URI uri) {
this.uri = requireNonNull(uri, "uri");
if (!"file".equals(uri.getScheme())) {
final String message =
String.format("Only `file` scheme is supported in monitor resource URIs! Illegal URI: `%s`", uri);
throw new IllegalArgumentException(message);
}
}

@PluginFactory
public static MonitorResource createMonitorResource(@PluginAttribute("uri") final URI uri) {
return new MonitorResource(uri);
}

public URI getUri() {
return uri;
}

@Override
public int hashCode() {
return uri.hashCode();
}

@Override
public boolean equals(final Object object) {
if (this == object) {
return true;
}
if (!(object instanceof MonitorResource)) {
return false;
}
final MonitorResource other = (MonitorResource) object;
return this.uri == other.uri;
}

@Override
public String toString() {
return String.format("MonitorResource{%s}", uri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

/**
* Container for the {@code MonitorResources} element.
*/
@Plugin(name = "MonitorResources", category = Core.CATEGORY_NAME, printObject = true)
public final class MonitorResources {

private final Set<MonitorResource> resources;

private MonitorResources(final Set<MonitorResource> resources) {
this.resources = requireNonNull(resources, "resources");
}

@PluginFactory
public static MonitorResources createMonitorResources(
@PluginElement("monitorResource") final MonitorResource[] resources) {
requireNonNull(resources, "resources");
final LinkedHashSet<MonitorResource> distinctResources =
Arrays.stream(resources).collect(Collectors.toCollection(LinkedHashSet::new));
return new MonitorResources(distinctResources);
}

public Set<MonitorResource> getResources() {
return resources;
}
}
Loading
Loading