如何自定义应用程序日志?

时间:2019-04-16 05:20:27

标签: java spring-boot slf4j application.properties

我正在使用API​​ Rest,并且需要有一个特定的日志文件,其名称类似于我的组织。创建或编辑组织时,我的API必须创建其文件(如果不存在),然后保存其数据。我的application.properties中已经有了默认路径文件的此参数。

logging.file=/Users/xxxxxxxxx/application.log

在我的Java类中,特别是在我的编辑方法中,如果没有创建文件,我将创建文件。

 String fileLog = "/Users/xxxxxxxxxxxx/logs/" + orgId + ".log";
 File file = new File(fileLog);
 BufferedWriter bw;
 if (!file.exists()) {
    // crea el archivo para la Org si no existe
    bw = new BufferedWriter(new FileWriter(file));
 }

最后,我设置了我的(logging.file)的默认定义,为该组织定义了新的日志文件。

PropertiesConfiguration config = new 
PropertiesConfiguration("/Users/xxxxxxxx/application.properties");
config.setProperty("logging.file", "/Users/xxxxxxxxxxxx/logs/" + 
orgId + ".log");
config.save();

我的最后一个问题是:如何在不重新启动服务器的情况下重置日志管理器?

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用application.properties。最常见的定制是:

logging.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup.
相关问题