Search code examples
logginglog4j

log4j relative path to create log file


Here is my log4j.properties file

# Define the root logger with appender file
log = D:/workspaces/Abhishek/Automation/MOPS/logs
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{ABSOLUTE} %5p  - %m%n

This does job for me only problem is the absolute path i have used. I want to use relative path now.

Below is my folder structure

--src
      --com
      --log4j.properties
--logs
     --log.out file

So my logs folder is outside the src folder where log4j.properties is situated?

How can i define relative path in such case. In case of jsp's we do it like ../webpages/page.jsp. How can it be achieved here ??


Solution

  • you can pass dynamic value like this

    {logfileLoc}/logs/file.log.
    

    in your case you want in the project folder itself so can have -D (java argument or equivalent) param like -D logfileLoc =D:/workspaces/Abhishek , and you can access with variable logfileLoc. and consider changing this value for different environments.