How to give dynamic file name in the appender in log4j.xml

后端 未结 5 1710
情深已故
情深已故 2020-11-28 06:16

I am using log4j to log information. I have used a log4j.xml file for creating log files. I have given the absolute path for each log file as a param

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 06:28

    It's much easier to do the following:

    In log4j.xml define variable as ${variable}:

        
        
        
            
               
    
    

    Then make sure you set the system property when you start your JVM such as:

    java -Dlogfilename=my_fancy_filename  example.Application
    

    That will create a dynamic log file name: my_fancy_filename.log

    Alternatively, you can set the system property in code so long as you do it before you create a logger (this is useful if you want your PID in your logs for instance). Such as:

    System.setProperty("logfilename", "a_cool_logname");
    

    Once that is set you can go ahead and get your loggers as normal and they will log to the dynamic file (be careful of those static Loggers that create loggers before your main method executes).

提交回复
热议问题