Dumitru Vlad
Dumitru Vlad

Reputation: 109

Log4j, how to put log file into dynamic directory location

can anyone show me how can I put my log file with Log4j in my project location? I want to put my log file into src/main/resources every time. I don't want to care about me location on disk of my project, for sample: if I have my project in D:/project and after that I change my location project in C:/project I want to put my log file on every time on src/main/resources with out change this line to this

log4j.appender.file.File=C:\\project

with this

log4j.appender.file.File=D:\\project

Have anyone any idea? Thx in advance for help :)

Upvotes: 3

Views: 3658

Answers (2)

m.hassaballah
m.hassaballah

Reputation: 250

You can define variable in your code using this code

System.setProperty("path", System.getProperty("user.dir"));

and change appender to be like that

log4j.appender.file.File=${path}/src/main/resources

Upvotes: 0

Kalyan Chavali
Kalyan Chavali

Reputation: 1348

You can write logic to determine if your application is being run in C:\... or D:\... by retrieving the current directory and set a system variable say "logPath" as below

System.setProperty("logPath", myPath); // where myPath is either C:\... or D:\... based on your logic

Then use this property in log4j.properties

log4j.appender.file.File=${logPath}src\main\resources\MyApplicationLog.log

Note : you would need to make sure that the system variable is set before log4j is initialized.

Upvotes: 3

Related Questions