mercredi 17 février 2016

Object Oriented Event Logging in Java API Design

I want to solve this problem:

Most apps have some logging service for reporting events in the application to a business intelligence analytics database. Also, mobile apps like android report the same things like user behavior and so on. There are many libraries to do this.

Problem: When developers want to add logging to their code, it's usually by scattering lines of code that report something inside their methods, for example:

      void doStuff() {
         try {
             doTheThing():
             logger.info("the thing was done", someClientInfoDTO, timestamp, eventid);
            } catch(Throwable woops) {
                logger.error(woops);
            }

           }

I see this pattern everywhere in one form or another. I want to take an object oriented approach to logging so classes can be annotated with their logging behavior. An eliminate the need for developers to just stick lines of logging code where they feel something important enough to log is happening.

Where my mind is is to rewrite the above code like this:

@Logger("class level logger")
class StuffDoer {
    @Logger("what to log", whatToDoOnError, etc)
    void doStuff() {
                doTheThing():
                   }
}

Question: Are there any libraries or existing projects that address the problem. Does anyone have an example of doing this successfully? I don't want to reinvent the wheel but i've been digging and coming up empty. Based on the response to this question i may start a github project to do this.

Jake Wharton gets us part way there with timber: http://ift.tt/1qtNv9h which is a good way for injecting a logging framework, but still results in devs peppering code with calls to it.

Aucun commentaire:

Enregistrer un commentaire