Log4j2按模块名称分开的日志文件 [英] Log4j2 separate log files by module name
问题描述
我想根据模块名称编写日志文件. 对于前. 一个用于用户管理模块的日志文件,以及 一种用于产品模块等.
I want to write log files based on module name. for ex. one log file for user management module and one for products module etc.
我的项目包结构如下所示;
My project package structure looks like below;
com.mycompany.service.user
com.mycompany.service.product
com.mycompany.controller.user
com.mycompany.controller.product
...
我想写来自的日志消息
com.mycompany.*.user
(com.mycompany.service.user和com.mycompany.controller.user)到com.mycompany.user.log
文件,
和com.mycompany.*.product
到com.mycompany.user.log
文件.
I want to write log messages from
com.mycompany.*.user
(com.mycompany.service.user and com.mycompany.controller.user) to com.mycompany.user.log
file,
and com.mycompany.*.product
to com.mycompany.user.log
file.
我知道我可以创建像波纹管这样的记录器
I know I can create loggers like bellow
<logger name="com.mycompany.service.user" level="INFO">
<AppenderRef ref="AppenderUsers" />
</logger>
<logger name="com.mycompany.controller.user" level="INFO">
<AppenderRef ref="AppenderUsers" />
</logger>
...
但是通过这种方式,我必须添加许多记录器.
不能使用通配符*
或**
或正则表达式来记录这样的记录器名称吗?
But this way I have to add so many loggers.
Cant I use wildcard *
or **
or regex to logger name somethihng like this?
<logger name="com.mycompany.*.user" level="INFO">
<AppenderRef ref="AppenderUsers" />
</logger>
或者有什么办法可以做到?
Or is there any way to do this?
推荐答案
最后,我已经通过编写自定义过滤器插件解决了这个问题. 我使用了 Regex 过滤器中使用的相同方法.但是正则表达式过滤器,将给定的正则表达式与Message内容匹配.但是我写了一个过滤器来匹配正则表达式和类名.
Finally I have solved this by writing a Custom Filter Plugin. I have used same approach used in Regex filter. But Regex filter, match given regex with Message content. But I have written a filter to match regex with class name.
我已经在我的博客文章中对此进行了描述. http://rohithag.blogspot.com/
I have describe this in my blog post. http://rohithag.blogspot.com/
这篇关于Log4j2按模块名称分开的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!