如何在java日志记录属性文件中配置我自己的格式化程序
对于我的 java 项目,我使用 java 日志记录 api。我想使用属性文件记录所有内容。
在使用这个文件(log.properties)之前,我在java代码中配置了我的onwn格式化程序。 (见下文)
现在我想在propertie文件中配置我自己的fomatter,而不是java代码。 有人知道该怎么做吗?
Formatter formatter = new Formatter() {
@Override
public String format(LogRecord arg0) {
StringBuilder b = new StringBuilder();
b.append(new Date());
b.append(" ");
b.append(arg0.getSourceClassName());
b.append(" ");
b.append(arg0.getSourceMethodName());
b.append(" ");
b.append(arg0.getLevel());
b.append(" ");
b.append(arg0.getMessage());
b.append(System.getProperty("line.separator"));
return b.toString();
}
};
java代码中的fomatter
............
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.FileHandler.level=警告
**java.util.logging.??? = 我如何使用以下信息在属性文件中配置我自己的格式化程序:data、clasename、methodename、level .etc.****
de log.proprties 中的格式化程序
For my java project, i am using the java logging api. I want to log everything using a property file.
Before using this file (log.properties), I configured my onwn formatter in the java code. (see below)
Now I want to configure my own fomatter in the propertie file, instead of the java code.
does someone know how to do that ?
Formatter formatter = new Formatter() {
@Override
public String format(LogRecord arg0) {
StringBuilder b = new StringBuilder();
b.append(new Date());
b.append(" ");
b.append(arg0.getSourceClassName());
b.append(" ");
b.append(arg0.getSourceMethodName());
b.append(" ");
b.append(arg0.getLevel());
b.append(" ");
b.append(arg0.getMessage());
b.append(System.getProperty("line.separator"));
return b.toString();
}
};
fomatter in the java code
..... .....
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=WARNING**java.util.logging.??? = how can i configure my own formater in the property file with this information: data, clasename, methodename, level .etc.****
formatter in de log.proprties
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写您自己的接受格式字符串的格式化程序。
在logging.properties 文件中配置格式化程序。例如:
如果编写这样的格式化程序看起来令人畏惧,那么可以从以下内容开始:
Write your own Formatter that accepts a format String.
Configure your formatter in the logging.properties file. For example:
If writing such a Formatter seems daunting, Here is something to start with:
将所需的属性放入属性文件中。
在您的 Formatter 调用中
Put the properties you want in the properties file.
In your Formatter call