Browse Source
* pr/17515: Polish "Add support for configuring logging groups" Add support for configuring logging groups via endpoint Closes gh-17515pull/17742/head
11 changed files with 598 additions and 73 deletions
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
/* |
||||
* Copyright 2012-2019 the original author or authors. |
||||
* |
||||
* Licensed 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 |
||||
* |
||||
* https://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.springframework.boot.logging; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.function.BiConsumer; |
||||
|
||||
/** |
||||
* A single logger group. |
||||
* |
||||
* @author Madhura Bhave |
||||
* @author Phillip Webb |
||||
* @since 2.2.0 |
||||
*/ |
||||
public final class LoggerGroup { |
||||
|
||||
private final String name; |
||||
|
||||
private final List<String> members; |
||||
|
||||
private LogLevel configuredLevel; |
||||
|
||||
LoggerGroup(String name, List<String> members) { |
||||
this.name = name; |
||||
this.members = Collections.unmodifiableList(new ArrayList<>(members)); |
||||
} |
||||
|
||||
public String getName() { |
||||
return this.name; |
||||
} |
||||
|
||||
public List<String> getMembers() { |
||||
return this.members; |
||||
} |
||||
|
||||
public boolean hasMembers() { |
||||
return !this.members.isEmpty(); |
||||
} |
||||
|
||||
public LogLevel getConfiguredLevel() { |
||||
return this.configuredLevel; |
||||
} |
||||
|
||||
public void configureLogLevel(LogLevel level, BiConsumer<String, LogLevel> configurer) { |
||||
this.configuredLevel = level; |
||||
this.members.forEach((name) -> configurer.accept(name, level)); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
/* |
||||
* Copyright 2012-2019 the original author or authors. |
||||
* |
||||
* Licensed 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 |
||||
* |
||||
* https://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.springframework.boot.logging; |
||||
|
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* Logger groups configured via the Spring Environment. |
||||
* |
||||
* @author HaiTao Zhang |
||||
* @author Phillip Webb |
||||
* @since 2.2.0 #see {@link LoggerGroup} |
||||
*/ |
||||
public final class LoggerGroups implements Iterable<LoggerGroup> { |
||||
|
||||
private final Map<String, LoggerGroup> groups = new ConcurrentHashMap<>(); |
||||
|
||||
public LoggerGroups() { |
||||
} |
||||
|
||||
public LoggerGroups(Map<String, List<String>> namesAndMembers) { |
||||
putAll(namesAndMembers); |
||||
} |
||||
|
||||
public void putAll(Map<String, List<String>> namesAndMembers) { |
||||
namesAndMembers.forEach(this::put); |
||||
} |
||||
|
||||
private void put(String name, List<String> members) { |
||||
put(new LoggerGroup(name, members)); |
||||
} |
||||
|
||||
private void put(LoggerGroup loggerGroup) { |
||||
this.groups.put(loggerGroup.getName(), loggerGroup); |
||||
} |
||||
|
||||
public LoggerGroup get(String name) { |
||||
return this.groups.get(name); |
||||
} |
||||
|
||||
@Override |
||||
public Iterator<LoggerGroup> iterator() { |
||||
return this.groups.values().iterator(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
/* |
||||
* Copyright 2012-2019 the original author or authors. |
||||
* |
||||
* Licensed 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 |
||||
* |
||||
* https://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.springframework.boot.logging; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link LoggerGroups} |
||||
* |
||||
* @author HaiTao Zhang |
||||
* @author Madhura Bhave |
||||
*/ |
||||
class LoggerGroupsTests { |
||||
|
||||
private LoggingSystem loggingSystem = mock(LoggingSystem.class); |
||||
|
||||
@Test |
||||
void putAllShouldAddLoggerGroups() { |
||||
Map<String, List<String>> groups = Collections.singletonMap("test", |
||||
Arrays.asList("test.member", "test.member2")); |
||||
LoggerGroups loggerGroups = new LoggerGroups(); |
||||
loggerGroups.putAll(groups); |
||||
LoggerGroup group = loggerGroups.get("test"); |
||||
assertThat(group.getMembers()).containsExactly("test.member", "test.member2"); |
||||
} |
||||
|
||||
@Test |
||||
void iteratorShouldReturnLoggerGroups() { |
||||
LoggerGroups groups = createLoggerGroups(); |
||||
assertThat(groups).hasSize(3); |
||||
assertThat(groups).extracting("name").containsExactlyInAnyOrder("test0", "test1", "test2"); |
||||
} |
||||
|
||||
private LoggerGroups createLoggerGroups() { |
||||
Map<String, List<String>> groups = new LinkedHashMap<>(); |
||||
groups.put("test0", Arrays.asList("test0.member", "test0.member2")); |
||||
groups.put("test1", Arrays.asList("test1.member", "test1.member2")); |
||||
groups.put("test2", Arrays.asList("test2.member", "test2.member2")); |
||||
return new LoggerGroups(groups); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue