简体   繁体   English

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

[英]How can I customize application logs?

I'm working in an API Rest, and I need to have an specific log file named like my organization.我在 API Rest 中工作,我需要一个像我的组织一样命名的特定日志文件。 When I'm creating or editing an organization my API must create its file if isn't exists and then save its data.当我创建或编辑组织时,我的 API 必须创建其文件(如果不存在),然后保存其数据。 I already have this parameters in my application.properties with a default path file.我的 application.properties 中已经有这个参数,带有默认路径文件。

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

In my Java class, specifically in my edit method I'm creating the file if isn't created.在我的 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));
 }

Finally I set the default definition of my (logging.file), defining the new log file for this organization.最后,我设置了我的 (logging.file) 的默认定义,为这个组织定义了新的日志文件。

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

My last problem is: How to reset the log manager without restart my server?我的最后一个问题是:如何在不重新启动服务器的情况下重置日志管理器?

The easiest way would be using application.properties.最简单的方法是使用 application.properties。 Most commonly customized are:最常见的定制是:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM