2323 * for backwards compatibility.
2424 */
2525public class LogFactory {
26+
27+ /**
28+ * NOTE : Any changes to rename this class should ensure that this log tag is no longer than 23.
29+ * Log tag longer than 23 will cause it to break on Android API level <= 23.
30+ */
2631 private static final String TAG = LogFactory .class .getSimpleName ();
2732 private static final String APACHE_COMMONS_LOGGING_LOGFACTORY = "org.apache.commons.logging.LogFactory" ;
2833
@@ -35,29 +40,31 @@ public class LogFactory {
3540 * @return logger
3641 */
3742 public static synchronized Log getLog (Class clazz ) {
38- return getLog (clazz .getSimpleName ());
43+ return getLog (getTruncatedLogTag ( clazz .getSimpleName () ));
3944 }
4045
4146 /**
4247 * Get the logger for the string tag
43- * @param string the string tag
48+ * @param logTag the string tag
4449 *
4550 * @return logger
4651 */
47- public static synchronized Log getLog (final String string ) {
48- Log log = logMap .get (string );
52+ public static synchronized Log getLog (String logTag ) {
53+ logTag = getTruncatedLogTag (logTag );
54+
55+ Log log = logMap .get (logTag );
4956 if (log == null ) {
5057 if (checkApacheCommonsLoggingExists ()) {
5158 try {
52- log = new ApacheCommonsLogging (string );
53- logMap .put (string , log );
59+ log = new ApacheCommonsLogging (logTag );
60+ logMap .put (logTag , log );
5461 } catch (Exception e ) {
5562 android .util .Log .w (TAG , "Could not create log from " + APACHE_COMMONS_LOGGING_LOGFACTORY , e );
5663 }
5764 }
5865 if (log == null ) {
59- log = new AndroidLog (string );
60- logMap .put (string , log );
66+ log = new AndroidLog (logTag );
67+ logMap .put (logTag , log );
6168 }
6269 }
6370 return log ;
@@ -74,4 +81,24 @@ private static boolean checkApacheCommonsLoggingExists() {
7481 return false ;
7582 }
7683 }
84+
85+ /**
86+ * Truncate log tag to be within 23 characters in length as required by Android on certain API levels.
87+ *
88+ * @param logTag Log tag to be truncated
89+ * @return truncated log tag
90+ */
91+ private static String getTruncatedLogTag (String logTag ) {
92+ if (logTag .length () > 23 ) {
93+ if (checkApacheCommonsLoggingExists ()) {
94+ Log log = new ApacheCommonsLogging (TAG );
95+ log .warn ("Truncating log tag length as it exceed 23, the limit imposed by Android on certain API Levels" );
96+ } else {
97+ android .util .Log .w (TAG , "Truncating log tag length as it exceed 23, the limit imposed by Android on certain API Levels" );
98+ }
99+ logTag = logTag .substring (0 , 23 );
100+ }
101+
102+ return logTag ;
103+ }
77104}
0 commit comments