Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.xml.XPathMatcher;
Expand All @@ -30,6 +31,8 @@
@Value
public class ConfigureLoggerLevel extends Recipe {

public static final String DEFAULT_FILE = "**/logback.xml";

@Override
public String getDisplayName() {
return "Configure logback logger level";
Expand All @@ -52,6 +55,16 @@ public String getDescription() {
example = "off")
LogLevel logLevel;

@Option(displayName = "File pattern",
description = "A glob expression that can be used to constrain which directories or source files should be searched. " +
"Multiple patterns may be specified, separated by a semicolon `;`. " +
"If multiple patterns are supplied any of the patterns matching will be interpreted as a match. " +
"When not set, '**/logback.xml' is used.",
required = false,
example = "**/logback-spring.xml")
@Nullable
String filePattern;

public enum LogLevel {
trace,
debug,
Expand All @@ -63,7 +76,7 @@ public enum LogLevel {

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

@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigureLoggerLevelTest implements RewriteTest {
@Test
void editExistingLogger() {
rewriteRun(
spec -> spec.recipe(new ConfigureLoggerLevel("org.springframework", ConfigureLoggerLevel.LogLevel.off)),
spec -> spec.recipe(new ConfigureLoggerLevel("org.springframework", ConfigureLoggerLevel.LogLevel.off, null)),
xml(//language=xml
"""
<configuration>
Expand Down Expand Up @@ -67,7 +67,7 @@ void editExistingLogger() {
@Test
void addNewLogger() {
rewriteRun(
spec -> spec.recipe(new ConfigureLoggerLevel("com.example.MyClass", ConfigureLoggerLevel.LogLevel.off)),
spec -> spec.recipe(new ConfigureLoggerLevel("com.example.MyClass", ConfigureLoggerLevel.LogLevel.off, null)),
xml(//language=xml
"""
<configuration>
Expand Down Expand Up @@ -104,4 +104,45 @@ void addNewLogger() {
spec -> spec.path("logback.xml"))
);
}

@Test
void logbackSpring() {
rewriteRun(
spec -> spec.recipe(new ConfigureLoggerLevel("com.example.MyClass", ConfigureLoggerLevel.LogLevel.off, "**/logback-spring.xml")),
xml(//language=xml
"""
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<logger name="org.springframework" level="error" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
""",
//language=xml
"""
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<logger name="org.springframework" level="error" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.example.MyClass" level="off"/>
</configuration>
""",
spec -> spec.path("logback-spring.xml"))
);
}
}