设置 java 中的日志记录属性

发布于 2024-11-18 03:34:59 字数 1284 浏览 2 评论 0原文

在阅读本文之前,您应该知道我是一个在未知的 Java 领域的 Python 开发人员。

我正在尝试为 Java 设置日志记录。当我设置logging.properties时,我能够让它工作。像这样:

$ /usr/bin/java -server -Xmx512m -Djava.util.logging.config.file= /path/to/logging/file/logging.properties -jar start.jar

logging.properties 文件被签入修订控制,并且在我当前的设置中将在所有环境中使用,所以我不想在中对logging.FileHandler.pattern进行硬编码logging.properties 文件。因此,我尝试将其移动到命令行,方法如下:

$ /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

但似乎 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

但这也不起作用。所以,我寻求帮助。

三个问题:

  1. 为什么我提供的第二个示例不起作用?
  2. 为什么我提供的第三个示例不起作用?
  3. 由于logging.properties文件已签入版本控制并且是共享文件,因此正确的方法是什么?最佳实践是什么?

回复时请对我们Java新手使用小词。 :-)

Before you read this, you should know that I am a python developer in uncharted Java territory.

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:

$ /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:

$ /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:

$ /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? What is the best practice?

When responding, please used small words for us Java newbies. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-11-25 03:34:59

-Dxyz.blahbla.bla=foobar 是一个系统属性,并不是所有东西都可以用作系统属性,因为一些代码会要求它财产,即使你可以写任何你想要的东西,没有人会读它。

例如,尝试以下代码:

class HelloSystemProperty {
     public static void main( String ... args ) {
         System.out.println( System.getProperty("getIt"));
      }
 }

然后像这样运行它:

java -DgetIt=Yeah HelloSystemProperty

因此,正如您现在可能猜到的那样,您不能只是将任何进入该文件的内容作为系统属性除非您阅读了它你自己并相应地处理它。

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

所以使用该文件应该足够了

我希望这会有所帮助。

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.

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.

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.

枯寂 2024-11-25 03:34:59

“对 FileHandler 模式进行硬编码”可能并不是一件坏事,因为该模式可以包含在运行时替换的标记。我知道 %t 被 VM 认为的系统临时目录(如 /var/tmp 或 C:\\TEMP)替换,%h 是用户的主目录(或者更确切地说是“user.home”系统属性),还有更多,但我不知道全部。

如果您使用其中的一些,该模式可能足够通用,您不必担心为了可移植性而尝试对其进行超级概括。

"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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文