configure log4j to log to custom file at runtime

后端 未结 4 452
逝去的感伤
逝去的感伤 2020-11-28 02:40

Can anyone please guide me as to how I can configure log4j to log to a specific file that I specify at run-time.The name and path of the log file are generated at run-time a

4条回答
  •  野性不改
    2020-11-28 03:05

    You can also do this from the log4j.properties file. Using the sample file below I have added the system property ${logfile.name}:

    # logfile is set to be a RollingFileAppender
    log4j.appender.logfile=org.apache.log4j.RollingFileAppender
    log4j.appender.logfile.File=${logfile.name}
    log4j.appender.logfile.MaxFileSize=10MB
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c - %m%n
    

    The log file name can then be set two different ways:

    1. As a command line, system property passed to java "-Dlogfile.name={logfile}"
    2. In the java program directly by setting a system property (BEFORE you make any calls to log4j).

      System.setProperty("logfile.name","some path/logfile name string");

提交回复
热议问题