Skip to content

Commit 4c8a1d9

Browse files
authored
Update SDK logger to truncate log tags that are longer than 23 characters in length (#1105)
1 parent 0a964c5 commit 4c8a1d9

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Release 2.15.0](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.15.0)
44

5+
### Bug Fixes
6+
7+
- **AWS Core Runtime**
8+
- Update `LogFactory.getLog` to automatically truncate the log tag to be within 23 character limit imposed by Android for Nougat(7.0) releases and prior(API <= 23). See [issue #1103](https://github.com/aws-amplify/aws-sdk-android/issues/1103)
9+
510
### Mis. Updates
611

712
- **Breaking Changes**

aws-android-sdk-core/src/main/java/com/amazonaws/logging/LogFactory.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
* for backwards compatibility.
2424
*/
2525
public 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

Comments
 (0)