Friday, March 13, 2009

AspectJ for Java logging: Serviceability without the clutter - part 2


In the previous entry about an Aspect for Java logging, I forgot to add a higher-level picture of the end-results.

Let's say your Java project has 4 major packages:
  1. my.otherproject.A
  2. my.project.B
  3. my.project.B.parsers
  4. my.project.C

With the Java logging aspect added to the code base, you can invoke your program with the standard Java logging option:
java -Djava.util.logging.config.file=<logging.properties> application.class

where <logging.properties> is a standard Java logging configuration file.

You can read more about the structure of this file in the Java Logging Overview, but here is an example of what I use:

# "handlers" specifies a comma separated list of log Handler 
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers = java.util.logging.ConsoleHandler

# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# Default global logging level.
# This specifies which kinds of events are logged across all loggers.
# For any given facility this global level
can be overriden by a facility
# specific level
.
# Note that the ConsoleHandler also has a separate level

# setting to limit messages printed to the console.
trace.my.level = INFO
trace.my.project.level = FINER

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = project%u.%g.log
java.util.logging.FileHandler.limit = 5000000
java.util.logging.FileHandler.count = 5
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = FINEST
java.util.logging.FileHandler.append = false

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


-----





Since the Java logging handles are hierarchical, the declaration of "trace.my.level = INFO" sets the default to all packages, whereas the declaration "trace.my.project.level = FINER" entry sets a different tracing level for your instrumented classes under the "my.project" Java package.





Dealing with verbose classes





One can tweak the "method" join point to exclude verbose classes from the tracing aspect, along the lines of:





    pointcut method():


        execution(* my.project..*.*(..)) &&


        !within(my.project.D.*) &&


        !within(Logging) &&


        !within(excluded.class.1) &&


        !within(excluded.class.2) &&


        !within(excluded.class.n);


 





Dealing with methods inside loops






This point is a bit more complicated and I'll get to it in a different post.

No comments:

Post a Comment