[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
Mon Mar 22 15:34:01 CET 2010
http://bugzilla.slf4j.org/show_bug.cgi?id=173
--- Comment #6 from Lorenzo Carrara <lorecarra at gmail.com> 2010-03-22 15:34:01 ---
(In reply to comment #5)
> Do you agree that the fix merely truncates tag names longer than 23 characters,
> like so:
>
> private static final int MAX_TAG_LENGTH = 23;
>
> AndroidLogger(final String name)
> {
> // fix for bug #173: trim tag length in case it exceeds maximum
> length
> this.name = (name != null && name.length() > MAX_TAG_LENGTH)?
> name.substring(0, MAX_TAG_LENGTH) : name;
> }
>
> Any comments, proposals for a more advanced way on how to shorten tag names?
>
Yes that would solve the issue. The only problem is that patching
AndroidLogger's constructor would result in creating a different logger for all
the tags with the same first 23 chars (so, many loggers for the same trimmed
tag). I've written a patch for org.slf4j.impl.AndroidLoggerFactory class that
basically does the same thing but avoids this problem, this is how it works:
- in getLogger, it checks the tag's length. If it's greater that 23 chars, it
trims out the first part (which usually is the least significant, like a part
of the package).
- then it checks the loggerMap to see if that logger has already been created.
- if there's no logger for that tag, it creates one and, if the tag has been
trimmed, logs a warning about that. Then the new logger is mapped to the
trimmed tag in loggerMap.
This way only one logger is created, and the warning is logged just the first
time it's used (when the logger is created), so the log doesn't get cluttered
with "too long tag" warnings. The drawback is that the "too long" tags are
trimmed every time getLogger is called for that logger, but that could be
solved using a lookup map between long tags and corresponding trimmed ones.
I've been using this patched version of slf4j in an Android project which uses
an external library only using tags longer that 23 chars, and everything works
perfectly.
Patched code follows:
/*
* Created 21.10.2009
*
* Copyright (c) 2009 SLF4J.ORG
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.slf4j.impl;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An implementation of {@link ILoggerFactory} which always returns
* {@link AndroidLogger} instances.
*
* @author Thorsten Möler
* @version $Rev:$; $Author:$; $Date:$
*/
public class AndroidLoggerFactory implements ILoggerFactory
{
//ANDROID: log tag max length, according to Android sources
private static final int TAG_MAX_LENGTH = 23;
//ANDROID: internal logger, logs warnings for too long tags
private static Logger internalLogger = null;
private final Map<String, AndroidLogger> loggerMap;
protected static Logger getInternalLogger() {
if(internalLogger == null)
{
// internal logger name length must be below TAG_MAX_LENGTH, or it will go into
an infinite loop
internalLogger =
LoggerFactory.getLogger("AndroidLoggerFactory");
}
return internalLogger;
}
public AndroidLoggerFactory()
{
loggerMap = new HashMap<String, AndroidLogger>();
}
//ANDROID introduced name length check
/* @see org.slf4j.ILoggerFactory#getLogger(java.lang.String) */
public AndroidLogger getLogger(String name)
{
boolean isTooLong = false;
String tooLongName = null;
if(name.length() > TAG_MAX_LENGTH)
{
// remove the first part of the name, which usually is
the least significant
isTooLong = true;
tooLongName = new String(name);
name = name.substring(name.length() - TAG_MAX_LENGTH,
name.length());
}
AndroidLogger slogger = null;
// protect against concurrent access of the loggerMap
synchronized (this)
{
slogger = loggerMap.get(name);
if (slogger == null)
{
if(isTooLong) getInternalLogger().info("Logger
name " + tooLongName + " is too long, using " + name + " instead");
slogger = new AndroidLogger(name);
loggerMap.put(name, slogger);
}
}
return slogger;
}
}
--
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