Java logback FileAppender 文件模式

发布于 2024-09-25 23:56:54 字数 333 浏览 0 评论 0原文

我有这样的附加器。

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

我想 ${date} 和 ${thread} 是当前日期和当前线程名称。我怎样才能做到呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-10-02 23:56:54

SiftingAppender 可能就是您所需要的。它旨在根据“鉴别器”分离日志记录事件,在您的情况下,鉴别器是日期和线程名称。

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.

行雁书 2024-10-02 23:56:54

您的意思是您想在运行时动态设置输出文件名吗? AFAIK 不可能直接通过配置。您有两种解决方法:

  • 从代码动态设置输出文件名,或
  • 使用映射诊断上下文 ,在同一日志文件中在日志消息前添加线程特定的数据。

第一种方法相当乏味且浪费,因为您需要针对可以从不同线程调用不同实例的类的特定于实例的记录器和附加程序。即便如此,如果多个线程之间共享同一个对象怎么办?

第二种更自然、更简单。此外,通过使用简单的 shell 脚本等进行一些后处理,您甚至可以将属于不同线程的日志过滤到不同的日志文件中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文