Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java library that adds annotations for Logging?

Tags:

java

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);
like image 834
quarks Avatar asked Sep 04 '12 11:09

quarks


People also ask

What are logging libraries in Java?

A Java logging framework is a computer data logging package for the Java platform. This article covers general purpose logging frameworks. Logging refers to the recording of activity by an application and is a common issue for development teams.

What does Lombok use for logging?

Using Log4j2 Logging with Lombok Now you can use either @Slf4j (recommneded) or @Log4j2 at class to use log4j2 as underlying logging implementation.

Does Java have built in logging?

Java provides a built-in framework in the java. util. logging package. There are also many third-party frameworks, including Log4j, Logback, and tinylog.

How do you add logs in Java?

The process of creating a new Logger in Java is quite simple. You have to use Logger. getLogger() method. The getLogger() method identifies the name of the Logger and takes string as a parameter.


2 Answers

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.

like image 116
Hervian Avatar answered Oct 16 '22 15:10

Hervian


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.

like image 24
Mark D Avatar answered Oct 16 '22 14:10

Mark D