简体   繁体   English

设置登录 java 的属性

[英]Setting properties for logging in java

Before you read this, you should know that I am a python developer in uncharted Java territory.在阅读本文之前,您应该知道我是 python 开发人员,在未知的 Java 领域。

I am trying to set up logging for Java.我正在尝试为 Java 设置日志记录。 I was able to get it to work when I set the logging.properties.当我设置 logging.properties 时,我能够让它工作。 Like this:像这样:

$ /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. logging.properties文件已检入修订控制,并且在我当前的设置中将用于所有环境,因此我不想在logging.properties文件中对 logging.FileHandler.pattern 进行硬编码。 So, I tried to move that to the command line, by doing this:因此,我尝试通过这样做将其移至命令行:

$ /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.但似乎 FileHandler.pattern 无法识别,因此该文件从未写入。 So, then I thought, well, let's just ditch the logging.properties file and try to throw everything on the command line.所以,然后我想,好吧,让我们放弃logging.properties文件并尝试将所有内容都扔到命令行中。 So, then I tried this:所以,然后我尝试了这个:

$ /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:三个问题:

  1. Why didn't the second example I provided work?为什么我提供的第二个示例不起作用?
  2. Why didn't the third example I provided work?为什么我提供的第三个示例不起作用?
  3. Since the logging.properties file is checked into version control and is a shared file, what is the right approach to this?由于 logging.properties 文件已签入版本控制并且是共享文件,因此正确的方法是什么? What is the best practice?最佳做法是什么?

When responding, please used small words for us Java newbies.回复时请用小字给我们Java新手。 :-) :-)

The -Dxyz.blahbla.bla=foobar is a System property, and not everything could be used as a System property, because there will be some code that will ask for that property, and even though you can put whatever you want, nobody will read it. -Dxyz.blahbla.bla=foobar是系统属性,并非所有内容都可以用作系统属性,因为会有一些代码会要求该属性,即使你可以放任何你想要的东西,也没有人会阅读。

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 UNLESS you read it your self and process it accordingly.因此,正如您现在可能猜到的那样,您不能只是 go 并将任何进入该文件的内容作为系统属性放入,除非您自己阅读并相应地处理它。

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.因此,如果您确实需要手动修改日志记录级别作为系统属性,您可能需要添加代码来读取该属性并设置值,但这可能太多了,我不确定您是否想要 go 那里。

So using the file should be enough所以使用文件就足够了

I hope this helps.我希望这有帮助。

"Hardcoding the FileHandler pattern" might not be such a bad thing, since the pattern can contain tokens that are replaced at runtime. “硬编码 FileHandler 模式”可能不是一件坏事,因为该模式可以包含在运行时替换的标记。 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.我知道 %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.如果您使用其中的一些,则该模式可能足够通用,您不必担心尝试对它进行超级概括以实现可移植性。

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

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