[slf4j-dev] [Bug 173] slf4j android: Android throws an IllegalArgumentException when Log Tag length exceeds 23 characters

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Wed May 19 21:38:23 CEST 2010


http://bugzilla.slf4j.org/show_bug.cgi?id=173


Wendell <wendell_temp_1234 at comcast.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wendell_temp_1234 at comcast.ne
                   |                            |t




--- Comment #10 from Wendell <wendell_temp_1234 at comcast.net>  2010-05-19 21:38:22 ---
I ran into the same thing and created a fix that uses the more useful part of a
full class name if possible:

public class AndroidLoggerFactory implements ILoggerFactory
{
   private static final int MAX_TAG_LENGTH = 23;

   private final Map<String, AndroidLogger> loggerMap;

   public AndroidLoggerFactory()
   {
       loggerMap = new HashMap<String, AndroidLogger>();
   }

   /* @see org.slf4j.ILoggerFactory#getLogger(java.lang.String) */
   public AndroidLogger getLogger(String name)
   {
       name = forceValidTag(name);

       AndroidLogger slogger = null;
       // protect against concurrent access of the loggerMap
       synchronized (this)
       {
           slogger = loggerMap.get(name);
           if (slogger == null)
           {
               slogger = new AndroidLogger(name);
               loggerMap.put(name, slogger);
           }
       }
       return slogger;
   }

   protected String forceValidTag(String tag)
   {
       if (tag.length() > MAX_TAG_LENGTH) {
           // try to do something expected

           // if there's a '.' in the tag, it probably came from a fully
qualified class name
           // use the trailing end

           // extra character because we're going to remove the '.'
           String lastPart = tag.substring(tag.length() - MAX_TAG_LENGTH - 1);

           int dot = lastPart.indexOf('.');
           if (dot >= 0 && dot != lastPart.length() - 1) {
               // return as much of the dotted path as will fit
               return lastPart.substring(dot + 1);
           }
           else {
               // no useful dot location, just take the beginning
               return tag.substring(0, MAX_TAG_LENGTH);
           }
       }
       else {
           return tag;
       }
   }
}


-- 
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the slf4j-dev mailing list