46

Since update AS 1.1 Preview 2, I'm getting red lines under all my Log messages

Log.d(TAG, "message");

With message: "The logging tag can be at most 23 characters..".

I didn't update anything fundamentally, except Android Studio itself. Is this a bug?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
TomCB
  • 3,983
  • 9
  • 40
  • 66
  • Apparently the error manifests itself when the length of the tag is *equal* to, or *greater* than, 23 characters: `java.lang.IllegalArgumentException: Log tag "tag.with.exactly.23.chs" exceeds limit of 23 characters`. I'm using Android Studio 2.x. – Daniel Aug 24 '16 at 15:10

8 Answers8

45

You can disable it if you so choose.

In Android Studio, Analyze->Inspect Code.

screenshot

Under Inspection Profile, click on the button with the 3 horizontal dots.

The following window should open. Search for "log" and uncheck "Too Long Log Tags".

screenshot

Update: Android Studio 2.2, it is located under Android Lint: Correctness

screenshot

Community
  • 1
  • 1
terencey
  • 3,282
  • 4
  • 32
  • 40
38

No, it's not a bug.

From Android Studio's Recent Changes on 1.1 Preview 2,

Checks that the tag passed to the logging calls, if its value can be resolved, is at most 23 characters long (as required by the Logging API.)

logging tag was 31

As shortly explained on the recent changes, it's due to how Log API doesn't allow tag that exceeds 23 characters.

SLF4J Android has an explanation to this:

[...] the length of such tags is currently limited to 23 characters (23 = 32 - 8 for namespace prefix - 1 for C terminator)

which matches the Android's source code.

Currently, the only function that explicitly mentions this exception is Log.isLoggable(),

...

Throws

IllegalArgumentException is thrown if the tag.length() > 23.

However, based on the comments, apparently the logger does throw the exception on release mode (it's ignored in debug mode).

You can disable the lint checking by following Terence's answer, but you've been warned.

Community
  • 1
  • 1
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • 6
    What is the logic behind this. Sure the error is system generated and there is some correlation between errors i can get and strings i can type but it consists of ASCII characters that i use in my "string", why wouldn't it allow 23 chars, and even why 23 exactly ? Thanks. – Иво Недев Mar 16 '15 at 11:55
  • 3
    From [Android source code](https://github.com/android/platform_frameworks_base/blob/master/core/jni/android_util_Log.cpp#L84), it's due to 32 (property key's max length) - 8 (namespace prefix) - 1 (C terminator), as quoted from [SLF4J Android](http://www.slf4j.org/android/). – Andrew T. Mar 17 '15 at 12:05
  • This may be a stupid comment, but I also saw the lint error but was surprised because logging of tags > 23 chars still work. I am confused :/ – degill Apr 10 '15 at 13:15
  • 1
    @degill that's not stupid at all, because Android actually doesn't check and throw any exception if you use `Log.d`, etc. The only method that may return the exception is only `Log.isLoggable` – Andrew T. Apr 10 '15 at 13:29
  • 2
    This lint check is just nonsense the way it is. You can be pretty sure that Android will *not* start to enforce this in `Log.d` et al. since that would just break tons of apps without *any* good reason. I really hope the lint check will be improved in the future to be shown either less severely or just on usages of `Log.isLoggable()`. – zapl Jul 10 '15 at 17:10
  • 3
    You can NEVER disable this lint check since it crash app in release (The most funny thing is it does not crash in debug mode) – LiangWang Jan 14 '16 at 00:11
  • Just got a crash "Log tag ... exceeds limit of 23 characters" on a real device. Pay attention to lint warnings! – Mikalai Daronin Jul 19 '16 at 17:33
  • 1
    Also, the warning only occurs on minSdk `23` and below: `IllegalArgumentException is thrown if the tag.length() > 23 for Nougat (7.0) releases (API <= 23) and prior, there is no tag limit of concern after this API level.` – Heath Borders Aug 15 '19 at 18:47
  • 2
    @HeathBorders note the quote you gave had a typo, currently the docs say: `IllegalArgumentException is thrown if the tag.length() > 23 for Nougat (7.0) and prior releases (API <= 25), there is no tag limit of concern after this API level.` - after this was fixed in https://issuetracker.google.com/issues/124593220 – arekolek May 12 '21 at 16:11
20

Complementing the answer by @Terence

You can also turn off the specific check via gradle with this in your build.gradle file:

lintOptions {
    disable 'LongLogTag'
}

Or by adding a lint.xml file to your project with xml:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="LongLogTag" severity="ignore" />
</lint>
matt5784
  • 3,065
  • 2
  • 24
  • 42
straya
  • 5,002
  • 1
  • 28
  • 35
7

You can never ignore this lint check, it definitely could bring unexpected results on your release version since it throws exceptions and stops executing (it would not crash your app).

I have had a terrible lesson learned recently: it's OK on debug mode, but behave differently on release version.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
LiangWang
  • 8,038
  • 8
  • 41
  • 54
4

This is recent change and In this build, its a new lint check. Which says,

Checks that the tag passed to the logging calls, if its value can be resolved, is at most 23 characters long (as required by the Logging API.)

For more info, read 3rd point in below link.

https://sites.google.com/a/android.com/tools/recent/androidstudio11preview2

If you dont want to get this, minimize the number of characters in your TAG and make sure that they wont cross the length more than 23.

Makotosan
  • 1,149
  • 2
  • 12
  • 20
Vilas
  • 1,695
  • 1
  • 13
  • 13
  • 1
    At the moment, the provided link does not contain the citation you have. Therefor you should always include the most essential part, like why is this lint check added. – dragi Apr 02 '15 at 08:30
4

To explain why this happens:

According to AOSP source code you can log with any tag you want. The problem is in Log.isLoggable.

Log.isLoggable checks the system property log.tag.<YOUR_TAG> if the priority you want to log is enabled. Here's documentation of this mechanism:

public static boolean isLoggable (String tag, int level)

Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: 'setprop log.tag. ' Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: 'log.tag.=' and place that in /data/local.prop.

Source: https://developer.android.com/reference/android/util/Log#isLoggable(java.lang.String,%20int)

Below API 26 (Oreo) the limit of system property keys was 31 characters. And "log.tag.".length() + 23 equals 31. If you call Log.isLoggable below Android Oreo with a tag longer than 23 characters it will throw, as described in the source code. Since Android O this limit no longer applies.

The Lint rule exists just to shield you from all these (typically) unnecessary details.


The documentation for Log.isLoggable also states the IllegalArgumentException will not be thrown since API 24, which according to my findings, is wrong. Follow: https://issuetracker.google.com/issues/124593220

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Are there any consequences if somehow we disabled max character check and used long tags in the app? – viper Apr 08 '19 at 08:27
  • 1
    You don't disable max character check, it's baked in the OS until Android 8. The check only applies to `Log.isLoggable`. So you can call it with the first 23 characters of the tag. Or check for a common tag, for example my app is called `com.potatoes` so I check if `com.potatoes` tag is loggable and then log as usual. Other than that **you can call `Log.e/w/i/v/d/log` with any long tag.** – Eugen Pechanec Apr 08 '19 at 08:38
3

Solution. Year 2020 ver.

build.gradle (app)

android {
    lintOptions {
        disable 'LongLogTag'
    } // put this. 
}
Jin Lim
  • 1,759
  • 20
  • 24
0

This error was thrown for me for a node_module library, complaining about a separate node_mode library.

I added this lint options property within that node_module library's build gradle file.

  android {
      lintOptions {
          abortOnError false
      }
  }

The library was aws-amplify push notification.

Error: Execution failed for task ':@aws-amplify/pushnotification:lint'

File updated: node_modules/@aws-amplify/pushnotification/android/build.gradle

Dylan w
  • 2,565
  • 1
  • 18
  • 30