开发者

Java logback FileAppender file pattern

开发者 https://www.devze.com 2023-01-18 11:00 出处:网络
I have appender like this. <appender name=\"Action.FileAppender\" class=\"ch.qos.logback.core.FileAppender\">

I have appender like this.

<appender name="Action.FileAppender" class="ch.qos.logback.core.FileAppender">
<file>logs\\${date}\\${thread}.log</file>
<encoder>
<pattern>%level - %msg%n</pattern>
</encoder>
</appender>

And i want to ${date} a开发者_Go百科nd ${thread} be current date and current thread name. How i can do it?


SiftingAppender might be what you need. It is designed to separate logging events according to "discriminators", which would be date and thread name in your case.


Do you mean you want to set the output filename dynamically during runtime? AFAIK it is not possible straight via configuration. You have two workarounds:

  • set the output filename dynamically from code, or
  • use a Mapped Diagnostic Context, to prepend your log messages with thread-specific data within the same log file.

The first way would be rather tedious and wasteful, since you would need instance-specific loggers and appenders for classes where different instances can be called from different threads. And even then, what if the same object is shared between multiple threads?

The second one is more natural and simple. Moreover, with a bit of postprocessing using e.g. a simple shell script, you can even filter out logs belonging to different threads into distinct log files.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号