Log file path set at runtime

I believe my logback configuration issue should be common but I am unable to find a satisfactory resolution. I'll first describe what I'm trying to achieve. I have a simple application that is deployed somewhere on the file system as follows: MyApp |- bin |- myExecJar.jar |- lib |- conf |- logback.xml |- logs At runtime my application computes the absolute path of the MyApp directory (e.g., /home/user1/apps/MyApp) using reflection. A system property called "app.base.path" is set to this computed path. This path is computed so that some other misc. paths are resolved relative to the value of "app.base.path" and are independent of the working directory (i.e., the value of the "user.dir" property) that the application was started from. In my logback.xml configuration file I have the line <file>${app.base.path}/logs/logback.log</file> to specify where the log file will be written to. My problem is this. Upon running "java -jar myExecJar.jar", the logback.xml file is read from the classpath (set in the jar manifest) and processed before my code gets the chance to programmatically set the "app.base.path" property. I therefore end up with a directory being created as follows "/home/user1/apps/MyApp/bin/app.base.path_IS_UNDEFINED logs/logback.log". Any suggestions on the best way to achieve what I'm trying to do in logback? Thank you.

Fud, It sounds obvious but you have to initialise your system property before you initialise logback. Logback initialises the first time LoggerFactory.getLogger() is called. So if you have you logger as a static member in your main class then it's going to get initialised too soon. In your main class you can have a logger member, but have it initially set to null. In your main method work out you app base property. Then do 'log = LoggerFactory.getLogger(...)'. I do some something like this and it works for me. David On 5 Aug 2010, at 16:29, fudmail <fudmail@gmail.com> wrote:
I believe my logback configuration issue should be common but I am unable to find a satisfactory resolution.
I'll first describe what I'm trying to achieve.
I have a simple application that is deployed somewhere on the file system as follows:
MyApp |- bin |- myExecJar.jar |- lib |- conf |- logback.xml |- logs
At runtime my application computes the absolute path of the MyApp directory (e.g., /home/user1/apps/MyApp) using reflection. A system property called "app.base.path" is set to this computed path.
This path is computed so that some other misc. paths are resolved relative to the value of "app.base.path" and are independent of the working directory (i.e., the value of the "user.dir" property) that the application was started from.
In my logback.xml configuration file I have the line <file>${app.base.path}/logs/logback.log</file> to specify where the log file will be written to.
My problem is this. Upon running "java -jar myExecJar.jar", the logback.xml file is read from the classpath (set in the jar manifest) and processed before my code gets the chance to programmatically set the "app.base.path" property. I therefore end up with a directory being created as follows "/home/user1/apps/MyApp/bin/app.base.path_IS_UNDEFINED logs/logback.log".
Any suggestions on the best way to achieve what I'm trying to do in logback?
Thank you. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user

David. Thank you! You were right. I didn't realize the class I was calling to compute the application's base directory just happened to have a static logger that called LoggerFactory.getLogger(...); which was the culprit. So, ensuring I set my system property "app.base.path" before any class can call LoggerFactory.getLogger(...) ensures the property is available when the logback.xml config file is processed. On Thu, Aug 5, 2010 at 12:30 PM, David Roussel <nabble@diroussel.xsmail.com> wrote:
Fud,
It sounds obvious but you have to initialise your system property before you initialise logback.
Logback initialises the first time LoggerFactory.getLogger() is called. So if you have you logger as a static member in your main class then it's going to get initialised too soon.
In your main class you can have a logger member, but have it initially set to null. In your main method work out you app base property. Then do 'log = LoggerFactory.getLogger(...)'.
I do some something like this and it works for me.
David
On 5 Aug 2010, at 16:29, fudmail <fudmail@gmail.com> wrote:
I believe my logback configuration issue should be common but I am unable to find a satisfactory resolution.
I'll first describe what I'm trying to achieve.
I have a simple application that is deployed somewhere on the file system as follows:
MyApp |- bin |- myExecJar.jar |- lib |- conf |- logback.xml |- logs
At runtime my application computes the absolute path of the MyApp directory (e.g., /home/user1/apps/MyApp) using reflection. A system property called "app.base.path" is set to this computed path.
This path is computed so that some other misc. paths are resolved relative to the value of "app.base.path" and are independent of the working directory (i.e., the value of the "user.dir" property) that the application was started from.
In my logback.xml configuration file I have the line <file>${app.base.path}/logs/logback.log</file> to specify where the log file will be written to.
My problem is this. Upon running "java -jar myExecJar.jar", the logback.xml file is read from the classpath (set in the jar manifest) and processed before my code gets the chance to programmatically set the "app.base.path" property. I therefore end up with a directory being created as follows "/home/user1/apps/MyApp/bin/app.base.path_IS_UNDEFINED logs/logback.log".
Any suggestions on the best way to achieve what I'm trying to do in logback?
Thank you. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
participants (2)
-
David Roussel
-
fudmail