- 
                Notifications
    You must be signed in to change notification settings 
- Fork 29
Add Log4j 1 to Log4j 2 configuration file conversion #202
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
          
     Draft
      
      
            ppkarwasz
  wants to merge
  11
  commits into
  openrewrite:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
ppkarwasz:feature/log4j1-to-log4j-core
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      367362b
              
                Add Log4j 1 to Log4j 2 configuration file conversion
              
              
                ppkarwasz a33dea0
              
                Fix licenses
              
              
                ppkarwasz 0bd6cf9
              
                Fix imports
              
              
                ppkarwasz 81ce466
              
                Use `repository.apache.org` only for snapshots
              
              
                ppkarwasz 9cc4a8b
              
                Limit requests to `repository.apache.org`
              
              
                ppkarwasz e1a4b99
              
                Try guessing bot suggestions
              
              
                ppkarwasz 599234e
              
                Reverts changes to `Slf4jToLog4jTest`
              
              
                ppkarwasz 608f1c9
              
                Merge branch 'main' into feature/log4j1-to-log4j-core
              
              
                timtebeek 4d9a3bc
              
                Run `./gradlew downloadRecipeDependencies` to update jars
              
              
                timtebeek 5339812
              
                Light polish
              
              
                timtebeek 6cb469a
              
                Update classes to expect a newer version of the log4j jars
              
              
                timtebeek File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -12,13 +12,24 @@ recipeDependencies { | |||||
|  | ||||||
| parserClasspath("org.slf4j:slf4j-api:2.+") | ||||||
| parserClasspath("log4j:log4j:1.+") | ||||||
| parserClasspath("org.apache.logging.log4j:log4j-core:2.+") | ||||||
| parserClasspath("org.apache.logging.log4j:log4j-api:2.+") | ||||||
| parserClasspath("org.apache.logging.log4j:log4j-core:2.24.3") | ||||||
| parserClasspath("org.apache.logging.log4j:log4j-api:2.24.3") | ||||||
| parserClasspath("commons-logging:commons-logging:1.+") | ||||||
| parserClasspath("ch.qos.logback:logback-classic:1.3.+") | ||||||
| parserClasspath("org.projectlombok:lombok:1.18.+") | ||||||
| } | ||||||
|  | ||||||
| repositories { | ||||||
| mavenCentral() | ||||||
| mavenLocal() | ||||||
| maven { | ||||||
| setUrl("https://repository.apache.org/snapshots") | ||||||
| mavenContent { | ||||||
| snapshotsOnly() | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|  | ||||||
| dependencies { | ||||||
| compileOnly("org.projectlombok:lombok:latest.release") | ||||||
| annotationProcessor("org.projectlombok:lombok:latest.release") | ||||||
|  | @@ -27,13 +38,15 @@ dependencies { | |||||
|  | ||||||
| implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}")) | ||||||
| implementation("org.openrewrite:rewrite-java") | ||||||
| implementation("org.openrewrite:rewrite-maven") | ||||||
| implementation("org.openrewrite.recipe:rewrite-java-dependencies:${rewriteVersion}") | ||||||
| implementation("org.openrewrite.recipe:rewrite-static-analysis:${rewriteVersion}") | ||||||
| runtimeOnly("org.openrewrite:rewrite-java-17") | ||||||
|  | ||||||
| implementation("log4j:log4j:1.+") | ||||||
| implementation("org.apache.logging.log4j:log4j-core:2.+") | ||||||
| implementation("org.apache.logging.log4j:log4j-core:2.24.3") | ||||||
| implementation("org.slf4j:slf4j-api:2.+") | ||||||
| implementation("org.apache.logging.log4j:log4j-converter-config:0.3.0-SNAPSHOT") | ||||||
|  | ||||||
| annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion") | ||||||
| implementation("org.openrewrite:rewrite-templating:$rewriteVersion") | ||||||
|  | @@ -46,7 +59,7 @@ dependencies { | |||||
| testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:latest.release") | ||||||
|  | ||||||
| testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") | ||||||
| testImplementation("org.openrewrite:rewrite-maven") | ||||||
| testImplementation("org.openrewrite:rewrite-properties:${rewriteVersion}") | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rewrite-bom manages the version here, so we can leave that out already. 
        Suggested change
       
 | ||||||
| testImplementation("org.openrewrite:rewrite-test") | ||||||
| testImplementation("org.openrewrite:rewrite-java-tck") | ||||||
|  | ||||||
|  | ||||||
        
          
          
            104 changes: 104 additions & 0 deletions
          
          104 
        
  src/main/java/org/openrewrite/java/logging/ConvertConfiguration.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright 2024 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * 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.openrewrite.java.logging; | ||
|  | ||
| import lombok.AllArgsConstructor; | ||
| import org.apache.logging.converter.config.ConfigurationConverter; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.*; | ||
| import org.openrewrite.NlsRewrite.Description; | ||
| import org.openrewrite.NlsRewrite.DisplayName; | ||
|         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| import org.openrewrite.text.PlainText; | ||
|  | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
|  | ||
| /** | ||
| * Converts a logging configuration file from one format to another. | ||
| */ | ||
|         
                  ppkarwasz marked this conversation as resolved.
              Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| @AllArgsConstructor | ||
| public class ConvertConfiguration extends Recipe { | ||
|  | ||
| @Option(displayName = "Pattern for the files to convert", | ||
| description = "If set, only the files that match this pattern will be converted.", | ||
| required = false) | ||
|         
                  ppkarwasz marked this conversation as resolved.
              Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| @Nullable | ||
| String filePattern; | ||
|  | ||
| @Option(displayName = "Input format", description = "The id of the input logging configuration format. See [Log4j documentation](https://logging.staged.apache.org/log4j/transform/log4j-converter-config.html#formats) for a list of supported formats.", example = "v1:properties") | ||
| String inputFormat; | ||
|  | ||
| @Option(displayName = "Output format", description = "The id of the output logging configuration format. See [Log4j documentation](https://logging.staged.apache.org/log4j/transform/log4j-converter-config.html#formats) for a list of supported formats.", example = "v2:xml") | ||
| String outputFormat; | ||
|  | ||
| private static final ConfigurationConverter converter = ConfigurationConverter.getInstance(); | ||
|  | ||
| @Override | ||
| public @DisplayName String getDisplayName() { | ||
| return "Convert logging configuration"; | ||
|         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
|  | ||
| @Override | ||
| public @Description String getDescription() { | ||
| return "Converts the configuration of a logging backend from one format to another. For example it can convert a Log4j 1 properties configuration file into a Log4j Core 2 XML configuration file."; | ||
| } | ||
|  | ||
| @Override | ||
| public int maxCycles() { | ||
| return 1; | ||
| } | ||
|  | ||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return | ||
| Preconditions.check(new FindSourceFiles(filePattern), | ||
| new TreeVisitor<Tree, ExecutionContext>() { | ||
|  | ||
| @Override | ||
| public boolean isAcceptable(SourceFile sourceFile, ExecutionContext executionContext) { | ||
| return super.isAcceptable(sourceFile, executionContext); | ||
| } | ||
|         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| @Override | ||
| public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) { | ||
| if (tree instanceof SourceFile) { | ||
| SourceFile sourceFile = (SourceFile) tree; | ||
| ByteArrayInputStream inputStream = new ByteArrayInputStream(sourceFile.printAllAsBytes()); | ||
| ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|  | ||
| converter.convert(inputStream, inputFormat, outputStream, outputFormat); | ||
|  | ||
| String utf8 = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); | ||
| if (tree instanceof PlainText) { | ||
| return ((PlainText) tree).withText(utf8).withCharset(StandardCharsets.UTF_8); | ||
| } | ||
| return PlainText.builder() | ||
| .id(sourceFile.getId()) | ||
| .charsetBomMarked(sourceFile.isCharsetBomMarked()) | ||
| .charsetName(StandardCharsets.UTF_8.name()) | ||
| .checksum(sourceFile.getChecksum()) | ||
| .fileAttributes(sourceFile.getFileAttributes()) | ||
| .markers(sourceFile.getMarkers()) | ||
| .sourcePath(sourceFile.getSourcePath()) | ||
| .text(utf8) | ||
| .build(); | ||
| } | ||
| return super.visit(tree, executionContext); | ||
|         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  ppkarwasz marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| }); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed likely best to merge this once released; thanks for creating the recipe before then!