0

I upgraded to log4j2 and faced with this problem which I could resolve:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I resolved it by following section Log4j 1.x API Bridge from Apache documents. I added this code to pom.xml:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>2.17.2</version>
</dependency>

However, another problem appeared as follows:

log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

It is very strange as I have already added log4j-jcl to pom.xml. How should I fix this problem?

Hard to provide a MWE. Any extra information you need, please mention in the comment.

Note: Please avoid any solution that tries to add a backdoor to log4j 1 or create log4j.properties for v1. I have a problem just because I am avoiding log4j v1.


mvn dependency:tree|grep log4
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-jcl:jar:2.17.2:compile

.

mvn dependency:tree

...
[INFO] +- org.hibernate:hibernate-c3p0:jar:5.6.7.Final:compile
[INFO] |  \- com.mchange:c3p0:jar:0.9.5.5:compile
[INFO] |     \- com.mchange:mchange-commons-java:jar:0.2.19:compile
...

log4j2.properties:

...

log4j.logger.org_apache_commons_beanutils.name = org.apache.commons.beanutils
log4j.logger.org_apache_commons_beanutils.level = FATAL

logger.org_hibernate.name = org.hibernate
logger.org_hibernate.level = FATAL

logger.org_hibernate_tool.name = org.hibernate.tool
logger.org_hibernate_tool.level = FATAL

logger.org_hibernate_mapping.name = org.hibernate.mapping
logger.org_hibernate_mapping.level = FATAL

logger.org_hibernate_hql.name = org.hibernate.hql
logger.org_hibernate_hql.level = FATAL

logger.org_hibernate_dialect.name = org.hibernate.dialect
logger.org_hibernate_dialect.level = FATAL

logger.org_hibernate_engine.name = org.hibernate.engine
logger.org_hibernate_engine.level = FATAL

logger.com_mchange.name = com.mchange
logger.com_mchange.level = FATAL

...

.

mercury
  • 189
  • 1
  • 15

1 Answers1

0

The problem you are having is still the same from the beginning: you either have the log4j:log4j artifact on the classpath or its clone ch.qos.reload4j:reload4j.

Adding log4j-jcl was a palliative measure: libraries that use Jakarta Commons Logging, now use Log4j2 in your application. However libraries that use Log4j 1.x directly use one of the artifacts mentioned above.

To solve the problem you simply need to exclude both log4j and reload4j from the artifacts that use it and add:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.17.2</version>
</dependency>

Edit: guessing from your comment about an Eclipse specific .classpath file, you are running your application from Eclipse. Eclipse does not distinguish normal and optional dependencies. mchange-commons-java has an optional dependency on log4j, that is why it does not appear in mvn dependency:tree listing. However Eclipse treats it as normal dependency and adds it on the runtime classpath.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • 1
    The whole point is that I want to completely shift from log4j 1 to log4j2 and never ever use the v1 or `log4j-1.2-api` anymore. – mercury Mar 27 '22 at 23:42
  • While your code does not use Log4j 1.x, your legacy libraries do. You have three choices: 1. upgrade them to a version that does not use Log4j 1.x, 2. remove them if they are not needed, 3. use `log4j-1.2-api` instead of `log4j`. Regarding the MChange artifacts, they seem to use SLF4J instead of directly Log4j 1.x, so adding [`log4j-slf4j-impl`](https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl) to the classpath should help. – Piotr P. Karwasz Mar 28 '22 at 08:32
  • Thanks Piotr, I am a beginner at Java. How should I add something to classpath? Is it about the `.classpath` file? – mercury Mar 29 '22 at 03:33
  • Another question, is it possible to have `log4j-1.2-api` for the submodules but `log4j2` for the main application? – mercury Mar 29 '22 at 03:36
  • Check [What is a classpath and how do I set it?](https://stackoverflow.com/q/2396493/11748454). Maven adds all `compile` and `runtime` dependencies, when it runs the application. Your IDE (Eclipse?) also shows you the dependencies that will be put on the classpath. Maven modules can have different dependencies, but it is usually better to use [dependency management](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management) in the main module. – Piotr P. Karwasz Mar 29 '22 at 06:22
  • I think by classpath you mean `pom.xml`. – mercury Mar 29 '22 at 22:39
  • The concepts are strictly linked: the `pom.xml` file determines the classpath for the compiler and the runtime classpath. I think I can guess what the source of your problem is: you are running your application using Eclipse, which does not support [optional Maven dependencies](https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html). `mchange-commons-java` has an **optional** transitive dependency on `log4j`, but Eclipse treats it as normal dependency and puts it on the runtime classpath. – Piotr P. Karwasz Mar 30 '22 at 06:09