是否有任何库允许通过添加注释来记录变量,例如:
@Log
String someValueToLog = someMethod();
@Log(level="debug",prefix="Here's the value=")
String someValueToLogAgain = someMethod();
Run Code Online (Sandbox Code Playgroud)
该功能类似于在代码中添加此行:
log.info(someValueToLog);
log.debug("Here's the value="+someValueToLogAgain);
Run Code Online (Sandbox Code Playgroud)
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:
@LogEnteringAndExiting(value={"arg1", "this"})
public String execute(String arg1) {
/*Some logic*/
return "done";
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
In the code above the LoggingHelper is a special class from IBM's WebpShere Commerce for which this proof of concept was developed.
The idea is to simplify the source code by removing trivial statements, in this case logging.
The overall logic is as follows:
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.
| 归档时间: |
|
| 查看次数: |
10264 次 |
| 最近记录: |