Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Removing Logging at Deploy

Help
2008-10-20
2013-04-26
  • Johan Karlsson
    Johan Karlsson
    2008-10-20

    Hi,

    I have received a lot of questions on how to remove the debug code when doing the final release. Therefore I will try to answer this once and for all.

    There are a number of ways to solve this:

    1. Check if debug is enabled.

    if(log.isDebugEnabled()){
        log.debug("Value x is "+x);
    }

    This will avoid creating the combined String object, and thus avoiding object creation. The call to log.debug() will never be done, which also saves some time.

    2. Checking a static final variable

    public static final boolean LOG_ENABLED = true;

    if(LOG_ENABLED){
        log.debug("Value x is "+x);
    }

    If the LOG_ENABLED is set to false, the code will be removed as dead code. This will save both time & space.

    3. Using a pre-processor, for example the ProGuard pre-processor.

    //#ifdef LOG_ENABLED
    log.debug("A log message");
    //#endif

    This will make it possible to remove all the logging code when desired.

    4. Using AspectME.

    See the AspectME documentation on how to write an aspect that does the logging for you.

    This method injects the logging code when needed. If you to disable logging, no extra code is added to your application.

    The approach that you choose depends on your needs and personal preferences.

    Best Regards
    Johan Karlsson

     
    • Karsten Ohme
      Karsten Ohme
      2008-10-20

      I can also add another jour instrumentation which does this out of the box. No other program is then needed for stripping debug code. This should be only some lines of code. The advantage is that the code stays free of #ifdef comments.

      Injecting log statments with aspcets, e.g. AspectME might not enough. It is OK for tracing the entering and leaving of methods, but for log statements with detailed information within methods it is not applicable. Here still manual logging is needed, which might be removed later. Stay tuned and give me two weeks.

      Karsten

       
    • Johan Karlsson
      Johan Karlsson
      2008-10-21

      Hi Karsten,

      that sounds great! Looking forward to see your code.

      Best Regards
      Johan

       
    • Johan Karlsson
      Johan Karlsson
      2008-10-21

      Hi,

      BTW I have added the above information to the FAQ page. Please update the FAQ when your solution is checked-in.

      Best Regards
      Johan

       
    • Tom
      Tom
      2009-07-07

      Is this issue still not resolved?

      It sounds like the best way to remove logging for a release build is to wrap every log statement with a preprocessor statement, as in example #3, given above.

      Is there any way to do a preprocessor statement on the same line as the log statement?  I come from a C/C++ background where we would write something like:
      LOG( "blah", x );
      and the whole thing would dissapear for release builds.  As far as I can tell, the J2ME equivelent is (as shown above):

      //#ifdef LOG_ENABLED
      log.debug("A log message");
      //#endif

      It would be nice if there were some way to do this on one line, or to somehow automate the whole process.

      Also, I see a reference in this thread to a FAQ - where is that?

       
      • Johan Karlsson
        Johan Karlsson
        2009-07-07

        Hi,

        for me this is not an issue in Microlog, this is just a description of how to solve the problem with removing logging. The removal is not part of Microlog. However Microlog has the isDebugEnabled() method that could be used for removal.

        If you look for pre-processor statements in Java ME, you could either use NetBeans or Eclipse with MTJ. These both has support for pre-processing.

        The FAQ: http://microlog.sourceforge.net/snapshot/microlog-logger/faq.html

        Regards
        Johan

         
    • Tom
      Tom
      2009-07-07

      I didn't mean to suggest that it was a problem with microlog.  I guess I should say that it is a J2ME issue that is relevant to logging.

       
      • Johan Karlsson
        Johan Karlsson
        2009-07-09

        Hi,

        then the answer; it has not been solved in J2ME. But I think that is is a general Java problem, since no pre-processing support is available in Java SE. You need to use a tool that is not part of the official Java language.

        Regards
        Johan