Skip to content
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

Logback to log4j2 support #97

Open
Misha123456 opened this issue Mar 16, 2023 · 6 comments
Open

Logback to log4j2 support #97

Misha123456 opened this issue Mar 16, 2023 · 6 comments
Labels
recipe Recipe Requested

Comments

@Misha123456
Copy link

I was reading the documentation here and can see log4j2 to logback conversion is supported.
But there is no way to convert vice versa (from logback to log4j2).

Are there plans to implement this in the future?

@timtebeek
Copy link
Contributor

hi @Misha123456 ! We had not planned to support a migration from logback to log4j2, but are open to contributions in case you're interested to pick that up. Implementation wise I imagine it will be very similar to the log4j2 -> logback recipes. If you're merely looking to switch dependencies you could have a look at META-INF/rewrite/logback.yml, but a full migration might also involve converting for instance logback.xml.

@timtebeek timtebeek added the question Further information is requested label Mar 17, 2023
@timtebeek timtebeek added recipe Recipe Requested and removed question Further information is requested labels May 6, 2023
@ppkarwasz
Copy link
Contributor

@timtebeek,

Could you assign this issue to me?

I would split it into two independent recipes:

  • "SLF4J to Log4j API", which mostly what library developers are interested in,
  • "Logback to Log4j Core", which would be targeted to application developers and just switches dependencies and configuration.

@timtebeek
Copy link
Contributor

Sure sounds good; Thanks for taking this on!

ppkarwasz added a commit to ppkarwasz/rewrite-logging-frameworks that referenced this issue Dec 26, 2023
This PR adds a rule to convert code written with SLF4J 1.x/2.x to Log4j
API.

It covers:

 * basic `Logger` methods,
 * compatible `LogBuilder/LoggingEventBuilder` methods,
 * `MDC` to `ThreadContext` conversion,
 * `Marker` conversion,
 * dependency switch from `slf4j-api` to `log4j-api`.

Solves part of openrewrite#97: it **does** not cover conversion between Logback and
Log4j Core, since the choice of a logging implementation is **totally**
independent from the choice of the API.
timtebeek added a commit that referenced this issue Dec 27, 2023
* Add SLF4J 2.x to Log4j API rewrite rule

This PR adds a rule to convert code written with SLF4J 1.x/2.x to Log4j
API.

It covers:

 * basic `Logger` methods,
 * compatible `LogBuilder/LoggingEventBuilder` methods,
 * `MDC` to `ThreadContext` conversion,
 * `Marker` conversion,
 * dependency switch from `slf4j-api` to `log4j-api`.

Solves part of #97: it **does** not cover conversion between Logback and
Log4j Core, since the choice of a logging implementation is **totally**
independent from the choice of the API.

* Indent tests as per project convention

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
@timtebeek
Copy link
Contributor

We recently completed quite a few recipes to move from JUL to SLF4J; those are now under:
https://github.com/openrewrite/rewrite-logging-frameworks/tree/main/src/main/java/org/openrewrite/java/logging/slf4j

I suspect that the approach taken there could be largely followed here: Mostly use declarative or Refaster recipes, and complement those with explicit recipes as needed, which wouldn't need much.

@ppkarwasz
Copy link
Contributor

We have recently revamped our documentation with:

Unlike the migration from JUL to SLF4J, which requires mostly code changes, migrating from Logback to Log4j Core requires:

  1. A migration of runtime dependencies, which can be easily done with the current declarative Maven/Gradle recipes.
  2. A migration of the configuration file.

The latter requires most of the work, since the configuration options of every standard Logback plugin must be matched to the corresponding options of a Log4j Core plugin. Starting October 2024, I plan to add support for a Logback-to-Log4j Core configuration conversion to the log4j-config-converter artifact introduced in apache/logging-log4j-transform#93. This artifact can then be added as a dependency of the OpenRewrite recipe.

@timtebeek
Copy link
Contributor

Nice to see, thanks! Spotted this note in the docs that ought to be an easy recipe for your users too.

Note that LogManager.getLogger(Foo.class) can be simplified as LogManager.getLogger(), if Foo is the enclosing class of the field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe Requested
Projects
Status: Backlog
Development

No branches or pull requests

3 participants