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?

1
  • 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
    Commented Aug 24, 2016 at 15:10

8 Answers 8

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

7
  • 12
    What are the consequences of this?
    – Joop
    Commented May 1, 2015 at 15:54
  • Nice advice! It works like a charm. BTW, what is the purpose of preventing long tag?
    – 김준호
    Commented Jul 14, 2015 at 2:22
  • @Joop and 김준호 I've no idea, Android Studio is restricting long tag names for some reason.
    – terencey
    Commented Jul 20, 2015 at 5:03
  • Where is this Analyze->Inspect Code be found?
    – nww04
    Commented Jun 14, 2016 at 4:11
  • 1
    Inspect code for me runs an inspection, its no where close to what the UI looks like from the picture of yours. :/
    – nww04
    Commented Jun 15, 2016 at 13:00
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.

9
  • 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. Commented Mar 16, 2015 at 11:55
  • 3
    From Android source code, it's due to 32 (property key's max length) - 8 (namespace prefix) - 1 (C terminator), as quoted from SLF4J Android.
    – Andrew T.
    Commented Mar 17, 2015 at 12:05
  • 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
    Commented Jul 10, 2015 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
    Commented Jan 14, 2016 at 0:11
  • 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 issuetracker.google.com/issues/124593220
    – arekolek
    Commented May 12, 2021 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>
3
  • both solution doesn't seem to work for Android Studio 1.4 for me. It can't find lintOptions in Gradle and <lint> tags errors out for me. :/
    – nww04
    Commented Feb 13, 2016 at 3:02
  • 2
    You should put lintOptions inside android block
    – ajmal
    Commented Jul 20, 2016 at 9:41
  • 1
    This is the correct and helpful answer. Thank you so much sir. Commented Aug 31, 2018 at 5:29
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.

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.

1
  • 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
    Commented Apr 2, 2015 at 8: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

2
  • Are there any consequences if somehow we disabled max character check and used long tags in the app?
    – viper
    Commented Apr 8, 2019 at 8: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. Commented Apr 8, 2019 at 8:38
4

Solution. Year 2020 ver.

build.gradle (app)

android {
    lintOptions {
        disable 'LongLogTag'
    } // put this. 
}
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.