aspose file tools*
The moose likes JBoss/WildFly and the fly likes log4j.xml system.property at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "log4j.xml system.property at runtime" Watch "log4j.xml system.property at runtime" New topic
Author

log4j.xml system.property at runtime

C Weaver
Greenhorn

Joined: Jun 28, 2012
Posts: 6
I've read all the articles and tried all the solutions. I think my issue is that JBoss is parsing my log4j.xml file at startup and not at run time. I have a j2ee interface that is called by the users. I have the following in my class:

static { System.setProperty("my.log.file", LogPath.getLogPath());}
private static final Logger log = Logger.getLogger( MyClass.class );

My appender is set up as:

<param name="file" value="${my.log.file}/appLog.log"/>

When the class is called. I expected the setProperty to store the path and then when I create my logger, for it to read the log4j.xml and parse the correct path name. but that's not what happens, I get a null pointer and no logs.

if I change the property to the JBoss server log directory, all is well and I get my log file.

<param name="file" value="${jboss.server.log.dir}/appLog.log"/>

I do NOT want my log file in the JBoss default folder.

So, my question, is there a way to force JBoss to reload my log4j.xml, is that the right approach, is it thread safe, and how costly is it. I expect this interface to get moderate to heavy usage and reloading the xml each time could bring the systems to it's knees.

Anyone have any ideas?

Thanks,
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

my.log.file is being set *AFTER* the <param> is evaluated.

Try adding -Dmy.log.file={whatever} on the VM startup parameters and see what happens.

WP
C Weaver
Greenhorn

Joined: Jun 28, 2012
Posts: 6
Fixed the issue with this http://www.avajava.com/tutorials/lessons/how-do-i-initialize-log4j-in-a-web-application.html?page=1

Basically, I need to set the system property before anything else does, so the above approach makes sure that I call a servlet right off the bat that has my set properties in it.

This worked like a champ!

Thanks,
C Weaver
Greenhorn

Joined: Jun 28, 2012
Posts: 6
Thanks for the reply WP,

I was trying to figure out a solution where I controlled everything in my app. I did not want to get my system admins involved as it takes days to get anything out of them.

Basically, the log4j.xml is only ready one once the first time your app is accessed. Since my test client was not the first call, the xml had already been loaded. The solution I posted above forces a servlet to execute first before anything else does and I can load my system property at that time.

Kinda handy to have in the project.

Thanks,
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Glad you fixed it, and Thanks for sharing that link.

WP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: log4j.xml system.property at runtime