Spring JTA 事务的 Log4J 配置

发布于 2024-08-24 16:23:38 字数 650 浏览 6 评论 0原文

我们使用此 Log4J 配置来显示 JTA 信息:

<category name="org.springframework.transaction">
    <priority value="DEBUG"/>
</category>

生成的日志条目的类型为:

15:36:08,048 DEBUG [JtaTransactionManager] [ ] Creating new transaction with name [class]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
15:36:09,564 DEBUG [JtaTransactionManager] [ ] Initiating transaction commit

... 现在我们使用 Spring 的 MessageListener 来监听 MQ 队列。问题是这是事务性的,我们每 2 秒打印一次上述日志记录。

我们想要的只是当有人使用我们的 REST API 访问利用 @Transactional 的服务时打印出这些 JTA 日志语句。我们不需要来自此“轮询”MQ 侦听实现的 JTA 日志条目。

你能做到吗?

We use this Log4J config to display JTA info:

<category name="org.springframework.transaction">
    <priority value="DEBUG"/>
</category>

The resulting log entries are of the type:

15:36:08,048 DEBUG [JtaTransactionManager] [ ] Creating new transaction with name [class]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
15:36:09,564 DEBUG [JtaTransactionManager] [ ] Initiating transaction commit

... Now we use Spring's MessageListener to listen to an MQ queue. Problem is this is transactional, and we get the aforementioned logging printed out every 2 seconds.

What we want, is just to have these JTA log statements printed out when someone uses our REST API to access services that utilize @Transactional. We don't want the JTA log entries that come from this "polling" MQ listening implementation.

Can you do this?

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

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

发布评论

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

评论(2

能否归途做我良人 2024-08-31 16:23:38

您应该在配置中将默认日志级别设置为高于 DEBUG,然后尝试在适当的调用内的 REST API 中手动调整 org.springframework.transaction 的日志级别。例如,

public void doSomething() {
    Logger txLogger = Logger.getLogger("org.springframework.transaction");
    Level defaultLevel = txLogger.getLevel();
    txLogger.setLevel(Level.DEBUG);
    // do my stuff
    txLogger.setLevel(defaultLevel);
}

这意味着在调用您的 API - 但只有那时 - MQ 侦听器发起的调用也会被记录,但据我所知,无法根据调用位置为同一类配置不同的日志级别:-(

更新另一种可能性是创建一个自定义 TransactionManager,它只是 Spring 提供的一个包装器,它将调用转发给 Spring 并编写自己的日志。 MQ 侦听器中的 Spring 版本然后您将有两个不同的类,因此您可以设置不同的日志级别,但配置和维护可能比它值得的更麻烦。

You should set the default log level higher than DEBUG in your configuration, then try adjusting the log level for org.springframework.transaction manually in your REST API inside the appropriate call., e.g.

public void doSomething() {
    Logger txLogger = Logger.getLogger("org.springframework.transaction");
    Level defaultLevel = txLogger.getLevel();
    txLogger.setLevel(Level.DEBUG);
    // do my stuff
    txLogger.setLevel(defaultLevel);
}

This means that during the call to your API - but only then - the calls initiated by the MQ listener would also be logged, but AFAIK there is no way to configure different log levels for the same class depending on where it is called from :-(

Update: Another possibility would be to create a custom TransactionManager, which would be just a wrapper for the one provided by Spring. It would forward the calls to Spring and write its own logs. You would use that in your API but the Spring version in the MQ listener. Then you would have two distinct classes, so you could set different log levels. I am fairly sure this is possible, however it may be more hassle to configure and maintain than it is worth...

意犹 2024-08-31 16:23:38

我猜您正在使用 DefaultMessageListenerContainer,对吗?这会主动轮询 JMS 队列,创建持续的事务流。

您可以尝试一件事,我犹豫是否建议它,但您可以考虑使用 SimpleMessageListenerContainer 代替。这不会轮询 JMS,而是依赖 JMS 向其传递消息。这更简单(因此得名),但在某些 JMS 设置中不太可靠。由于它更加被动,因此会产生更少的事务负载,从而减少日志噪音。

我犹豫是否建议这样做,因为改变你的设计来减少对数噪声可能不是一个好主意。

I'm guessing you're using DefaultMessageListenerContainer, is that right? This actively polls the JMS queue, creating a constant stream of transactions.

There is one thing you could try, and I hesitate to suggest it, but you could consider using SimpleMessageListenerContainer instead. This doesn't poll JMS, it relies on JMS to deliver messages to it instead. This is simpler (hence the name), but less reliable in some JMS setups. Since it's more passive, it'll generate less transaction load, and hence less log noise.

I hesitate to suggest it because changing your design to reduce log noise is probably not a good idea.

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