设置用于登录 java 的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6516294/
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
Setting properties for logging in java
提问by Hymaniekazil
Before you read this, you should know that I am a python developer in uncharted Java territory.
在您阅读本文之前,您应该知道我是一名处于未知 Java 领域的 Python 开发人员。
I am trying to set up logging for Java. I was able to get it to work when I set the logging.properties. Like this:
我正在尝试为 Java 设置日志记录。当我设置 logging.properties 时,我能够让它工作。像这样:
$ /usr/bin/java -server -Xmx512m -Djava.util.logging.config.file= /path/to/logging/file/logging.properties -jar start.jar
The logging.properties
file is checked into revision control and in my current set up is to be used in all environments, so I don't want to hard code the logging.FileHandler.pattern in the logging.properties
file. So, I tried to move that to the command line, by doing this:
该logging.properties
文件已签入版本控制,并且在我当前的设置中将在所有环境中使用,因此我不想在logging.properties
文件中对 logging.FileHandler.pattern 进行硬编码。因此,我尝试通过执行以下操作将其移至命令行:
$ /usr/bin/java -server -Xmx512m -Djava.util.logging.config.file= /path/to/logging/file/logging.properties -Djava.util.logging.FileHandler.pattern=/var/log/project_name/solr/solr.log -jar start.jar
But it seems that the FileHandler.pattern wasn't recognized, so the the file never wrote. So, then I thought, well, let's just ditch the logging.properties
file and try to throw everything on the command line. So, then I tried this:
但似乎 FileHandler.pattern 未被识别,因此该文件从未写入。所以,然后我想,好吧,让我们放弃logging.properties
文件并尝试将所有内容都放在命令行上。所以,然后我尝试了这个:
$ /usr/bin/java -server -Xmx512m -Djava.util.logging.Handler=java.util.logging.FileHandler -D.level=WARNING -Djava.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter -Djava.util.logging.FileHandler.pattern=/var/log/project_name/solr/solr.log -jar start.jar
But that didn't work either. So, I am asking for help.
但这也不起作用。所以,我在寻求帮助。
Three questions:
三个问题:
- Why didn't the second example I provided work?
- Why didn't the third example I provided work?
- Since the logging.properties file is checked into version control and is a shared file, what is the right approach to this? What is the best practice?
- 为什么我提供的第二个例子不起作用?
- 为什么我提供的第三个例子不起作用?
- 由于 logging.properties 文件已签入版本控制并且是共享文件,因此正确的方法是什么?最佳做法是什么?
When responding, please used small words for us Java newbies. :-)
回答的时候,请各位Java新手用小字。:-)
回答by OscarRyz
The -Dxyz.blahbla.bla=foobar
is a System property, and not everything could be used as a System property, because there will be somecode that will ask for that property, and even though you can put whatever you want, nobody will read it.
这-Dxyz.blahbla.bla=foobar
是一个 System 属性,并不是所有的东西都可以用作 System 属性,因为会有一些代码会要求该属性,即使您可以放置任何想要的内容,也没有人会阅读它。
For instance, try this code:
例如,试试这个代码:
class HelloSystemProperty {
public static void main( String ... args ) {
System.out.println( System.getProperty("getIt"));
}
}
And then run it like:
然后像这样运行它:
java -DgetIt=Yeah HelloSystemProperty
So, as you may guess by now you can't just go and put anything that goes into that file as a system property UNLESSyou read it your self and process it accordingly.
因此,您现在可能已经猜到了,除非您自己阅读并相应地处理它,否则您不能将进入该文件的任何内容作为系统属性放入其中。
So if you definitely NEED to modify the logging level by hand as a system property you may want to add code to read that property and set the values, but probably that's too much and I'm not sure if you want to go there.
因此,如果您确实需要手动修改日志记录级别作为系统属性,您可能需要添加代码来读取该属性并设置值,但这可能太多了,我不确定您是否想去那里。
So using the file should be enough
所以使用文件应该就足够了
I hope this helps.
我希望这有帮助。
回答by Ti Strga
"Hardcoding the FileHandler pattern" might not be such a bad thing, since the pattern can contain tokens that are replaced at runtime. I know %t is replaced by whatever the VM believes is the system temp directory (like /var/tmp or C:\\TEMP), %h is the user's home directory (or rather the "user.home" system property), and there are some more but I don't know all of them.
“对 FileHandler 模式进行硬编码”可能并不是一件坏事,因为该模式可以包含在运行时被替换的标记。我知道 %t 被虚拟机认为是系统临时目录的任何东西所取代(如 /var/tmp 或 C:\\TEMP),%h 是用户的主目录(或者更确切地说是“user.home”系统属性),还有一些,但我不知道所有这些。
If you use some of those, the pattern might be sufficiently generic that you don't have to worry about trying to super-generalize it for portability.
如果您使用其中的一些,该模式可能足够通用,您不必担心为了可移植性而尝试对其进行超级泛化。