multithreading - Best practice for consecutive logger calls in multithreaded environment -
consider code:
if (param1 == null || param2 == null) { logger.error("failed stuff."); logger.debug("param1: " + param1); logger.debug("param2: " + param2); } it readable, in multithreaded environment logically atomic log message split 3 parts.
now, part of solution simple, , readability not suffer much:
if (param1 == null || param2 == null) { logger.error("failed stuff."); logger.debug( "param1: " + param1 + system.getproperty("line.separator") + "param2: " + param2 ); } if changing logger output ok, can write:
if (param1 == null || param2 == null) { string message = "failed stuff."; if (logger.isdebugenabled()) { message += system.getproperty("line.separator") + "param1: " + param1 + system.getproperty("line.separator") + "param2: " + param2; } logger.error(message); } clean log, uglier code...
alternatively, write:
if (param1 == null || param2 == null) { synchronized (logger) { logger.error("failed stuff."); logger.debug("param1: " + param1); logger.debug("param2: " + param2); } } what recommend , why?
configure logging subsystem identify logging thread in output:
timestamp process.thread_id severity lorem ipsum dolor sit amet ^^^^^^^^^ now easy thread log together.
Comments
Post a Comment