在 Java 中抑制运行时控制台警告的最佳方法是什么?

发布于 2024-07-16 09:09:30 字数 529 浏览 8 评论 0 原文

我正在使用 org.apache.commons.httpclient.methods.PostMethod 类的 getResponseBody() 方法。 但是,我总是收到在运行时写入控制台的消息:

警告:要缓冲较大或未知大小的响应正文。 建议使用 getResponseBodyAsStream。

在代码中,无论如何我都必须将响应写入字节数组,因此我应该使用 getResponseBody() 方法。 但是有没有一种简单的方法可以抑制警告消息,这样我就不必在每次运行时都查看它?

如果是编译器错误,我会使用 @SuppressWarnings 注释,但这不是编译时问题; 它发生在运行时。 另外,我可以使用 getResponseBodyAsStream 写入 ByteArrayOutputStream,但这似乎是一种绕过警告的狡猾方法(额外的代码行来完成 getResponseBody() 已经为我做的事情)。

我的猜测是答案涉及 System.out 或 System.err 操作,但是有没有好的方法可以做到这一点?

I am using the getResponseBody() method of the org.apache.commons.httpclient.methods.PostMethod class. However, I am always getting a message written to the console at runtime:

WARNING: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

In the code I have to write the response to a byte array anyway, so it is the getResponseBody() method that I ought to use. But is there an easy way that I can suppress the warning message so I don't have to look at it at every run?

If it was a compiler error, I'd use the @SuppressWarnings annotation, but this isn't a compile-time issue; it happens at runtime. Also, I could use getResponseBodyAsStream to write to a ByteArrayOutputStream, but this seems like a hacky way to get around the warning (extra lines of code to do what getResponseBody() is already doing for me).

My guess is that the answer involves System.out or System.err manipulation, but is there a good way to do this?

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

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

发布评论

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

评论(7

只有影子陪我不离不弃 2024-07-23 09:09:31

简单地“保存”stderr 消息,然后在完成主要任务后打印它们

PrintStream origErr = System.err;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newErr = new PrintStream(baos);
System.setErr(newErr);

====== do stuff ======

System.setErr(origErr);
System.err.print(baos);

to simply 'save' the stderr messages then print them after completion of the main task

PrintStream origErr = System.err;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newErr = new PrintStream(baos);
System.setErr(newErr);

====== do stuff ======

System.setErr(origErr);
System.err.print(baos);
作死小能手 2024-07-23 09:09:30

如果您想彻底停止此日志条目,可以使用一个可调的最大值来触发警告。 我看到了这个查看代码

        int limit = getParams().getIntParameter(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1024*1024);
        if ((contentLength == -1) || (contentLength > limit)) {
            LOG.warn("Going to buffer response body of large or unknown size. "
                    +"Using getResponseBodyAsStream instead is recommended.");
        }

HttpMethodBase.setParams() 看起来像是将 HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT 设置为所需值的地方。

If you want to cleanly stop this log entry, there's a tunable max that triggers the warning. I saw this looking at the code.

        int limit = getParams().getIntParameter(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1024*1024);
        if ((contentLength == -1) || (contentLength > limit)) {
            LOG.warn("Going to buffer response body of large or unknown size. "
                    +"Using getResponseBodyAsStream instead is recommended.");
        }

HttpMethodBase.setParams() looks like the place to set HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT to the desired value.

仅此而已 2024-07-23 09:09:30

当 httpClient 不知道返回数据的长度时会发生该警告
您应该在服务器端设置内容长度属性,该警告应该消失

response.addHeader("Content-Type", "text/html; charset=utf-8");
response.addHeader("Content-Length", String.valueOf(output.getBytes("utf8").length));

之后

that warning happends when httpClient have no idea about the length of the return data
you should set the content-length attribute in your server end

response.addHeader("Content-Type", "text/html; charset=utf-8");
response.addHeader("Content-Length", String.valueOf(output.getBytes("utf8").length));

after that, that warning should disapear

离鸿 2024-07-23 09:09:30

我建议您按照警告的建议进行操作,并使用流而不是字节数组。 如果您尝试推送的响应特别大(假设它是一个大文件),您会将其全部加载到内存中,这将是一件非常糟糕的事情。

使用流确实会更好。

也就是说,您可以通过临时替换 System.err 或 System.out 来解决它。 它们只是 PrintStream 对象,并且可以使用 setOut 和 setErr 方法进行设置。

PrintStream oldErr = System.err;
PrintStream newErr = new PrintStream(new ByteArrayOutputStream());
System.setErr(newErr);

// do your work

System.setErr(oldErr);

编辑:

我同意最好
使用流,但就像现在一样,
我需要放置的目标 API
响应是一个字节数组。 如果
必要时,我们可以进行重构
API,将允许它采取
溪流; 那会更好。 这
警告肯定是有的
原因。

如果您可以修改 API,请这样做。 在这种情况下,流处理是最好的方法。 如果由于内部压力或其他原因而不能,请转到 @John M 的路由并增加 BUFFER_WARN_TRIGGER_LIMIT - 但请确保您有一个已知的 contentLength,否则该路由甚至会失败。

I would recommend that you do as the warning suggests and use a stream rather than a byte array. If the response you're trying to push is particularly large (suppose it's a large file), you will load it all into memory, and that'd be a very bad thing.

You're really better off using streams.

That said, you might hack around it by replacing System.err or System.out temporarily. They're just PrintStream objects, and they're settable with the setOut and setErr methods.

PrintStream oldErr = System.err;
PrintStream newErr = new PrintStream(new ByteArrayOutputStream());
System.setErr(newErr);

// do your work

System.setErr(oldErr);

Edit:

I agree that it would be preferable to
use streams, but as it is now, the
target API where I need to put the
response is a byte array. If
necessary, we can do a refactor to the
API that will allow it to take a
stream; that would be better. The
warning is definitely there for a
reason.

If you can modify the API, do so. Stream processing is the best way to go in this case. If you can't due to internal pressures or whatever, go @John M's route and pump up the BUFFER_WARN_TRIGGER_LIMIT -- but make sure you have a known contentLength, or even that route will fail.

兲鉂ぱ嘚淚 2024-07-23 09:09:30

库是通过log4j输出的吗? 如果是这样,编辑 log4j.properties 将此类的输出设置为“ERROR”将有效,例如

log4j.logger.org.apache.commons.httpclient.methods.PostMethod=ERROR

Is the library outputting through log4j? if so, editing the log4j.properties to set the output for this class to "ERROR" would work, e.g.

log4j.logger.org.apache.commons.httpclient.methods.PostMethod=ERROR
牵你手 2024-07-23 09:09:30

此问题已在 ASF JIRA 上进行过讨论。 有两种方法可以解决此问题:

  • BUFFER_WARN_TRIGGER_LIMIT。 足够高的值更有可能抑制警告; 然而,这违背了警告本身的目的。 如果您要缓冲大量数据以最终在一次传递中解析,那么最好在处理数组之前将数据从流读取到数组中。
  • 如果您对 ERROR 或 FATAL 感到满意,请将 HttpClient 的日志记录级别设置为更高的值。

This issue has already been debated on the ASF JIRA. There are two ways to resolve this:

  • Set a higher value for BUFFER_WARN_TRIGGER_LIMIT. A high enough value is more likely to suppress the warning; however, that defeats the purpose of the warning itself. If you are buffering a lot of data to be eventually parsed in one pass, you are better off reading the data from a stream into an array before working on the array.
  • Set the logging level to a higher value for HttpClient, if you are comfortable with ERROR or FATAL.
笑着哭最痛 2024-07-23 09:09:30

假设警告已写入 stderr,您始终可以通过将 stderr 管道传输到 /dev/null 或系统上的任何等效项来抑制警告。

Assuming the warning is written to stderr, you can always suppress the warning by piping stderr to /dev/null, or whatever the equivalent on your system is.

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