Reputation: 35346
Is there any library that will allow to log variables by just adding annotations? For example:
@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);
Upvotes: 16
Views: 16382
Reputation: 1496
It's 2023 now, I found we have alternative choice :)
logback.XLogger https://www.slf4j.org/extensions.html#extended_logger
We can combine these 2 lib to make it.
Upvotes: 0
Reputation: 19185
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
.
Please check slf4j which provides common cases logging annotations.
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
}
Similar of Creating custom annotations
Upvotes: 1
Reputation: 1076
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";
}
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.
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.
Upvotes: 3
Reputation: 41
http://aspect4log.sf.net allows 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.
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)!
Bottom line - if u own the code, aspect4log is your choice. If you do not own the code - go for loggifier.
Upvotes: 3
Reputation: 309008
You can do this relatively easily if you use Spring and aspect-oriented programming. Take a look at that.
Upvotes: 0