Best practices for Java logging from multiple threads?

前端 未结 12 1102
星月不相逢
星月不相逢 2020-12-03 02:39

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 s

相关标签:
12条回答
  • 2020-12-03 03:28

    Use a logging framework, such as Log4.

    and if you are not happy with the output you can write your own Appender, Filter, whatever to tweak it just write. So you could do even some caching to rearrange the entries, although I am not saying this is a good idea.

    0 讨论(0)
  • 2020-12-03 03:34

    Use logback-classic. It is a newer and better implementation of log4j.

    0 讨论(0)
  • 2020-12-03 03:35

    log4j is and has been the standard for java logging for many years. But if you don't fancy an external dependency then the java.util.logging package provides an acceptable solution.

    0 讨论(0)
  • 2020-12-03 03:37

    Use a logging framework that implements some form of the NDC pattern, like Log4J.

    0 讨论(0)
  • 2020-12-03 03:39

    I had a similar problem and implementation demands for special logs only. My solution was:

    1. I took a blockinglinkedqueue with size of *2 of the app's traffic/min.

    2. All threads put the object in the queue and finishes the job.

    3. Separate Log-Writer thread taking head object from queue and writing it to log4j file using a separate appender. This appender was not used for systemlogs.

    This ensures that logs are written serially and always are in order.

    This will not affect performance of the application since log writing is a completely separate process and will not create a bottleneck.

    You can also use aysncappender of log4j.

    0 讨论(0)
  • 2020-12-03 03:44

    You could use synchronization mechanisms (like a monitor or a semaphor) to make sure, that one log request is processed before accepting the next. This could all be hidden from the code calling the logging routines.

    0 讨论(0)
提交回复
热议问题