Hi everybody. I'm an old C/C++ guy trying to get into Java. It's a weird new world. Here I've got a HelloWorldOfLogging code chunk that's not cooperating yet.
The code builds and it logs to the console but only to ERROR. Nothing I do to the log4j.properties file has any effect on this: no change in format, no change in log level, nada.
Previously it complained that it couldn't find the properties file, giving me this output (top line is log4j, bottom four are mine):
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
22:57:48.398 [main] ERROR HelloWorld - error test
22:57:48.399 [main] ERROR HelloWorld - Debug isn't enabled. :(
22:57:48.399 [main] ERROR HelloWorld - Info isn't enabled. :(
22:57:48.399 [main] ERROR HelloWorld - Warn isn't enabled. :(
This much more common issue was an easy fix: adding a single argument to the JVM itself (via the IntelliJ project configuration):
-Dlog4j.configurationFile=(FULLY SPECIFIED PATH HERE)
Which removed the log4j error message but otherwise gave me the same log output:
22:57:48.398 [main] ERROR HelloWorld - error test
22:57:48.399 [main] ERROR HelloWorld - Debug isn't enabled. :(
22:57:48.399 [main] ERROR HelloWorld - Info isn't enabled. :(
22:57:48.399 [main] ERROR HelloWorld - Warn isn't enabled. :(
So, the output's totally unchanged otherwise, suggesting that it's finding the properties file but not using it.
The log4j.properties file I'm using is ripped right from the doc:
# 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
And here's my very simple code:
import org.apache.log4j.Logger;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = Logger.getLogger(HelloWorld.class);
logger.info("Hello World of logging!");
logger.info("Working Dir: " + System.getProperty("user.dir"));
logger.debug("debug test");
logger.info("info test");
logger.warn("warn test");
logger.error("error test");
if (!logger.isDebugEnabled())
logger.error("Debug isn't enabled. :(");
if (!logger.isInfoEnabled())
logger.error("Info isn't enabled. :(");
if (!logger.isWarnEnabled())
logger.error("Warn isn't enabled. :(");
}
}
I've also tried using the BasicConfigurator from earlier examples to no avail. :\
If anybody's ever seen this or otherwise has any idea what I might do to get to the bottom of this, I'd very much appreciate it. I'm at a complete loss at this point.
Thanks so much in advance.