Sign in to follow this  
serratemplar

log4j.properties has no effect

Recommended Posts

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.

Share this post


Link to post
Share on other sites

Even though I'm totally not an expert in terms of logging in Java (I think it's quite confusing for people...), my guess is that you use the "wrong" appender. I don't know how to configure it properly for your case and my advice could be completely wrong, but try using
log4j.appender.A1.target=System.out

and see if you get any output. There's another configuration example right above the headline "Default Initialization Procedure", where they use a ConsoleAppender.

 

And if all this doesn't work, this configuration definitely works in a project of mine, so you could give it a try.

log4j.rootLogger=info, stderr, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.stdout.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.stdout.filter.a.LevelMin=TRACE
log4j.appender.stdout.filter.a.LevelMax=WARN

Share this post


Link to post
Share on other sites

TY hannesp. I tried both of your recommendations: unfortunately, no dice. The behavior is precisely the same.

 

It's as if it's ignoring the file's contents, but content that the file's there. I must be doing something wrong but I'm at a loss as to what. :\

Share this post


Link to post
Share on other sites

Woa I'm sorry, totally overlooked the fact that it seems that your versions simply don't match. You are using a newer version of log4j (2), but a configuration style of the old version. Try to use https://logging.apache.org/log4j/2.x/manual/configuration.html as a template instead of the example you used until now. The section "Configuration with Properties" should do it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this