title: Best practices for Java logging from multiple threads?
body: <p>I want to have a diagnostic log that is produced by several tasks managing data. These tasks may be in multiple threads. Each task needs to write an element (possibly with subelements) to the log; get in and get out quickly.  If this were a single-task situation I'd use <a href="http://java.sun.com/webservices/docs/1.5/api/javax/xml/stream/XMLStreamWriter.html" rel="noreferrer">XMLStreamWriter</a> as it seems like the best match for simplicity/functionality without having to hold a ballooning XML document in memory. </p>
  
  <p>But it's not a single-task situation, and I'm not sure how to best make sure this is "threadsafe", where "threadsafe" in this application means that each log element should be written to the log correctly and serially (one after the other and not interleaved in any way).</p>
  
  <p>Any suggestions? I have a vague intuition that the way to go is to use a queue of log elements (with each one able to be produced quickly: my application is busy doing real work that's performance-sensitive), and have a separate thread which handles the log elements and sends them to a file so the logging doesn't interrupt the producers.</p>
  
  <p>The logging doesn't necessarily have to be XML, but I do want it to be structured and machine-readable.</p>
  
  <p>edit: I put "threadsafe" in quotes. Log4j seems to be the obvious choice (new to me but old to the community), why reinvent the wheel...</p>