我们有一个多线程服务,当另一个线程同时写入日志时,某些单独的日志消息会被破坏。我怎样才能防止这种情况发生?
我们正在使用工作线程来进行REST服务调用,接收和处理响应。在执行此操作时,我们使用logback向dropwizard中的默认日志记录引擎写入日志。
我正在看的具体情况是当线程抛出异常时,我们记录堆栈跟踪。我看到的是,跟踪被至少一个其他线程的条目减少了一半:
实施例
[pool-4-thread-13] Error!
stack trace line 1
[pool-4-thread-17] Event 1 happened
[pool-4-thread-17] Event 2 happened
stack trace line 2
stack trace line 3
stack trace line 4
我期望在日志中看到的内容是这样的:
[pool-4-thread-13] Error!
stack trace line 1
stack trace line 2
stack trace line 3
stack trace line 4
[pool-4-thread-17] Event 1 happened
[pool-4-thread-17] Event 2 happened
config.yaml
server:
adminContextPath: /admin
adminConnectors:
- type: http
port: 9193
applicationContextPath: /
applicationConnectors:
- type: http
port: 9192
maxRequestHeaderSize: 64KiB
requestLog:
timeZone: UTC
appenders:
- type: file
currentLogFilename: ./logs/service-requests.log
archive: true
# When the log file rotates, the archived log will be renamed to this and gzipped. The
# %d is replaced with the previous day (yyyy-MM-dd). Custom rolling windows can be created
# by passing a SimpleDateFormat-compatible format as an argument: "%d{yyyy-MM-dd-hh}".
archivedLogFilenamePattern: ./logs/service-requests.log.%d.log.gz
# The number of archived files to keep.
archivedFileCount: 30
logging:
level: INFO
loggers:
# We only care about warning log messages from the apache libraries, like hadoop, zookeeper, and phoenix
org.apache: WARN
appenders:
- type: file
timeZone: UTC
currentLogFilename: ./logs/service-log.log
archive: true
archivedFileCount: 30
archivedLogFilenamePattern: ./logs/service-log-%d.log.gz
logFormat: "[%date{dd MMM yyyy HH:mm:ss}] [%thread] %-5level %logger - %message %n"