1

I must be doing something stupid, but I can't find it. I'm trying to add Log4j to my project, but it isn't reading the properties file. I have created a minimal workable example project. In IntelliJ, I created a new gradle project. I added log4j and added a main class.

Here is the project structure.

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── Main.java
    │   └── resources
    │       └── log4j.properties
    └── test
        ├── java
        └── resources

build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
}

log4j.properties

log4j.rootLogger=TRACE

Main.java

import org.apache.logging.log4j.LogManager;

public class Main {
    public static void main(String[] args) {
        System.out.println("Print");
        System.out.println("Root Logger Level: " + LogManager.getRootLogger().getLevel());
    }
}

When I run this, the output says that the root logger's level is "ERROR", even though I'm setting it to trace. Of course this means that any log statements below error level are not printed.

Print
Root Logger Level: ERROR

Every piece of documentation I read says that the properties file needs to be in a directory on the classpath, which makes sense. According to the gradle documentation and my own investigation, the resources directory is on the classpath. Experimentally, I have tried placing the file at every level of the hierarchy with no difference. I can't see any reason why the properties file isn't being found.

I tried specifying it directly by adding -Dlog4j.configuration=<full/path/to/properties> to the VM arguments. This still didn't work, but gradle gives me the following output:

BUILD SUCCESSFUL in 748ms
3 actionable tasks: 1 executed, 2 up-to-date
ERROR StatusLogger Reconfiguration failed: No configuration found for '2a139a55' at 'null' in 'null'
10:23:30 PM: Task execution finished 'Main.main()'.

This appears to happen after the program has terminated. I'm not sure if it's related.

I must be missing something stupid, but I can't find anything. I've looked at dozens of examples and it seems like I'm doing everything right. What am I missing?

TechnoSam
  • 578
  • 1
  • 8
  • 23
  • Could you check that IntelliJ is not excluding the resources folder? Sometimes this happens in eclipse. Here you are a reference: https://www.jetbrains.com/help/idea/content-roots.html#adding_content_root – sirandy Sep 10 '20 at 02:41
  • @sirandy That doesn't appear to be the case. The project structure lists resources as a content root, and attempting to add it results in an error saying "content entries should not intersect". Furthermore, the properties file is in the build output, so I expect that it isn't being ignored here. – TechnoSam Sep 10 '20 at 02:48
  • Ok, then try adding this lines to your gradle configuration file. `compile group: 'org.slf4j', name: 'slf4j-api', version: '1.8.0-beta2' compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.11.1' compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'` The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time. – sirandy Sep 10 '20 at 02:57
  • This is interesting, when I add a SLF4J logger, I get an error "No SLF4J providers were found." But I can use Log4j with error and fatal, so I know I have the library loaded. I'm not sure what this reveals, but it's something. – TechnoSam Sep 10 '20 at 03:08
  • All right! This is a kind of warning instead of an error. If you want to dismiss it you should use an slf4j implementation. Since you are using log4j you could use `slf4j-log4j12` instead `org.apache.logging.log4j` it's just a recommendation. For more info check this answer: https://stackoverflow.com/q/54652836/1670134 – sirandy Sep 10 '20 at 03:13
  • I'm still not able to control the log level with my file, and I still can't see any output below error. It seems like not having a provider might be a big deal. I added the dependency suggested in the linked answer, but nothing changed. However I don't want to waste too much time on this particular warning if it's not the root cause of my issues. Right now, even with SLF4J, I still don't get any output below "ERROR". – TechnoSam Sep 10 '20 at 03:28
  • Try modifying your log4j.properties file. This is a configuration that has worked for me: `# Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # Print only messages of level WARN or above in the package com.foo. log4j.logger.com.andy=DEBUG` – sirandy Sep 10 '20 at 03:41
  • No dice. I realized I forgot to mention that I'm not getting any output at all from the SLF4J call, even with level "ERROR". Even the default config should display that, so something else is wrong. I'm going to bed for tonight, will check back in the morning. Thanks so much for the help so far! – TechnoSam Sep 10 '20 at 03:48
  • I eventually solved it! This answer here helped me out: https://stackoverflow.com/a/49893867/3287359 For some reason, the resources weren't getting loaded, but removing and re-adding the directory to the module fixed the problem! It probably makes sense to mark this as a duplicate of the linked question. – TechnoSam Sep 11 '20 at 03:34

2 Answers2

1

To the original question, because you are trying to use log4j2, the preferred mode of configuration is xml file. You can refer to manual: https://logging.apache.org/log4j/2.x/manual/configuration.html

So to use the version

System.setProperty(ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY, "src/main/resources/configuration.xml");

Soni
  • 142
  • 8
  • This is the closest I've gotten so far! First, since this is log4j2, I want to set `ConfigurationFactory.CONFIGURATION_FILE_PROPERTY`, not what you said. This works if I create the logger after setting this config, so it doesn't work for classes that have the logger as a static member, which is what most do. So now that I know this, I know it's not finding my config file for sure. For some reason, nothing is on my classpath. I'll have to continue to investigate. – TechnoSam Sep 11 '20 at 00:47
  • When you say config file, do you mean the xml config or the .properties file? – Soni Sep 11 '20 at 02:14
  • It wasn't working for either. I eventually solved it! This answer here helped me out: https://stackoverflow.com/a/49893867/3287359 For some reason, the resources weren't getting loaded, but removing and re-adding the directory to the module fixed the problem! – TechnoSam Sep 11 '20 at 03:33
0

The following solution helped me. As per the documentation mentioned on https://logging.apache.org/log4j/2.x/faq.html, we have to rename log4jxml to log4j2.xml as this is the new naming convention and place this in src/main/resources.In my case this issue was solved as soon as I did this.

vivek
  • 1