This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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

log4h.configurationFile behaves differently than the log4j.configuration setting from 1.2

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Log4j 1.2, I used log4j.configuration to specify a log4j.server.properties. then, in the root of my classpath both a log4j.properties and a log4j.server.properties were there. Thus, in localhost, with no system property set, the former would direct logging output to the console of the IDEas specified in log4j.properties. But on the server, the log4j.configuration=log4j.server.properties would be set, and output would go to the various files in the logging folders like we want on the servers in our various environments, as defined in log4j.server.properties.

I want to do the same thing on log4j 2. But the log4j.configurationFile appears to demand an absolute URL. But the absolute file path will be different for each webapp installed on that server. I just want to specify log4j2.server.xml to get the same effect I got under log4j 1.2. When I try, I get "Unable to access log4j2.server.xml java.lang.IllegalArgumentException: URI is not absolute".

How can I get the same effect I got under 1.2, without having some script replace the file?
 
Saloon Keeper
Posts: 21123
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's never a good idea to use relative paths for resources in a webapp. You don't know where they'll end up. Especially if the server does internal "change working directory" operations at random intervals - which it's fully entitled to do, because "working directory" is not part of the J2EE web application spec.

In my own case, I normally use the log4j.xml file in the WAR classpath and have a separate console logger which is used both by the IDE and as part of the server log in production. Logs pertaining to various subsystems I made using absolute paths. Then again, the conventions of /var/log or /opt/product/log are fairly well-established under Linux, so I can demand those locations with relative impunity. If you're doing stuff that takes you into the Windows world, there are fewer established conventions.

So what I would probably do is simply define the log directory root for the app as a context environment entry variable, set up a JNDI lookup in the logger initialization code to obtain it, and construct the logger channels from there.

Since I typically have Spring managing my webapp assets, I'd first check the relevant Spring docs, since with any luck, that can all be done in the Sprint application context config file.

 
Brian Mulholland
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

The log4j docs themselves recommend putting the config files in your classpath root. I am doing exactly that, just like you say you are. But I merely want to have two there, and control which one is used by the environment setting log4j makes available. Log4j 1.2 makes this possible because I can give a simple file name to replace the default. Log4j 2's preference for absolute paths makes it brittle. I run multiple apps in the same jvm on our server, so absolute paths absolutely doesn't work. I can't specify /opt/long path to my webapp/WEB-INF/classes/log4j.server.xml because that long path would include the SPECIFIC application. Thus the config would not affect the other applications. See the dilemma?
 
Tim Holloway
Saloon Keeper
Posts: 21123
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got confused here. There's 1 and only 1 WEB-INF/classes directory per webapp. No sharing. So each webapp would have to have its own config file anyway, regardless of whether the stuff within that directory points, relative or absolute.
 
Brian Mulholland
Ranch Hand
Posts: 68
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOLVED - So, I was doing it right. The log4j.configurationFile DOES allow log4j2.server.xml just as I wanted. But we had a lingering log4jConfigListener defined in our web.xml that I didn't know about (darn co-workers, always doin' stuff). For some reason, when this was there, that config DEMANDED an absolute URL. When I removed it, everything worked....smooth as butter.
 
if you think brussel sprouts are yummy, you should try any other food. And this 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!