0

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} and ${thread} be current date and current thread name. How i can do it?

sfireman
  • 31
  • 1
  • 5

2 Answers2

2

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Possible this is a way, but this way use MDC. How will work MDC in multithreaded application? – sfireman Oct 04 '10 at 13:26
  • The Mapped diagnostic context (MDC) in Logback uses ThreadLocal to maintain separation between threads, so it works rather well in a multi-threaded application. – Mike Yockey Jan 31 '12 at 13:43
0

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Thanks! Can you help me with example of the first way? – sfireman Oct 04 '10 at 12:05
  • @sfireman, sorry, I can't. It's apparently different from Log4J in this respect. [The manual](http://logback.qos.ch/manual/configuration.html) mentions that it is possible, but gives no examples. – Péter Török Oct 04 '10 at 12:37