How to print a string to logcat for testing? - android

I know this question has been asked before, but I can't find a clear answer. I'm new to Android Studio and the logcat console confuses me. I have a string date and I want to keep 'track' of it in the console so I know what value it has.
I tried commands like Log.i and console.log, but they don't seem to work for me.
Where should I write my print line code?
Where can I see it when it gets printed?
Do I have to run the whole app (on the emulator, or on a device) to print the line to the console?

Where do I write my print line code ?
That is up to you.
Where can I see it when it gets printed ?
In LogCat:
You will get this from the Android view (usually docked on the bottom edge on the left side). Note that I have sometimes encountered a bug where the automatically-applied filter for your app (right-hand drop-down list, above the output) seems to result in nothing showing up in the output. You may need to remove the filters, then adjust the output based on log level.
You can also view LogCat at the command line via adb logcat and in the Android Device Monitor (Tools > Android > Android Device Monitor from the Android Studio main menu).
Do I have to run the hole app (emulator or via a device) to print the line to the console?
Yes.

Your use of Log.i is the correct way to write to Logcat. Here is the documentation for Log.
I typically use a tag that describes the stage I am in (setup, teardown, UI update, etc), and I will usually log any action that can trigger an exception.
To view the Logcat, there should be a window for that in Android Studio. When the windows is not visible, you can access it from a tab in the lower left corner of the screen:

if you want to show logs in error
you can write
Log.e("Your Tag for identy", "Your String ");
if you want to show logs in info
you can write
Log.i("Your Tag for identy", "Your String ");

Related

Android Studio 3.0 Logcat continuously showing messages and does not stop

When I start debugging my App in Android Studio 3.0 and open the Logcat, it displays so many messages and warnings, but the problem is, it never stops, even when the debugging is completed and the App is terminated, it still prints so many messages continuously.
Can anybody help???
Since you are not debugging or running any application on the device you have connected. Hence, the logcat is displaying the logs generated by all the apps in the device as well as the system logs. Which makes the log cat window go nuts.
What you can do is.
On the top right corner of the log cat window, there is a filter config combo box. Select or create a filter of your own to filter out relevant logs.
The filter could be created using any details, it could be a process ID (PID), the tag name e.g I/ActivityManager: where I stands for Info, V for Verbose, D for Debug and so on.
You can add filter in your Logcat, and mention all the classes whose logs you want to discard.
In the field LogTag (shown in the pic) define a regex like this ^(?!Class A | Class B|#). Here Class A and Class B are the classes whose logs you don't want to see.That's it.
If you see no debuggable application then change it to your app name. If that wont help change the log level to error. Last thing you can try is to filter the results. Good luck!

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"

LogCat won't show my logs

I've searched for similar problems, but I didn't find anything useful - I'm working with eclipse, and I can't see my Logs.
The device is connected properly, the app runs and does what it's supposed to do, but I get no logs from it.
I get other logs messages from the device, not the ones that I print, e.g. Log.d("SMS", "hello"). On the other hand, if I use the statement System.out.println("hello"), I do see it, tagged as System.out.
I've tried to disconnect and reconnect the device, restart it, close and open eclipse, choose the device from Device window. It happens both with a 'real' device and an emulator. I've also tried to remove the filtering, but nothing helps - I still don't get the logs.
Okay, I've found the problem -
Apprently there are some illegal tags, and I've used one of them.
My app is spam SMS blocker, and I've used the tag SMS. If I change it to another tag (like SMSBlocker) it suddenly appears in the LogCat.
Check if your project is using proguard.
Basically proguard will remove all the debug logs and optimize your code while creating apk.
Try adding proguard.enabled=false in your project.properties
I think you should use TAG.
When you log from your android app, the first parameter is a TAG string. So if you set it up to a unique string (like your app name) then you can later filter by it in Eclipse.
Example : Log.e(TAG, "state error");

Eclipse Logcat goes crazy

I was using my Eclipse Logcat crazy normally until yesterday where it started displaying messages without stop.
My message that I am printing using log.d are no longer printed to the log.
Or maybe they are but because of the huge number of messages printed they are gone.
The messages do not stop printing to the log whether I am using the app or not.
The only way to stop it is to unplug my device from the PC otherwise the messages will keep flushing to the logcat
Any suggestions ?
Thanks in advance :)
It sounds like you have no filtering of the log output and therefor sees output for every app on the phone.
Create a logcat filter with your application package name.
In the logcat window, go to "Saved filters", create a new one (if Eclipse hasn´t autocreated one for you) by clicking plus-sign to add. Type a name of your filter and write your application package name under "by Application Name". Click "ok".
When you select it you only see logcat outputs for your specified app.

Eclipse - Limit logcat output to debug relevent logs

I reinstalled my Eclipse and now my Logcat logs everything my phone sends like battery status or mail sending status.
Is there a way to tell Eclipse/Logcat to just log what is really coming from my app which I'm debugging?
Filter the Logcat with "app:com.yourdomain.appname". I've also added a filter that gets rid of more items using a not operator:
tag:^((?!CoreMetrics|InputEventConsistency|memalloc|Adreno200-EGLSUB|Resources|global|TaggingRequest|Facade[B|U]|dalvik|skia|KeyCharacterMap|BackStackEntry|FragmentManager|ServiceRunnable|ServiceLocator|BaseHttpRequest|szipinf|APACHE).)*$
You'll need to edit the list specifically for the items you're seeing, but it's a great way to reduce the static.
You can log using a Tag and then filter in logcat using the same tag. You can add a filter by clicking on the + in the left hand side list. You can recognize it because the first tag will be "All Messages". Clicking through the list enables the filters.
Log.i("MyTag", "some log message");
Also in newer versions of the ADT eclipse plugin, I blieve that it auto-adds your app to the filter list.
In Your LogCat click button(Second Last) to left of the "downarrow button" the click "plus" sign write filter name and in application write the package name of the app you wanna get filtered logs for examole com.yourapp press ok and viola !!!
You can enter search filters in the search box at the top, or you can make those filters permanent via the filters panel visible on left side of eclipse logcat window. If it is not visible then note the button on the top right of logcat that displays/hides it.
"Is there a way to tell Eclipse/Logcat to just log what is really coming from my app which I'm debugging?"
Not exactly. You should be able to do that with the filter "app:com.yourapp". This filters based on the 'application' column/data of the logcat output, which would be perfect but Android devices are rather eratic at adding that information to debug output. Actually, the Nexus 4 is the first phone I've used that reliably specifies that data field in its logcat output.
But even if 'application' column doesn't work for you, you should be able to find other filters that reduce the visible log statements to a manageable level.

Categories

Resources