log4j migration to log4j2
Asked Answered
V

1

6

Log4j migration guide

https://logging.apache.org/log4j/2.x/manual/migration.html

states that

Calls to org.apache.log4j.Logger.getLogger that accept a LoggerFactory must remove the org.apache.log4j.spi.LoggerFactory and use one of Log4j 2's other extension mechanisms.

What are the extension mechanisms available in log4j2 and how best to migrate a method like below in log4j2

private static TraceLoggerFactory stTraceFactory = new TraceLoggerFactory();

public static Logger getTraceLogger(final String name) {
    return getLogger(name, stTraceFactory);
}


class TraceLoggerFactory implements LoggerFactory {
    @Override
    public Logger makeNewLoggerInstance(final String name) {
        return new TraceLogger(name);
    }
}
Voracious answered 24/6, 2015 at 9:59 Comment(1)
you might need to use log4j2 with slf4j for using LoggerFactory.Delusion
D
1

Not Exactly related to the Question, But the below are my learning in log4j2 migration. hope it helps someone.

Since the log4j vulnerability which was discovered in late 2021, many organizations have upgraded to log4j2 to overcome it. and many developers needed to understand what exactly is log4j and what is the vulnerability. I recommend you to read about that, as it was a major vulnerability in software industry. I also migrated 2 applications to log4j2 and faced many issues in the migration. hence writing this here. i will write a detailed article on this later but for the time being, if anyone else is migrating to log4j2 from log4j i prefer reading my below learnings.

  1. This is a major change in the application, so be careful while doing it. it involves a change in almost all the files present.
  2. If you are making a change to a legacy application, you might need to change many other supported jars, hence understand that it's gonna take time.
  3. If there is a log4j.properties file, convert it to log4j.xml ,it might feel hard to convert but trust me you wont find much docs on properties file.
  4. A REPLACE ALL method wont always work, double check your work.
  5. There will be transitive dependencies, try to understand that and exclude them in pom.
  6. If you are changing versions of spring or any other framework, understand that there were many changes done in the new versions, refer docs.

documentation for migrating from Log4j to Log4j2: https://logging.apache.org/log4j/2.x/manual/migration.html#Log4j2ConfigurationFormat

Some sites that helped me: https://stackify.com/log4j2-java/ https://www.digitalocean.com/community/tutorials/log4j2-example-tutorial-configuration-levels-appenders https://howtodoinjava.com/log4j2/log4j2-consoleappender-example/ https://access.redhat.com/solutions/4347841

Delusion answered 30/5, 2023 at 10:12 Comment(1)
The introduction is ironically true: many companies decided to migrate from Log4j 1.x to Log4j 2.x, although Log4Shell did only affect Log4j 2.x. You can run some OpenRewrite Logging recipes to automatically convert Log4j 1.x to Log4j 2.x.Applicator

© 2022 - 2024 — McMap. All rights reserved.