java.util.logging.Logger 在哪里存储他们的日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3025691/
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
Where does java.util.logging.Logger store their log
提问by Thang Pham
This might be a stupid question but I am a bit lost with java Logger
这可能是一个愚蠢的问题,但我对 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:
我在哪里可以看到日志?还有来自 java.util.logging.Logger 库的引用:
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.`
在每次记录调用时,Logger 最初都会根据记录器的有效日志级别对请求级别(例如 SEVERE 或 FINE)进行廉价检查。如果请求级别低于日志级别,则日志记录调用会立即返回。
通过这个初始(廉价)测试后,Logger 将分配一个 LogRecord 来描述日志消息。然后它会调用一个过滤器(如果存在)来更详细地检查是否应该发布记录。如果通过,它会将 LogRecord 发布到其输出处理程序。`
Does this mean that if I have 3 request level
log:
这是否意味着如果我有 3 个request level
日志:
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
?
我的log level
是 SEVERE,我可以看到所有三个日志,如果我的log level
是 WARNING,那么我就看不到 SEVERE 日志,对吗?我该如何设置log level
?
采纳答案by Péter T?r?k
Where do I see the log?
我在哪里可以看到日志?
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.
在日志文件或标准输出中,取决于您的实际日志处理程序配置。这可以通过属性文件或直接通过日志 API 设置。
Does this mean that if I have 3 request level log...
这是否意味着如果我有 3 个请求级别的日志...
SEVERE
is the most important (highest priority) and FINE
is the least important message type of the 3 shown in your example. So if your log level is SEVERE
, only the SEVERE
messages get logged. If level is FINE
, all 3 messages get logged.
SEVERE
是最重要的(最高优先级),并且FINE
是示例中显示的 3 种消息类型中最不重要的消息类型。因此,如果您的日志级别为SEVERE
,则只会SEVERE
记录消息。如果级别为FINE
,则记录所有 3 条消息。
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 to FINE
or FINEST
.
这在实际生产环境中非常有用,您可能只想记录错误和可能的警告(希望这些信息很少见,但您想了解它们),因此您可以将日志级别设置为WARNING
. 但是,在您的开发环境中,例如在调试问题时,您希望查看日志中的所有信息,即使它会创建大量日志数据并减慢应用程序的速度。因此,您将日志级别设置为FINE
或FINEST
。
Here is a good introduction to Java Logging.
这是Java Logging 的一个很好的介绍。
Update:a simple example from the above page to configure the logger to log to a file at level FINEST
:
更新:上面页面中的一个简单示例,用于将记录器配置为在级别记录到文件FINEST
:
Handler fh = new FileHandler("%t/wombat.log");
Logger.getLogger("").addHandler(fh);
Logger.getLogger("com.wombat").setLevel(Level.FINEST);
To log to the console, replace the FileHandler
above with a ConsoleHandler
:
要登录到控制台,请将FileHandler
上面的内容替换为ConsoleHandler
:
Handler ch = new ConsoleHandler();
Logger.getLogger("").addHandler(ch);
This is just an example though - in a real app, it is preferable to configure the logging via a config property file.
这只是一个例子 - 在真实的应用程序中,最好通过配置属性文件配置日志记录。
回答by VonC
The Java TM Logging Overviewis quite interesting to answer all your questions on the Java Logger
:
在的Java TM记录概述是相当有趣的回答在Java你所有的问题Logger
:
You will see your log where the Handler
(s) associated with your Logger
will generate said Log (in a Console
, or in a Stream
...).
The default configuration establishes a single handler on the root logger for sending output to the console.
您将看到您的日志,其中Handler
与您相关联的(s)Logger
将生成所述日志(在 aConsole
或 a Stream
... 中)。
默认配置在根记录器上建立一个处理程序,用于将输出发送到控制台。
Log Level:
Each log message has an associated log Level. The Level gives a rough guide to the importance and urgency of a log message. Log level objects encapsulate an integer value, with higher values indicating higher priorities.
The
Level
class defines seven standard log levels, ranging fromFINEST
(the lowest priority, with the lowest value) toSEVERE
(the highest priority, with the highest value).
日志级别:
每条日志消息都有一个关联的日志级别。级别为日志消息的重要性和紧迫性提供了粗略的指南。日志级别对象封装一个整数值,值越大表示优先级越高。
的
Level
类定义了七个标准日志级别,从FINEST
(最低优先级,具有最低值)至SEVERE
(最高优先级,具有最高值)。
回答by Andrew Hubbs
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 is FINE, WARNING, SEVERE
will show up, but if you have it set to SEVERE
then only those will come up.
它的去向取决于您的配置。API 文档中有关于此的详细信息。日志级别与您所说的完全相反。如果您的配置为FINE
,FINE, WARNING, SEVERE
则将显示所有内容,但如果您将其设置为 ,SEVERE
则只会出现那些。
In general you should use FINE
while debugging and switch to SEVERE
when it is in a production environment.
一般来说,你应该FINE
在调试SEVERE
时使用,并在生产环境中切换到它。