简体   繁体   中英

Is there a Java library that adds annotations for Logging?

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);

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.

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:

  1. 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.
  2. The AbstractProcessor registers a TaskListener at the compiler (Javac).
  3. The TaskListener uses Javassist to weave log statements into the byte code of the given method/class.

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.

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

You can do this relatively easily if you use Spring and aspect-oriented programming. Take a look at that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM