我的问题是,我的项目中的许多方法现在需要存储它们的日志,AOP并不是很可行需要。
注释方法将调用一种自定义方法,以存储每当登录其中的东西时存储日志消息。
我从来没有做过注释,也不真正熟悉反思,所以我想知道这样的事情是否可以可行,还是您建议某种方法。
非常感谢。
My problem is that a lot of methods in my project now require to have their logs stored, AOP isn't very viable since there isn't an appropriate point to cut, so I'm thinking about making a custom annotation and putting it wherever it's needed.
Annotated methods would call a custom method to store the log message whenever something is logged inside it.
I have never made annotations and I'm not really familiar with reflection, so I would like to know if such a thing would be doable, or is there some kind of approach you would suggest.
Thank you very much.
发布评论
评论(2)
最后,我使用记录过滤器过滤所有日志记录事件,然后使用iLoggingeent的stacktrace来找出记录事件的堆栈中是否存在注释。
注释:
这是过滤器的实现:
要找到一个注释方法:
logback.xml:
In the end I used the logback filter to filter all logging events, then used the stacktrace from the ILoggingEent to find out whether an annotation is present in the stacktrace of the logging event.
Annotation:
Here's the implementation of the filter:
To find an annotated method:
Logback.xml:
您可以从 lombok )。它可以帮助您定义一个默认的
日志
实例,您可以在非常注释的类中使用。编辑:您也可以使用 interceptor
但是,如果您仍然想要使用反射使用您的自定义注释,总是可能的。
You can use
Slf4j
annotation from lombok. It helps you define a defaultlog
instance that you can use in very annotated classes.Edit: You can also use an interceptor
But if you still want to use reflection with your custom annotation, it is always possible.