Skip to content

Commit 06ba628

Browse files
authored
Adds filePattern parameter to ConfigureLoggerLevel (#235)
Sometimes logback configurations are not named logback.xml. Added an optional parameter to make the recipe usable in these cases as well. Resolves #234
1 parent d214405 commit 06ba628

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/main/java/org/openrewrite/java/logging/logback/ConfigureLoggerLevel.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
20+
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.*;
2122
import org.openrewrite.internal.ListUtils;
2223
import org.openrewrite.xml.XPathMatcher;
@@ -30,6 +31,8 @@
3031
@Value
3132
public class ConfigureLoggerLevel extends Recipe {
3233

34+
public static final String DEFAULT_FILE = "**/logback.xml";
35+
3336
@Override
3437
public String getDisplayName() {
3538
return "Configure logback logger level";
@@ -52,6 +55,16 @@ public String getDescription() {
5255
example = "off")
5356
LogLevel logLevel;
5457

58+
@Option(displayName = "File pattern",
59+
description = "A glob expression that can be used to constrain which directories or source files should be searched. " +
60+
"Multiple patterns may be specified, separated by a semicolon `;`. " +
61+
"If multiple patterns are supplied any of the patterns matching will be interpreted as a match. " +
62+
"When not set, '**/logback.xml' is used.",
63+
required = false,
64+
example = "**/logback-spring.xml")
65+
@Nullable
66+
String filePattern;
67+
5568
public enum LogLevel {
5669
trace,
5770
debug,
@@ -63,7 +76,7 @@ public enum LogLevel {
6376

6477
@Override
6578
public TreeVisitor<?, ExecutionContext> getVisitor() {
66-
return Preconditions.check(new FindSourceFiles("**/logback.xml"), new XmlIsoVisitor<ExecutionContext>() {
79+
return Preconditions.check(new FindSourceFiles(filePattern == null ? DEFAULT_FILE : filePattern), new XmlIsoVisitor<ExecutionContext>() {
6780

6881
@Override
6982
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {

src/test/java/org/openrewrite/java/logging/logback/ConfigureLoggerLevelTest.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConfigureLoggerLevelTest implements RewriteTest {
2727
@Test
2828
void editExistingLogger() {
2929
rewriteRun(
30-
spec -> spec.recipe(new ConfigureLoggerLevel("org.springframework", ConfigureLoggerLevel.LogLevel.off)),
30+
spec -> spec.recipe(new ConfigureLoggerLevel("org.springframework", ConfigureLoggerLevel.LogLevel.off, null)),
3131
xml(//language=xml
3232
"""
3333
<configuration>
@@ -67,7 +67,7 @@ void editExistingLogger() {
6767
@Test
6868
void addNewLogger() {
6969
rewriteRun(
70-
spec -> spec.recipe(new ConfigureLoggerLevel("com.example.MyClass", ConfigureLoggerLevel.LogLevel.off)),
70+
spec -> spec.recipe(new ConfigureLoggerLevel("com.example.MyClass", ConfigureLoggerLevel.LogLevel.off, null)),
7171
xml(//language=xml
7272
"""
7373
<configuration>
@@ -104,4 +104,45 @@ void addNewLogger() {
104104
spec -> spec.path("logback.xml"))
105105
);
106106
}
107+
108+
@Test
109+
void logbackSpring() {
110+
rewriteRun(
111+
spec -> spec.recipe(new ConfigureLoggerLevel("com.example.MyClass", ConfigureLoggerLevel.LogLevel.off, "**/logback-spring.xml")),
112+
xml(//language=xml
113+
"""
114+
<configuration>
115+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
116+
<layout class="ch.qos.logback.classic.PatternLayout">
117+
<Pattern>
118+
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
119+
</Pattern>
120+
</layout>
121+
</appender>
122+
123+
<logger name="org.springframework" level="error" additivity="false">
124+
<appender-ref ref="STDOUT" />
125+
</logger>
126+
</configuration>
127+
""",
128+
//language=xml
129+
"""
130+
<configuration>
131+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
132+
<layout class="ch.qos.logback.classic.PatternLayout">
133+
<Pattern>
134+
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
135+
</Pattern>
136+
</layout>
137+
</appender>
138+
139+
<logger name="org.springframework" level="error" additivity="false">
140+
<appender-ref ref="STDOUT" />
141+
</logger>
142+
<logger name="com.example.MyClass" level="off"/>
143+
</configuration>
144+
""",
145+
spec -> spec.path("logback-spring.xml"))
146+
);
147+
}
107148
}

0 commit comments

Comments
 (0)