简体   繁体   中英

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 :)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM