java.util.logging.Logger 在哪里存储日志
我对 java Logger 有点迷茫
private static Logger logger = Logger.getLogger("order.web.OrderManager");
logger.info("Removed order " + id + ".");
我在哪里可以看到日志?还有来自 java.util.logging.Logger 库的引用:
在每次日志记录调用中,记录器最初都会根据记录器的有效日志级别对请求级别(例如 SEVERE 或 FINE)执行简单的检查。如果请求级别低于日志级别,则日志记录调用立即返回。
通过这个初始(廉价)测试后,Logger 将分配一个 LogRecord 来描述日志消息。然后,它将调用过滤器(如果存在)来对是否应发布记录进行更详细的检查。如果通过,它将把 LogRecord 发布到其输出处理程序。`
这是否意味着,如果我有 3 个请求级别
日志:
logger.log(Level.FINE, "Something");
logger.log(Level.WARNING, "Something");
logger.log(Level.SEVERE, "Something");
并且我的日志级别
为 SEVERE,我可以查看所有三个日志,如果我的日志级别
是警告,那么我看不到严重日志,这是正确的吗?如何设置日志级别
?
I am a bit lost with java Logger
private static Logger logger = Logger.getLogger("order.web.OrderManager");
logger.info("Removed order " + id + ".");
Where do I see the log? Also this quote from java.util.logging.Logger library:
On each logging call the Logger initially performs a cheap check of the request level (e.g. SEVERE or FINE) against the effective log level of the logger. If the request level is lower than the log level, the logging call returns immediately.
After passing this initial (cheap) test, the Logger will allocate a LogRecord to describe the logging message. It will then call a Filter (if present) to do a more detailed check on whether the record should be published. If that passes it will then publish the LogRecord to its output Handlers.`
Does this mean that if I have 3 request level
log:
logger.log(Level.FINE, "Something");
logger.log(Level.WARNING, "Something");
logger.log(Level.SEVERE, "Something");
And my log level
is SEVERE, I can see all three logs, and if my log level
is WARNING, then I can't see SEVERE log, is that correct? And how do I set the log level
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在日志文件或标准输出中,具体取决于您的实际日志处理程序配置。这可以通过属性文件或直接通过日志记录 API 设置。
SEVERE
是最重要的(最高优先级),而FINE
是 3 种消息类型中最不重要的消息类型你的例子。因此,如果您的日志级别为SEVERE
,则仅记录SEVERE
消息。如果级别为FINE
,则所有 3 条消息都会被记录。当在实际生产环境中时,这非常有用,您可能只想记录错误和可能的警告(希望这些警告相当罕见,但您想了解它们),因此您可以将日志级别设置为 WARNING 。然而,在您的开发环境中,例如调试问题时,您希望查看日志中的所有信息,即使它会创建大量日志数据并减慢应用程序的速度。因此,您将日志级别设置为
FINE
或FINEST
。这是一个很好的Java 日志记录简介。
更新:上面页面中的一个简单示例,用于将记录器配置为记录到
FINEST
级别的文件:要记录到控制台,请替换
FileHandler 上面带有
ConsoleHandler
:这只是一个示例 - 在真实的应用程序中,最好通过配置属性文件配置日志记录。
In a log file or standard output, depending on your actual log handler configuration. This can be set via a property file or directly via the logging API.
SEVERE
is the most important (highest priority) andFINE
is the least important message type of the 3 shown in your example. So if your log level isSEVERE
, only theSEVERE
messages get logged. If level isFINE
, all 3 messages get logged.This is very useful when in real production environment, you may want to log only the errors and possibly warnings (which are - hopefully - fairly rare, but you want to know about them), so you can set log level to
WARNING
. However, in your development environment, when e.g. debugging a problem, you want to see all information in the logs, even though it creates a large amount of log data and slows down the application. So you set log level toFINE
orFINEST
.Here is a good introduction to Java Logging.
Update: a simple example from the above page to configure the logger to log to a file at level
FINEST
:To log to the console, replace the
FileHandler
above with aConsoleHandler
:This is just an example though - in a real app, it is preferable to configure the logging via a config property file.
Java TM 日志记录概述非常有趣回答您关于 Java
Logger
的所有问题:您将看到您的日志与您的记录器关联的处理程序将生成所述日志(在控制台或流中)。 ..).
默认配置在根记录器上建立一个处理程序,用于将输出发送到控制台。
The Java TM Logging Overview is quite interesting to answer all your questions on the Java
Logger
:You will see your log where the
Handler
(s) associated with yourLogger
will generate said Log (in aConsole
, or in aStream
...).The default configuration establishes a single handler on the root logger for sending output to the console.
它去哪里取决于您的配置。 API 文档中有关于此的详细信息。日志级别与你所说的完全相反。如果您的配置为
FINE
,则FINE、WARNING、SEVERE
的所有内容都会显示,但如果您将其设置为SEVERE
,则只有那些会出现。一般来说,您应该在调试时使用
FINE
,并在生产环境中切换到SEVERE
。Where it goes is dependent on your configuration. There are details about this in the API docs. The log level is the exact reverse of what you said. If you have a configuration of
FINE
, everything that isFINE, WARNING, SEVERE
will show up, but if you have it set toSEVERE
then only those will come up.In general you should use
FINE
while debugging and switch toSEVERE
when it is in a production environment.