Logback - set log file name programmatically

后端 未结 4 1445
天涯浪人
天涯浪人 2020-12-04 19:19

I am using logback, and I am trying to set the log file name programmatically within my Java program (similar to Setting Logback Appender path programmatically), and I tried

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 20:07

    Here is what you can do to ignore those extra file creation.Below is the config file

    
    
    
    c:/logs/${application-name}.log
    
    %d %p %t %c - %m%n
    
    
    
    
    
      
    

    Here is the java part,

    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator jc = new JoranConfigurator();
    jc.setContext(context);
    context.reset(); // override default configuration
    // inject the name of the current application as "application-name"
    // property of the LoggerContext
    context.putProperty("application-name", NAME_OF_CURRENT_APPLICATION);
    jc.doConfigure("/path/to/the/above/configuration/file.xml");
    

    I got this from here http://logback.qos.ch/faq.html#sharedConfiguration

提交回复
热议问题