java.util.logging.Logger 在哪里存储日志

发布于 2024-09-05 20:59:59 字数 759 浏览 3 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(3

镜花水月 2024-09-12 20:59:59

在哪里可以看到日志?

在日志文件或标准输出中,具体取决于您的实际日志处理程序配置。这可以通过属性文件或直接通过日志记录 API 设置。

这是否意味着如果我有3个请求级别日志...

SEVERE 是最重要的(最高优先级),而 FINE 是 3 种消息类型中最不重要的消息类型你的例子。因此,如果您的日志级别为 SEVERE,则仅记录 SEVERE 消息。如果级别为FINE,则所有 3 条消息都会被记录。

当在实际生产环境中时,这非常有用,您可能只想记录错误和可能的警告(希望这些警告相当罕见,但您想了解它们),因此您可以将日志级别设置为 WARNING 。然而,在您的开发环境中,例如调试问题时,您希望查看日志中的所有信息,即使它会创建大量日志数据并减慢应用程序的速度。因此,您将日志级别设置为 FINEFINEST

这是一个很好的Java 日志记录简介

更新:上面页面中的一个简单示例,用于将记录器配置为记录到 FINEST 级别的文件:

Handler fh = new FileHandler("%t/wombat.log");
Logger.getLogger("").addHandler(fh);
Logger.getLogger("com.wombat").setLevel(Level.FINEST);

要记录到控制台,请替换 FileHandler 上面带有 ConsoleHandler

Handler ch = new ConsoleHandler();
Logger.getLogger("").addHandler(ch);

这只是一个示例 - 在真实的应用程序中,最好通过配置属性文件配置日志记录。

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.

Does this mean that if I have 3 request level log...

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.

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.

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:

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:

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.

五里雾 2024-09-12 20:59:59

Java TM 日志记录概述非常有趣回答您关于 Java Logger 的所有问题:

loggingoverview

您将看到您的日志与您的记录器关联的处理程序将生成所述日志(在控制台或流中)。 ..).
默认配置在根记录器上建立一个处理程序,用于将输出发送到控制台。

日志级别

每条日志消息都有一个关联的日志级别。该级别对日志消息的重要性和紧迫性提供了粗略的指导。日志级别对象封装一个整数值,值越大优先级越高。

Level类定义了七个标准日志级别,范围从FINEST(最低优先级,具有最低值)到SEVERE(< strong>最高优先级,具有最高值)。

The Java TM Logging Overview is quite interesting to answer all your questions on the Java Logger:

logging overview

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.

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 from FINEST (the lowest priority, with the lowest value) to SEVERE (the highest priority, with the highest value).

桜花祭 2024-09-12 20:59:59

它去哪里取决于您的配置。 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 is FINE, WARNING, SEVERE will show up, but if you have it set to SEVERE then only those will come up.

In general you should use FINE while debugging and switch to SEVERE when it is in a production environment.

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