问题
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