Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Configuring log4j once instead of in every class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to use log4j in a project I have. This project is a mix of servlets and regular classes. Currently, in every class that needs to user a Logger, I do 2 steps:

1. Make a logger object for that class

2. Configure it with PropertyConfigurator and the log4j.properties file i use

Information on the correct use or common usage of log4j seems sparse other than the apache page (or maybe my google skills are just lacking), but I wanted to know is this an accepted way of using log4j? Is there a way to simply configure once in the whole project and just make a new Logger object for every class that needs it?

Thanks for any help
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never do anything but configure loggers in a log4j.properties files and create one in each class. What specifically are you having an issue with?
 
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, I use Log4J through the commons logging API so that we can revert to Java logging if need be.

That said, I create a static logger for every class. For example:
Then it's just a matter of setting up the log4j.properties file properly.

If you are doing anything more than that, you're over-engineering the problem....
 
Bob Homes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No issue specifically. I was just wondering if that was a common way of doing things.

Well actually, there is one issue. I was going to post a new topic about this, but maybe I will just post here. How do you reference your .properties file? Like what file path do you use?

This whole project is run in tomcat, so right now, for the PropertyConfigurator.configure() parameter, i use something like "../webapps/ProjectName/WEB-INF/classes/resources/log4j.properties", with the appropriate slashes for the environment, but that strikes me as not very portable. I'm betting on the fact that Tomcat has to be started in the TOMCAT_HOME/bin directory, but is there a better, more elegant way to handle this?
 
Bear Bibeault
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Homes wrote:How do you reference your .properties file?


I don't. I drop the file into Tomcat's /lib folder and forget about it.

Forget about a configurator. Just drop the Log4J jar file and the commons logging jar file into the /lib folder along with the log4j.pproperties file, and let it do the rest.
 
Bob Homes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Bob Homes wrote:How do you reference your .properties file?


I don't. I drop the file into Tomcat's /lib folder and forget about it.

Forget about a configurator. Just drop the Log4J jar file and the commons logging jar file into the /lib folder along with the log4j.pproperties file, and let it do the rest.



Ok that worked. But instead of putting it in the lib folder, I put it in a package so it ends up in the classes folder of the project. This way the file doesn't have to be placed anywhere manually by the user.

Thanks for the help.
 
Bear Bibeault
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's cool, as long as it's in the classpath.

Placing it in Tomcat's lib folder allows you to control the logging that Tomcat itself emits.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!