当自定义日志文件中记录错误时,System.out 中发出警告

发布于 2024-09-18 04:59:19 字数 404 浏览 3 评论 0原文

目前,我们在 WebSphere Portal Server 上运行相当多的应用程序,其中大多数都有自己的日志文件,因此 System.out 不会太过载。然而,一旦应用程序中发生错误,就很容易被忽视。如果记录了错误,我希望在 System.out 中收到警告,并通知您查看相关的日志文件。现在,我通过将 log4j 记录器包装到一个具有与记录器类相同方法的类中来做到这一点,并且它只是将所有调用委托给记录器。仅当调用错误消息和致命消息方法时,才会从记录器中检索要记录的文件,并将警告写入 System.out。这工作正常,除了现在 LoggerWrapper 是记录器方法的调用者,因此作为调用者而不是原始调用者写入日志文件。

有没有办法解决这个问题,甚至可能采用完全不同的方法?我想避免重写所有异常处理程序以将消息添加到 System.out 本身。

基督教

currently we're running quite a few applications on our WebSphere Portal Server, and most of the have their own log-file, so the System.out won't be too overloaded. However, once an error happens in an application it's easy to miss. If an errors is logged, I would like to get a warning in the System.out with a notice to take a look at the related log-file. For now I did this by wrapping the log4j logger into a class with equal methods to the logger-class, and it just delegates all calls to the logger. only when the error- and fatal-messages methods are called, the file to log at is retrieved from the logger, and a warning is written to the System.out. This is working fine, except that now the LoggerWrapper is the caller to the logger-methods, und thus is written to the log files as the caller, not the original caller.

Is there any way around this, maybe even with a completely different approach? I would like to avoid having to rewrite all exception-handlers to add the message to the System.out themselves.

Christian

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

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

发布评论

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

评论(3

迷雾森÷林ヴ 2024-09-25 04:59:19

您可以简单地配置 Log4J 附加程序来写入 stdout(或者在本例中为 stderr)。这是标准方式。通过将日志阈值设置为 WARNING,您可以过滤掉大量日志消息。

代价是错误消息将同时出现在 stderr 和日志文件中,但对我来说这不是问题。事实上,它可能有助于查找与给定 stderr 消息相对应的特定日志。

You could simply configure a Log4J appender to write to stdout (or rather stderr in this case). That is the standard way. By setting the log threshold to WARNING, you can filter out the bulk of the log messages.

The price is that the error messages will appear both on stderr and in the log file(s), however to me that would not be an issue. In fact, it may help finding the specific logs corresponding to a given stderr message.

素年丶 2024-09-25 04:59:19

我在我们的应用程序中做了非常类似的事情,在其中管理所有日志记录。

我通过将“ErrorConsoleAppender”附加到我的根记录器解决了这个问题。我的配置文件中的所有记录器都是附加的(当然,这意味着它们适当地“冒泡”)。

正如人们所怀疑的那样,“ErrorConsoleAppender”将错误推送到控制台,并且仅将错误推送到控制台。它对我们来说非常有效,可以保持我们的控制台清洁,除非出现严重问题。我的代码如下:

...
    <appender name="ErrorConsoleAppender" class="org.apache.log4j.ConsoleAppender">
            <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/> 
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="[%d{ABSOLUTE}] %-5p > %m%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                  <param name="LevelMin" value="ERROR" />
                  <param name="LevelMax" value="ERROR" />
                  <param name="AcceptOnMatch" value="true" />
            </filter>
            <filter class="org.apache.log4j.varia.DenyAllFilter" />
    </appender>
...
    <root>
            <priority value="ALL" />
            <appender-ref ref="ErrorConsoleAppender" />
    </root>

当然,要按照您所描述的方式进行这项工作,您必须添加不转储堆栈跟踪的简单 log.error(String) 调用,如 Peter 上面所建议的。


If editing the log.error(String) calls is not practical, then I would suggest using AspectJ to crosscut your code and provide the functionality you need. Even if you're not familiar with aspect-oriented programming, it's fairly simple to achieve what you've described with it. In fact, one of the primary use cases for AOP is precisely the scenario you've described. Aspects are great for logging.

从表面上看,在您的情况下,最合适的方法似乎是使用“抛出后”语法在发生特定异常时采取适当的记录操作。

我希望所有这些都能以某种方式有所帮助,如果您对 AOP 如何解决您的问题还有其他疑问,请告诉我,

-gMale

I do a very similar thing in our application, in which I administer all logging.

I've solved the problem by attaching an "ErrorConsoleAppender" to my root logger. All loggers in my configuration file are additive (of course, meaning they "bubble up" appropriately).

As one would suspect, the "ErrorConsoleAppender" pushes errors to the console and only errors. It works amazingly well for us, keeping our console clean unless there's a serious problem. My code for that is as below:

...
    <appender name="ErrorConsoleAppender" class="org.apache.log4j.ConsoleAppender">
            <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/> 
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="[%d{ABSOLUTE}] %-5p > %m%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                  <param name="LevelMin" value="ERROR" />
                  <param name="LevelMax" value="ERROR" />
                  <param name="AcceptOnMatch" value="true" />
            </filter>
            <filter class="org.apache.log4j.varia.DenyAllFilter" />
    </appender>
...
    <root>
            <priority value="ALL" />
            <appender-ref ref="ErrorConsoleAppender" />
    </root>

Of course, to make this work in the manner you described, you would have to have add simple log.error(String) calls that do not dump stack traces, as suggested above by Peter.


If editing the log.error(String) calls is not practical, then I would suggest using AspectJ to crosscut your code and provide the functionality you need. Even if you're not familiar with aspect-oriented programming, it's fairly simple to achieve what you've described with it. In fact, one of the primary use cases for AOP is precisely the scenario you've described. Aspects are great for logging.

On the surface, it seems most fitting approach, in your case, would be to use the "after throwing" syntax to take appropriate, logging action when specific exceptions occur.

I hope all that helps in some way, let me know if you have further questions about how AOP can address your problem,

-gMale

调妓 2024-09-25 04:59:19

感谢您的想法。在内部讨论之后,我最终编写了一个简单的附加程序,它附加到所有记录器,并且当记录错误或致命消息时,它将向 System.out 输出一个简短的通知。由于附加程序可以访问调用记录器,因此它可以分析所有其他附加程序并找出它们登录到的位置,并将其添加到通知中。

基督教

Thanks for the ideas. After discussing this internally I ended up writing a simple appender, which is attached to all loggers, and which will output a short notice to the System.out when an error or fatal-message is logged. Since the appender has access to the calling logger, it can analyze all other appenders and find out where they log to, and add this to the notice.

Christian

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