Log4j tracing

July 19th, 2005

I write this a lot:

logger.debug("varName: "+varName);

Starting to think about this, would there be a way to have log4j handle this for you? So you could just write something like

logger.debugVar(varName);

and it would give the same result as above. It seems pretty hard (the variable name is not passed through the method call), but if anybody has an idea for this: shoot!

9 Responses to “Log4j tracing”

  1. a Says:
    Make an eclipse macro. That's how we do it.
  2. James Strachan Says:
    If one day the java compiler handled annotation processing inside method bodies you could do @DebugVar(varName) though I'm not sure if that'll ever be supported. Though other languages like Groovy should be able to support this kinda stuff easily.
  3. andrej Says:
    1. you can write an enhancer to produce the code. f.e. you can do that with ASM. find all debugVar() occurences in the code and replace them with your code. 2. if you like commons-logging instead of log4j you can try my product. it is based on commons-logging and allow you to write log-messages as templates in the following kind: LOG.debug( "my variable : ${myVar}" ); in this example "LOG" is a class and "debug" is a static no-op method. with pencil (the name of my product) this code will be transformed to a common-logging statement: if( LOGGER.isDebugEnabled() ) { LOGGER.debug( new StringBuffer() .append("my variable : ") .append(myVar) ); } if you like this way, i can also develop a version with the above named debugVar() functionality; in the actual version log4j is not supported. but in the future i will implement the support of log4j. maybe in the 0.3 release. the actual version is maybe more for testing. i've not used this product in production. but if you like to play, it is maybe a good thing for you ;) url: http://pencil.dev.java.net
  4. Tom Klaasen Says:
    a: Hmm, that sounds like a good idea. I'll just have to figure out how to write macros in eclipse; James: maybe, one day...; andrej: I don't like the idea of post-processing the code. It would give me red wiggly lines in eclipse. And the whole idea is to use auto-completion, so your second option doesn't seem to be a viable either. Thanks for the comments everybody!
  5. andrej Says:
    1. macros in eclipse ? do you mean templates? if not, template is an alternative. 15 minutes. ;) 2. i'm also very careful with post-processing. but in my solution you have no red lines ;) and the debuger works also in the same way like before. but the argument with auto-completion in your favorite ide is right! it's not ready yet. good luck, andrej
  6. Ron Says:
    how does an eclipse template help you? your code ends up infested with these log statements. so what you saved some typing.
  7. eu Says:
    Actually it is possible to resolve variable name by its value in the runtime. As long as debug info is in there... http://jroller.com/page/eu/20050516#resolving_local_variable_name_passed
  8. Keith Pitty Says:
    Yes, Eclipse templates are the way to go for this. Within Eclipse, see Window --> Preferences --> Java --> Editor --> Templates and have a look at how the pre-defined templates are set up. In a few minutes you'll have worked out how to create your own template.
  9. Nev Says:
    Here's a half-formed thought - call it like this logger.debugVar("varName"); The method can then look at the stack to find out which class is doing the call and then use reflection to get the field's value - ah that would only work for fields and not locals...

Leave a Reply