2 Replies Latest reply on Jan 9, 2012 7:08 AM by Andrew Redhead

    solder logging v seam 2 logging

    Andrew Redhead Newbie

      Hi,


      I've spent some time working with Seam 2.x and am starting to play with CDI\Weld and Seam 3.


      I was looking for a sensible way to do some simple logging from my classes and thought solder logging would fit the bill.


      Having looked at the code for solder logging I have a couple of questions about the design of solder logging...


      In Seam 2.x you can use @Logger to get an instance of seam 2 Log, which turns out to be an instance of LogImp, this class has methods such as:




         public void debug(Object object, Object... params)
         {
            if ( isDebugEnabled() )
            {
               log.debug(  interpolate(object, params) );
            }
         }
         
         public void debug(Object object, Throwable t, Object... params)
         {
            if ( isDebugEnabled() )
            {
               log.debug(  interpolate(object, params), t );
            }
         }



      I like this because it means I don't need to write all if debug enabled guards around my debug statements.


      Looking at solder Logger, there is code such as:




          /**
           * Issue a log message with a level of TRACE using {@link java.text.MessageFormat}-style formatting.
           *
           * @param t the throwable
           * @param format the message format string
           * @param params the parameters
           */
          public void tracev(Throwable t, String format, Object... params) {
              doLog(Level.TRACE, FQCN, format, params, t);
          }
      
          /**
           * Issue a log message with a level of TRACE using {@link java.text.MessageFormat}-style formatting.
           *
           * @param t the throwable
           * @param format the message format string
           * @param param1 the sole parameter
           */
          public void tracev(Throwable t, String format, Object param1) {
              if (isEnabled(Level.TRACE)) {
                  doLog(Level.TRACE, FQCN, format, new Object[] { param1 }, t);
              }
          } 



      The key point being that some methods check that the requested log level is being recorded and others do not.


      I can't work out why!


      I had assumed that all the log methods would check to make sure that the log message would be recorded before pressing on with all the string manipulation (as seam 2 logging does).


      Can anyone explain to me why solder logging sometimes checks the log level and sometimes doesn't?


      Apologies if I've missed some obvious doco on the net, I have tried googling but didn't find anything.


      Further apologies if this is the wrong forum - I wasn't sure if I should post here or in the weld forum.


      Thanks, Andy



        • 1. Re: solder logging v seam 2 logging
          Ken Finnigan Master

          Andy,


          In the Logger class of Solder you'll find that the isEnabled check is only performed on methods in which it needs to perform an action such as new Object[] { param1 }, as you note above.  At this point there has been no String manipulation.


          If you then check out doLog() in each of the implementation classes for JDK, Log4J and SLF4J, you'll notice that they all perform the level enabled check before calling the underlying Logger implementation, which is where the String manipulation occurs.


          Regards
          Ken

          • 2. Re: solder logging v seam 2 logging
            Andrew Redhead Newbie

            Hi,


            thanks for your reply :)


            Deferring the check until something expensive is about to happen (and you are close enough to the thing that is going to do the work to ask) makes sense.


            It looks like the Slf4j and JDK logger implementations make the log level check in the doLog function, however the Log4j implementation (Log4jLogger) does not:




                protected void doLog(final Level level, final String loggerClassName, final Object message, final Object[] parameters, final Throwable thrown) {
                    logger.log(loggerClassName, translate(level), parameters == null || parameters.length == 0 ? message : MessageFormat.format(String.valueOf(message), parameters), thrown);
                }





            I can't see the code that makes the level check?


            Cheers, Andy