Java logback FileAppender 文件模式
我有这样的附加器。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
您的意思是您想在运行时动态设置输出文件名吗? 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:
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.