Ensuring the value of logged events

Logs are useful sources of information when looking into problems found in production installations. However, to make the most of a logging infrastructure, you at least have to ensure that the right events get logged.

For example, suppose you have a Java EE system running JBoss, and you would like to ensure that all accesses to your service beans should be logged before and after the execution of the service methods. To this end, you can decide to make use of the interceptors infrastructure offered by JBoss and implement your own interceptor that adds the before and after.

The code would look like:

@Interceptors({BeforeAfterLogger.class})
@Service
public class AService {...}

Now, in order to ensure that all your @Service classes have the @Interceptors in place, you can write a little checker:

model allModelClasses select: [:each |
   (each isAnnotatedWith: 'Service') and: [
      (each isAnnotatedWith: 'Interceptors') not]]

The script above retrieves all violations of the rule. If later on you want to add more interceptors, you can tighten the rule and check for the exact interceptor class.

model allClasses select: [ :each |
   (each isAnnotatedWith: 'Service') and: [
      each annotationInstances
         detect: [ :a | a name = 'Interceptors']
         ifOne:  [ :a | a attributes notEmpty and: [
               '*BeforeAfterLogger.class*' match: a attributes first value ] ]
         ifNone: [ false ] ] ].

Just because logs offer useful dynamic information, does not mean that they cannot be reasoned about using static analysis.

Posted by Tudor Girba at 9 August 2013, 11:35 pm with tags story, assessment, moose link
|