Java Util Logger Write Synchronization

醉酒当歌 提交于 2020-01-23 13:03:14

问题


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

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.


回答1:


You don't need any synchronization, quoting JavaDoc of 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.



来源:https://stackoverflow.com/questions/14211629/java-util-logger-write-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!