Log4J 日志事件重新路由

发布于 2024-07-29 03:23:49 字数 441 浏览 2 评论 0原文

我想构建一个 Appender (或类似的东西)来检查事件并在某些条件下创建日志新事件。

一个例子是 Escalating Appender,它检查是否记录了一定数量的相同事件,如果是,则使用更高的日志级别记录该事件。 因此,您可以定义如下内容:如果您在此记录器上收到超过 10 个相同的警告,请将其设为错误。

所以我的问题是:

  1. 这样的东西已经存在吗?

  2. Appender 是实现此行为的正确类吗?

  3. 您认为我应该注意哪些陷阱?

澄清: 我对收集和分析事件的算法很满意。 我将使用附加程序内的集合来完成此操作。 为了我的目的,坚持并不是必要的。 我的问题#2 是:appender 是执行此操作的正确位置吗? 毕竟,为附加程序创建日志记录条目不是正常行为。

I would like to build an Appender (or something similar) that inspects Events and on certain conditions creates logs new Events.

An example would be and Escalating Appender that checks if a certain amount of identical Events get logged and if so logs the Event with a higher logleve. So you could define something like: If you get more then 10 identical Warnings on this logger, make it an Error.

So my questions are:

  1. Does something like this already exist?

  2. Is an Appender the right class to implement this behavior?

  3. Are there any traps you could think of I should look out for?

Clarification:
I am fine with the algorithm of gathering and analysing the events. I'll do that with a collection inside the appender. Persistence is not necessary for my purpose. My question #2 is: is an appender the right place for this to do? After all it is not normal behaviour to creat logging entries for an appender.

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

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

发布评论

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

评论(3

泛滥成性 2024-08-05 03:23:49

您可以通过实现log4j提供的Appender接口来创建自己的appender。

http://logging.apache.org/log4j/ 1.2/apidocs/org/apache/log4j/Appender.html

这将是一种方法。 另一种方法是使用现有的附加程序,然后编写一些监视日志的代码。 例如,您可以记录到数据库,然后编写一个进程来监视数据库中的日志条目并根据它所看到的内容创建元事件。

这主要取决于您对什么感到满意。 您必须处理的一个问题是如何回顾日志来创建元事件。 您要么必须在附加程序中累积事件,要么将它们保存在可以查询以构造元事件的地方。 积累它们的问题是,如果您停止并启动进程,您要么必须将它们转储到某个地方,以便在进程重新启动时重新拾取它们,要么重新开始。

例如,假设我想每 10 次抛出一个 NullPointerException 时创建一个日志条目。 如果我在某种数据库中有日志条目,那么每次抛出 NPE 时,我都会运行一个查询来查看自上次为它们创建日志条目以来已抛出了多少个 NPE。 如果我只是在每次抛出一个时在内存中对它们进行计数,如果我在抛出 5 个后重新启动应用程序,如果我不保留该数字,我将失去计数。

You can create your own appender by implementing the Appender interface provided by log4j.

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html

That would be one approach. Another would be to use an existing appender and then write some code that monitors the log. For example, you could log to the database and then write a process that monitors the log entries in the database and creates meta-events based on what it sees.

It depends most on what you're comfortable with. One question you'll have to deal with is how to look back in the log to create your meta-events. Either you'll have to accumulate events in your appender or persist them somewhere that you can query to construct your meta-events. The problem with accumulating them is that if you stop and start your process, you'll either have to dump them somewhere so they get picked back up or start over whenever the process restarts.

For example, let's say that I want to create a log entry every 10th time a NullPointerException is thrown. If I have the log entries in a database of some kind, every time an NPE is thrown I run a query to see how many NPEs have been thrown since the last time I created a log entry for them. If I just count them in memory every time one is thrown, if I restart the application after 5 are thrown, if I don't persist that number I'll lose count.

欲拥i 2024-08-05 03:23:49

Logback(log4j 的后继者)将允许您通过 TurboFilters 启用任何事件的日志记录。 例如,假设同一事件在给定时间范围内发生 N 次或多次,您可以强制接受该事件(无论其级别如何)。 另请参阅 DuplicateMessageFilter ,它执行相反的操作(拒绝重复发生的事件)。

但是,即使 logback 也不允许增加日志记录事件的级别。 Log4j 也不会。 这两个框架都不是为此设计的,我不鼓励您尝试在同一线程内动态增加级别。 另一方面,在后处理期间增加级别完全是另一回事。 另一种可能性是向另一个线程发出信号以生成更高级别的日志事件。 (让您的涡轮过滤器向另一个线程发出信号,以生成具有更高级别的日志记录事件。)

从您的问题中不清楚为什么您希望增加级别。 级别的增加本身是一个原因还是实现目标的一种手段,即无论其级别如何都记录事件。 如果是后者,那么 logback 的 TurboFilters 就是最佳选择。

哈特哈,

Logback (log4j's successor) will allow you to enable logging for any event via TurboFilters. For example, assuming the same event occurs N or more times in a given timeframe, you could force the event to be accepted (regardless of its level). See also DuplicateMessageFilter which does the inverse (denying re-occurring events).

However, even logback will not allow the level of the logging event to be incremented. Log4j will not either. Neither framework is designed for this and I would discourage you from attempting to increment the level on the fly and within the same thread. On the other hand, incrementing the level during post processing is a different matter altogether. Signaling another thread to generate a new logging event with a higher level is an additional possibility. (Have your turbo-filter signal another thread to generate a new logging event with a higher level.)

It was not clear from your question why you wished the level to be incremented. Was the increment of the level a reason in itself or was it a means to a goal, that is having the event logged regardless of its level. If the latter, then logback's TurboFilters are the way to go.

HTH,

九公里浅绿 2024-08-05 03:23:49

正如 Rafe 已经指出的那样,最大的挑战是将实际事件保存在 Appender 中,以便您知道触发事件的时间已经到来(例如升级日志级别)。

因此,我提出以下策略:

  1. 使用自定义 JDBCAppender。 与 Log4j 捆绑的那个不同,这个可以记录异常。
  2. 设置一个嵌入式数据库,例如 HSQLDB,并设置一个包含一张表的数据库用于事件记录。 它解决了持久性问题,因为您可以使用 SQL 查找发生的事件类型。
  3. 运行一个单独的线程来监视数据库并检测所需的事件模式。
  4. 使用 LogManager 访问所需的记录器并手动设置其级别。

As Rafe already pointed out, the greatest challenge would be persisting the actual events in the Appender, so that you'll know the time has come to trigger your event (e.g. escalate log level).

Therefore, I propose a following strategy:

  1. Use a custom JDBCAppender. Unlike the one bundled with Log4j, this one can log exceptions.
  2. Set-up an embedded database, like HSQLDB, and set-up a database with one table for event logging. It solves the persistence problem, as you can use SQL to find types of events that occurred.
  3. Run a separate thread that monitors the database, and detects desired event patterns.
  4. Use a LogManager to access desired Loggers and set their level manually.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文