Java Util Logger写同步 [英] Java Util Logger Write Synchronization

查看:157
本文介绍了Java Util Logger写同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在应用程序中(例如,以Web应用程序为例),我们在启动期间创建了一个记录器实例。它甚至可以是单身,也无所谓。重要的是整个应用程序有1个实例。我们使用java.util.logger

Normally in applications (Take web application for example) we have a single instance of the logger created during the startup. It can even be a singleton and it doesn't matter. The important thing is there is 1 instance for the whole application. We use java.util.logger

现在想象来自两个不同用户的两个请求会抛出异常,我们正在记录那些写入日志文件的请求。这些对日志文件的两个不同请求中的写入是否同步?或者我们是否需要明确地同步它们因为我发现在极少数情况下我们得到的日志在tomcat日志文件中的两个请求之间都是混合的?

Now image you have two requests from two different users which throw an exception and we are logging those which get written to the log file. Is the write in these two different requests to the log file synchronized in someway? Or do we need to explicitly synchronize them cause I found in rare cases we got logs which are all mixed up between two requests in the tomcat log file?

我不完全是这里只关注两个日志消息的因果关系。

I am not exactly concerned about causality here just separation of two log messages.

推荐答案

你不需要任何同步,引用 JavaDoc Logger

You don't need any synchronization, quoting JavaDoc of Logger:


Logger上的所有方法都是多线程安全的。

All methods on Logger are multi-thread safe.

请注意,来自不同线程的单独调用仍然可以交错。它只是意味着你不会有一条消息被另一条消息打断和切片。

Note that separate calls from different threads can still be interleaved. It just means that you won't have one message being interrupted and sliced by the other.

这篇关于Java Util Logger写同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆