log4j:WARN No appenders could be found for logger

Hi,
I am unable to initialize log4j for loggin purpose in my java spring mvc project.
I am using embedded jetty in it. So there is a main file which starts the server.
the log4j.properties file located under : resources->properties->staging,prod->log4j.properties.
Its unable to find it .
But when i just moved it from there to directly under resources it was able to find it.

But i want to keep it under this particular directory structure.
How can i do it ?

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Put it in the lib directory of the webapp
0
Rohit BajajAuthor Commented:
Actually i am running my application like : java -DFLOCK_APPS_CONFIG=staging -jar snippet.jar
so if the parameter is staging i need to pick the staging one. and if its prod i need to pick the prod one.
0
CEHJCommented:
Actually, i meant the classes directory, but it would seem now that you're NOT running a webapp. You need to pass a classpath to that command, which will include snippet.jar AND the log4j jar(s) AND log4j.properties
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Rohit BajajAuthor Commented:
You mean pass the file in the classpath to java command ?
But is there any way to do it from inside Java code ?
Also what i am wondering is that i have the following file structure  : resources->spring
Inside spring i have root-context.xml
In my web.xml i have :
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/root-context.xml</param-value>
    </context-param>

Open in new window


Its able to pick it up inside spring folder.
In an another xml i have :
    <context:property-placeholder
            location="classpath:properties/${FLOCK_APPS_CONFIG}/database.properties"
            ignore-unresolvable="true"/>

Open in new window


So my application is able to access files inside properties/staging or properties/prod  .
0
Rohit BajajAuthor Commented:
I need one more advice from you.
Is it a good idea to use log4j ?
Or are there better alternatives for logging ?
0
CEHJCommented:
Logback is really to be preferred (by the same authors)
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Because your log4j.properties is NOT in a standard location you need to tell Log4J where it is. You can use a system property (using the -D syntax on the command line when you start the app) or you could do it this way from inside the code...

// put this at the top of your main() method
PropertyConfigurator.configure("classpath:properties/" + System.getProperty("FLOCK_APPS_CONFIG") + "/log4j.properties");

Open in new window

0
Rohit BajajAuthor Commented:
Hi,
I added the line :
        PropertyConfigurator.configure("classpath:properties/" + System.getProperty("FLOCK_APPS_CONFIG") + "/log4j.properties");

Open in new window

But i am getting the following error :
DDVMACROBINSU:flock-snippets robinsuri$ java -DFLOCK_APPS_CONFIG=staging -DPORT=8080 -jar target/snippet.jar
log4j:ERROR Could not read configuration file [classpath:properties/staging/log4j.properties].
java.io.FileNotFoundException: classpath:properties/staging/log4j.properties (No such file or directory)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileInputStream.<init>(FileInputStream.java:93)
	at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)
	at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)
	at org.directi.code.AppStart.main(AppStart.java:30)
log4j:ERROR Ignoring configuration file [classpath:properties/staging/log4j.properties].
log4j:WARN No appenders could be found for logger (org.directi.code.AppStart).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Open in new window


Although the file is present at : properties/staging/log4j.properties
0
CEHJCommented:
classpath:properties cannot be part of the path to the config file in a Unix system (it contains a path separator) and is probably not part of it anyway.

I would advise against taking that approach: it's not good to bind the placement of the config file into code and it's also unnecessary. All you need to do is find out what the classpath for your container is (you should know that anyway) and make sure it's in it. <WEBAPP>/classes is usually a good bet
0
Rohit BajajAuthor Commented:
How do i pass the path to log4j.properties in the classpath while running java command as i am also passing a jar
I read that you cannot pass both classpath and -jar attribute to java command.

I tried passing -cp classes/properties/staging/log4j.properties...
But still it was unable to find the log4j file.
0
CEHJCommented:
I read that you cannot pass both classpath and -jar attribute to java command.

You understood correctly. Therefore you can omit -jar and just use the classpath. Example:

java -classpath classes/properties/staging:foo.jar:bar.jar a.b.SomeApp

Open in new window


(SomeApp in package a.b)
Note the path to the properties is a directory path and so should not contain the file name
0
Jim CakalicSenior Developer/ArchitectCommented:
If you set the System property log4j.debug you'll get a lot of really useful information about what log4j is doing at startup (including where it is looking for it's configuration). Just set -Dlog4j.debug=true on the JVM startup.

You can control specifically where log4j looks by setting the log4j.configuration System property to the full path location of the file. I think this is a URL so you'll need to prefix your path with the file protocol.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Notwithstanding the points raised by CEHJ, (which incidentally are a result of your choice to do the environment selection at run-time and not at build-time like I suggested in one of your other questions), if you still wish to select the config file at runtime based on the system property, try this code instead...

// put this at the top of your main() method
PropertyConfigurator.configure(AppStart.class.getResourceAsStream("/properties/" + System.getProperty("FLOCK_APPS_CONFIG") + "/log4j.properties"));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Yes, that's a good idea, as it cleverly uses the 'look in several places' classloading approach of resource loading in Java. However, i still think there's no substitute for having accurate knowledge of your container's classloading behaviour.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.