在SLF4j中从Java更改日志记录输出文件的名称 [英] Change the name of a logging output file from Java in SLF4j

查看:59
本文介绍了在SLF4j中从Java更改日志记录输出文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用slf4j来管理我的应用程序日志记录.

I am using slf4j to manage my application logging.

我想在配置log4j.properties之后更改输出文件的名称.我的意思是,文件名是输入我程序的一个参数,并且是可变的.我想从Java更改此slf4j属性.

I want to change the name of the output file after configuring the log4j.properties. I mean, the file name is a parameter that enters into my program and is variable. I want to change this slf4j property from java.

有人知道这是否可能吗?

Does anyone know if it's possible?

提前谢谢!

推荐答案

最后,我解决了这个问题:

Finally I solved the problem:

public void updateFichierLogNames(String warnFileName, String infoFileName, String errorFileName) {
        Properties props = new Properties(); 
    try { 
        InputStream configStream = getClass().getResourceAsStream( "/log4j.properties"); 
        props.load(configStream); 
        configStream.close(); 
    } catch (IOException e) { 
        System.out.println("Error: Cannot laod configuration file"); 
    } 

    props.setProperty("log4j.appender.infoFile.File", infoFileName); 
    props.setProperty("log4j.appender.warnFile.File", warnFileName); 
    props.setProperty("log4j.appender.errorFile.File", errorFileName); 

    LogManager.resetConfiguration(); 
    PropertyConfigurator.configure(props); 

    }
}

这篇关于在SLF4j中从Java更改日志记录输出文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆