How to get slf4j-android to honor Logcat logging level? - android

I am using slf4j-android 1.6.1-RC1 via gradle/maven and when I call Log.debug nothing comes out in Logcat under Android Studio 0.3.5 when I run an application in the emulator.
Just for fun I tried the following:
private final Logger Log = LoggerFactory.getLogger(MainActivity.class);
...
Log.debug("Got this far, woohoo!");
android.util.Log.d("blah","I am here!");
The Log.d's output did appear in Logcat but Log.debug did not.
I checked Log.isDebugEnabled() and sure enough it is set to false. But that seems weird since android.util.Log.d works just fine. Shouldn't slf4j be using the same log level? In fact, shouldn't slf4j just be calling android.util.Log under the covers?
I also replaced Log.debug with Log.error and that did work. So the problem seems to be that slf4j has somehow decided that debug events shouldn't be emitted even though Log.d will emit them.
How do I get slf4j to honor the log level set in Logcat in Android Studio like android.util.Log does?

If you look at the source for slf4j-android, you can see that it calls android.util.Log#isLoggable to decide if the log entry should be made. The javadoc for isLoggable says (my emphasis):
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.
So by default calling slf4j's Logger.debug will do nothing. On the other hand, android.util.Log.d doesn't call isLoggable, and so the log entry is made.
None of the options mentioned in the javadoc are palatable, which rather renders slf4j's Logger.debug less than useful. It'd be nice if the logger could be programatically configured to ignore isLoggable, but at the time of writing this it can't.
See also Does Log.isLoggable returns wrong values?

I found this version much easier to use: http://noveogroup.github.io/android-logger/
You can set the desired log level in an android-logger.properties configuration file. It is not exactly honoring the Logcat log level but at least you can show debug messages without messing around with setprop when using slf4j-android

You can also use https://github.com/mvysny/slf4j-handroid - a special fork which logs debug messages during development phase; it also contains workarounds for bugs in Android Studio 1.5 not logging certain exceptions.

This worked for me. To change the default INFO level to DEBUG for the class that called LoggerFactory.getLogger(MyClass.class), type this at the command line:
adb shell setprop log.tag.MyClass DEBUG
Then all of the DEBUG and higher output for MyClass will be in logcat.

Related

Android : How to enable logging Log.v values which are by default disabled

I am a novice to the Android Studio and working on one group project.I am trying to log some variable values to logging.
When I use Log.e values are properly logged. While when I use Log.w or Log.v nothing gets printed. Here I am switching from logcat from error to verbose and to warning as I am using Log but still can't see anything printed.I previously used Toast but heard that using it might make my app slow and using Log.e everywhere is not a good practice.So how to print logs of lower priority?
I tried referring following resources but didn't find anything that could help me enabling logging for lower priority Logs, Just got the info that for some reason lower priority logs are disabled.
https://developer.android.com/reference/android/util/Log.html
Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?
Thank you in advance.
All logs are always default enabled.
I think you are viewing logs in error view.
Check
You are viewing logs in Verbose
Your filter is set to selected app.
You have selected your app not other app. (where in below image no debuggable process is written)
Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?
Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch statement. You know that an error has occurred and therefore you're logging an error.
Log.w: Use this when you suspect something shady is going on. You may not be completely in full on error mode, but maybe you recovered from some unexpected behavior. Basically, use this to log stuff you didn't expect to happen but isn't necessarily an error. Kind of like a "hey, this happened, and it's weird, we should look into it."
Log.i: Use this to post useful information to the log. For example: that you have successfully connected to a server. Basically use it to report successes.
Log.d: Use this for debugging purposes. If you want to print out a bunch of messages so you can log the exact flow of your program, use this. If you want to keep a log of variable values, use this.
Log.v: Use this when you want to go absolutely nuts with your logging. If for some reason you've decided to log every little thing in a particular part of your app, use the Log.v tag.
Explanation by link Kurtis Nusbaum
Update:
If above things does not work then you are facing an device setting issue. Some mobiles have set default log level to DEBUG or ERROR. Allow logging from phone setting.
You can check if log is loggable by Log.isLoggable()
Check
Settings -> Accessibility -> Developer options -> advanced logging->set "Allow all"
or
Settings->Accessibility - > Developer Options -> Performance optimization -> Advanced logging -> set "Allow all"
or for other phone search in "developers options": option "logging" and set "all".
also you can use Log.wtf when your Log.d is not working.
try restarting android studio also

Logcat not showing errors from my Huawei P9 phone

I am using Android Studio 2.3 and my phone is Huawei P9 Lite. I noticed that my logcat does not show errors from my phone. For example if I have code that will crash because of NullPointerException and I run it on emulator I get exception in logcat (font color: red) and popup saying MyApplication stopped working, but if I run same that code on my phone, i just get crash popup and nothing in logcat. Info messages are displayed, but errors are not. What do I need to do in order to show error messages from my phone in logcat?
Huawei disables LogCat and any access to the logs of your phone. You should go to a hidden menu where you can enable these functions.
Open your Dialer app and enter the following code: *#*#2846579#*#*.
Enter the Background Settings page.
Click on “Log Settings”
Check all 3 options to enable full logging.
Ignore the "will affect performance" warning.
Reboot the phone.
This helped me, hope it will help to you, too.
Source: https://www.xda-developers.com/huawei-phones-disable-logcat-heres-how-to-restore-access/
I was facing the same issue and I solved it as below:
Select Tools -> Android -> then disable ADB integration and then re-enable it. Hope it'll help you
Don't close this popup as this will clear your logcat.
if nothing has appeared try the emphasized text
First, make sure you have chosen your mobile from here
and set the filter to verbose:
You can use debug .
You can use Log :
int ASSERT
Priority constant for the println method.
int DEBUG
Priority constant for the println method; use Log.d.
int ERROR
Priority constant for the println method; use Log.e.
int INFO
Priority constant for the println method; use Log.i.
int VERBOSE
Priority constant for the println method; use Log.v.
int WARN
Priority constant for the println method; use Log.w.
Example :
if i want to log to error log so i will use
Log.e("TAG","Message");
its will print in error log "Message"
and you can fillter it by the tag "Tag"

Why are Log.d() messages showing on device when they shouldn't?

Android docs indicate:
The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
But try to do a Log.d() and you'll find it's actually still recording to Logcat on a real device.
Does anyone know why? Or how to disable it?
Thank you!
What you are seeing is expected behaviour. Log.d will always be logged and visible if you use logcat and connect the device. Hence if you dont want debug logs in production app , turn it off. Infact android sdk suggests you do that. This SO answer might help you as well. Should I comment my log calls when creating my final package?
Andriod sdk says
Turn off logging and debugging
Make sure you deactivate logging and disable the debugging option
before you build your application for release. You can deactivate
logging by removing calls to Log methods in your source files. You can
disable debugging by removing the android:debuggable attribute from
the tag in your manifest file, or by setting the
android:debuggable attribute to false in your manifest file. Also,
remove any log files or static test files that were created in your
project.
Also, you should remove all Debug tracing calls that you added to your
code, such as startMethodTracing() and stopMethodTracing() method
calls.
Source
You should encapsulate logging in your own class and enable/disable special types of logs when you need.
if verbose is compiled in dvelopment, it would be funny if debug logs not. So signe your app and give it a try, i think the debug messages are missing then.
When you plug your device it activates the debug mode. I don't think it's doing the same if the app run with the device unplugged, though you can not check that...

Understanding of Android Log files

I have Log.d to log some info inside the the source files.
1)Where do you see the output of that Log.d statement? Logcat has way too many process and kernel operations listed just junked up the whole place. I don't need to know all those internal system operations. I only need to list the Log.d statement.
You can specify a filter as described in the 'adb logcat' help output. But you need to also silence the things you are not interested in. For instance, if you specified the Tag as 'MyApp' you can just have logcat dump the MyApp lines using:
adb logcat 'MyApp:v,*:s' which silences everything (*:s) and displays anything tagged with 'MyApp' that is verbose and above. For debug and above use 'MyApp:d' instead.

Android Dev: Logcat in Eclipse - can't restrict output to V/D/I/E?

I'm using Logcat in Eclipse. I've been unable to find documentation online telling me as such, but presume that the V, D, I, E etc. buttons when clicked should restrict Logcat output to that pipeline? No matter which I select, every logcat output is spewed out, and it's making debugging nigh on impossible.
Can someone please tell me how to display only Debug output, i.e. Log.d(..) prints? I don't think this is possible using a filter, since I've tried creating one with log level 'Debug' and an empty tag and pid field, yet this simply prints all log outputs again.
Many thanks!
UPDATE
So, thanks to Aleadam's link below, I'm now aware that V/D/E/ etc. are 'priority levels' and include those priorities lower than themselves. It doesn't seem possible to restrict to only D(ebug) output, but at least I know how to restrict to D,V since V(erbose) is the only priority level lower than Debug:
adb logcat *:W
Of course, this must be done via the command line, so I'm now going to figure out how to employ the same strategy in Eclipse. The buttons really aren't working for me, I've no idea why, but it's frustrating!
Many thanks for all the information you've posted, guys.
Those buttons should work in the manner you're describing so long as they were logged with the appropriate API call in the Log class. http://developer.android.com/reference/android/util/Log.html
Regexes/wildcards do not work. There's a bug I requested from almost a year ago. http://code.google.com/p/android/issues/detail?id=11580&can=4&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
adb logcat *:D will not show only debug, but everything with debug priority or higher.
For example, adb logcat *:E > err.log will save errors only.
For many options, check "Filtering Log Output" in here: http://developer.android.com/guide/developing/tools/adb.html#logcat
If you're running under linux use grep. If you're on windows, use the shell: (e.g. adb shell "logcat |grep 'D\/'")
Filtering works on my system but I specify a Log Tag. This then creates a new button next to Log. So for Log Tag "QPR" I see [Log][QPR] buttons and when I press on [QPR] I only see logging that has been tagged as "QPR" as in Log.d(TAG,"onCreate",e);
At least on my system if I click on [Log] (E) I only get errors.
Hope that helps,
JAL

Categories

Resources