java 无法为可执行 jar 配置日志记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14903978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't configure logging for executable jar
提问by Christian Vielma
This is similar to this other question, although I already put the logging.properties
in the executable jar and doesn't work.
这类似于另一个问题,尽管我已经将它logging.properties
放入了可执行 jar 并且不起作用。
I have a class (ReportGenerator) that has the following:
我有一个类(ReportGenerator),它具有以下内容:
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
I'm using Netbeans so I put the logging.properties
file in the path src/main/resources
. It has this (among other things):
我正在使用 Netbeans,所以我将logging.properties
文件放在 path 中src/main/resources
。它有这个(除其他外):
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = /my/folder/reports.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 10
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = OFF
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.mypackage.ReportGenerator.level = ALL
The jar is generated using Maven, when decompressed I can see that the logging.properties
is in the main folder of the jar. Along with the folder com
where my class is.
jar 是使用 Maven 生成的,解压后我可以看到它logging.properties
在 jar 的主文件夹中。连同com
我的班级所在的文件夹。
-com
-mypackage
-ReportGenerator
logging.properties
...other things
When I run from console:
当我从控制台运行时:
java - jar MyReportsJar.jar
It shows me the logs through the console. I want to log it to the file I set in the logging.properties.
它通过控制台向我显示日志。我想把它记录到我在logging.properties.
What am I doing wrong?
How do I do it without setting the JVM java.util.logging.config.file
param?
我究竟做错了什么?如何在不设置 JVMjava.util.logging.config.file
参数的情况下执行此操作?
回答by Christian Vielma
After dealing a few days against this and reading many resources on the Internet I came up with this solution:
在对此进行了几天的处理并阅读了互联网上的许多资源后,我想出了这个解决方案:
I have to change the logging properties of the LogManagerin the program. I can use the logging.properties
file packaged in the .jar
like this:
我必须在程序中更改LogManager的日志记录属性。我可以使用这样logging.properties
打包的文件.jar
:
LogManager.getLogManager().readConfiguration(ReportGenerator.class.getClassLoader().getResourceAsStream("logging.properties"));
And then I can do the same as before to get the logger and log:
然后我可以像以前一样获取记录器和日志:
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
But I found very useful that you can specify another logging.properties
file on runtime so my final code is this:
但是我发现您可以logging.properties
在运行时指定另一个文件非常有用,所以我的最终代码是这样的:
String logFile = System.getProperty("java.util.logging.config.file");
if(logFile == null){
LogManager.getLogManager().readConfiguration(ReportGenerator.class.getClassLoader().getResourceAsStream("logging.properties"));
}
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
That way, if I execute the jar as this:
这样,如果我这样执行 jar:
java -jar MyReportsJar.jar
It uses the internal logging.properties
file.
它使用内部logging.properties
文件。
But if I execute:
但是如果我执行:
java -Djava.util.logging.config.file=<external-logging.properties> -jar MyReportsJar.jar
it uses the external logging.properties
file.
它使用外部logging.properties
文件。