有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java util日志属性中何时需要“.level”?

我见过java util日志以多种方式配置。通常,记录器名称使用完全限定的类名。在相应的日志中。属性文件中,我看到了在包级别或类级别以几种不同的方式配置日志级别。例如,要将com.example.MyClass的loglevel设置为FINE:

  • com。实例级别=良好
  • com。示例。*=好的
  • com。实例MyClass=很好
  • com。实例我的班级。级别=良好

这四种变体都有效吗(假设它们在文件后面没有被覆盖)? 其中有哪个选项比其他选项“更正确”吗? 我喜欢java。util。如果不存在.level,就假设它是日志记录

我试图找到这个配置文件的最终指南,但在 https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html#a1.8这似乎还不够具体


共 (1) 个答案

  1. # 1 楼答案

    Do all four of these variants work (assuming they are not overridden later in the file)?

    Most of the online tutorials I've seen use the explicit .level suffix...is that preferred (and why)?

    在标准LogManager下,com.example.* = FINEcom.example.MyClass = FINE不会更改级别。键必须以.level结尾才能更改级别

    根据LogManager文档:

    All properties whose names end with ".level" are assumed to define log levels for Loggers. Thus "foo.level" defines a log level for the logger called "foo" and (recursively) for any of its children in the naming hierarchy. Log Levels are applied in the order they are defined in the properties file. Thus level settings for child nodes in the tree should come after settings for their parents. The property name ".level" can be used to set the level for the root of the tree.

    如果您使用的是LogManager的子类,那么您需要查阅该文档来验证语法

    Does java.util.logging just assume .level if its not there?

    根据文件,它没有。如果你申报的东西没有。它只会被认为是LogManager entry

    Are any of the options "more correct" than the others?

    LogManager属性文件can't create loggers。这意味着您的日志文件必须与代码创建记录器的方式相匹配。例如,如果您的文件使用com.example.level = FINE,而您的代码使用com.example.MyClass1com.example.MyClass2作为记录器名称,您将永远不会看到MyClass1或MyClass2切换为FINE,因为the code never created package parent logger。Root是所有命名记录器的父级,因此这是一次更改多个记录器的理想方式。如果需要做任何非常复杂的事情,那么可以使用LogManager支持的config选项