Java 的日志注解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12262824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging annotations for Java
提问by quarks
Is there any library that will allow to log variables by just adding annotations like:
是否有任何库允许通过添加注释来记录变量,例如:
@Log
String someValueToLog = someMethod();
@Log(level="debug",prefix="Here's the value=")
String someValueToLogAgain = someMethod();
That functions similar to adding this line in the code:
其功能类似于在代码中添加这一行:
log.info(someValueToLog);
log.debug("Here's the value="+someValueToLogAgain);
采纳答案by Hervian
I have created a project called log-weaver, which introduces a number of @LogXXX statements.
When you compile a project, that uses one of these annotations, Log-statements are weaves into the bytecocde.
Example source code:
我创建了一个名为log-weaver的项目,它引入了许多 @LogXXX 语句。
当你编译一个使用这些注释之一的项目时,日志语句被编织到字节码中。
示例源代码:
@LogEnteringAndExiting(value={"arg1", "this"})
public String execute(String arg1) {
/*Some logic*/
return "done";
}
The source code will remain as is, but the byte code will look as if the source code had been written like this:
源代码将保持原样,但字节码看起来好像源代码是这样编写的:
private static final Logger comGithubHervian_LOGGER = LoggingHelper.getLogger(ClassWithLogAnnotatedMethods.LogAround_log1ArgumentAndThis.class);
private static final String = comGithubHervian_CLASSNAME = ClassWithLogAnnotatedMethods.LogAround_log1ArgumentAndThis.class.getName();
public String execute(String arg1) {
if (LoggingHelper.isEntryExitTraceEnabled((Logger)comGithubHervian_LOGGER)) {
comGithubHervian_LOGGER.entering(comGithubHervian_CLASSNAME, "execute", new Object[]{arg1, this});
}
/*Some logic*/
String string = "done";
if (LoggingHelper.isEntryExitTraceEnabled((Logger)comGithubHervian_LOGGER)) {
comGithubHervian_LOGGER.exiting(comGithubHervian_CLASSNAME, "execute", string);
}
return string;
}
In the code above the LoggingHelper is a special class from IBM's WebpShere Commerce for which this proof of concept was developed.
在上面的代码中,LoggingHelper 是来自 IBM 的 WebpShere Commerce 的一个特殊类,为它开发了这个概念证明。
The idea is to simplify the source code by removing trivial statements, in this case logging.
The overall logic is as follows:
这个想法是通过删除琐碎的语句来简化源代码,在这种情况下是日志记录。
大体逻辑如下:
- An AbstractProcessor detects the usage of one the log annotations, and creates some useful data structure, to hold information about the method name, arguments etc.
- The AbstractProcessor registers a TaskListener at the compiler (Javac).
- The TaskListener uses Javassist to weave log statements into the byte code of the given method/class.
- AbstractProcessor 检测日志注释的使用,并创建一些有用的数据结构,以保存有关方法名称、参数等的信息。
- AbstractProcessor 在编译器 (Javac) 上注册一个 TaskListener。
- TaskListener 使用 Javassist 将日志语句编织到给定方法/类的字节码中。
Please be aware that the current project is designed for use with IBM's WebSphere Commerce, but you could easily adjust it, such as to weave log-statements of your own choosing into the code.
请注意,当前项目是为与 IBM 的 WebSphere Commerce 一起使用而设计的,但您可以轻松地对其进行调整,例如将您自己选择的日志语句编入代码中。
回答by Mark D
http://aspect4log.sf.netallows you to log method calls, arguments, returned value, thrown exception (it even allows you to change the log level depending on exception, by default it uses ERROR for unchecked exceptions and WARN for checked exceptions. It helped me a lot in removing boilerplate code and improved logging.
http://aspect4log.sf.net允许您记录方法调用、参数、返回值、抛出的异常(它甚至允许您根据异常更改日志级别,默认情况下,它对未检查的异常使用 ERROR,对已检查的异常使用 WARN . 它在删除样板代码和改进日志记录方面帮助了我很多。
I've also know about http://loggifier.unkrig.de- it does logging via java.util.logging (which no one uses), a bit too complex to setup and not that well document but it has one BIG feature - it claims that it can instrument already compiled jar/war/ear files (which is great if u want to profile someone's uglry jar which u can not recompile)!
我也知道http://loggifier.unkrig.de- 它通过 java.util.logging(没有人使用)进行日志记录,设置起来有点复杂,文档也不是很好,但它有一个很大的功能 -它声称它可以检测已经编译的 jar/war/ear 文件(如果你想分析某人的丑陋 jar,你无法重新编译,这很好)!
Bottom line - if u own the code, aspect4log is your choice. If you do not own the code - go for loggifier.
底线 - 如果你拥有代码,aspect4log 是你的选择。如果您不拥有代码 - 去 loggifier。
回答by Amit Deshpande
Logging is done inside actual logic annotations can be used only for specific elements in the source code. you can at most log LOCAL_VARIABLE
using this but it can never be used to log plain statements
.
日志记录是在实际的逻辑注释中完成的,只能用于源代码中的特定元素。您最多可以LOCAL_VARIABLE
使用它来记录,但它永远不能用于记录plain statements
。
Please check slf4jwhich provides common cases logging annotations.
请检查slf4j,它提供了常见的案例日志注释。
Elements for which Annotation declaration is supported are:
支持注解声明的元素有:
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE
}
}
回答by duffymo
You can do this relatively easily if you use Spring and aspect-oriented programming. Take a look at that.
如果您使用 Spring 和面向方面的编程,您可以相对容易地做到这一点。看看那个。