Why and when should I use the android Logging? Should it be used only for debugging purposes? It seems to me that if kept in a production application, it would slow it down considerably.
One key part of my question, is WHEN it should be used...in what cases should I need it? Are there any 'best practices' for using this?
This may be a very stupid or amateur question, but I've never understood the need or felt compelled to use it. I may be missing something.
http://developer.android.com/reference/android/util/Log.html
Also - I already know that logging for errors/verbose/info/etc are different, and some are discarded when not debugging. Please don't reiterate the information that's in the class overview without giving me an explanation why. Thanks!
I agree with you, I never really used it either. I much prefer debugging over log reading (and unit-testing over debugging), when trying to figure out what's happening and getting an error.
Some people argue it can be useful to log "additional details" when your application crashes and get an exception, but I usually reply to them that the exception itself should hold that additional details when necessary, and expose them in its getMessage. Thus, the default stack trace that's included in the LogCat is more than enough to see what's going on.
But anyway, it's always nice to have the possibility to log something, even though I haven't found it really useful so far, you never know when it might help :)
Regarding my comment, see Preparing for Release. (Showing logging should be removed before release, hence not being used in production).
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.
I used logging the other day, I fired off another thread to do some work but I needed to check some data being produced in the thread, without logging or displaying Toast's, how could I get the values? I'm tried debugging/stepping through code in Eclipse before and just run into several problems.
With logging, I can log each value, view the logcat and know exactly what my code is doing.
You usually debug only when you know there is something wrong. (And when you know, you might write additional test cases.)
With logging (at the INFO level, for example), you can add some additional information to trace the data in the app. This allows you to find out that there is something wrong.
Thus, it adds a higher-level overview.
Additionally, it can be easily disabled, does not slow the app down significantly (if done right), and might thus offer another avenue of approach to see if everything works correctly, with few disadvantages and some advantages. (See also Logging vs. Debugging, especially the links in #smonff's answer.)
Related
So I have an android application that has a myriad of activities (intents?) which all do their own thing. We were wanting to create a sort of debug log though, one where you can see what the user has been through and error that occurs. You may have had to send one before if you've run into a bug with a program you've used.
My initial thoughts are to just create a class where I can send information/data to and it just writes it onto a text file. It would need to be accessible across all the activities so that I can easily write to it and re-use it.
I do wonder whether that's a good way to go through, noted that it doesn't really save any actual errors but only data I tell it to. And I'm not sure if its a great idea to be constantly opening->writing->closing a file for a debug log.
Is there a smarter way? Or a common pattern that would be good to use?
Thanks so much!
Sentry's Android SDK, will automatically report errors and exceptions in your application.
The Sentry SDK catches the exception right before the crash and builds a crash report that will persist to the disk. The SDK will try to send the report right after the crash, but since the environment may be unstable at the crash time, the report is guaranteed to send once the application is started again.
You can see full documentation here
The question may sound stupid, but I'm explaining:
I'm not 100% sure how logcat Works, but I think it's safe to guess that it reads internal produced messages from the app it's monitoring.
I think it would be possible to also get to read these messages in the way that logcat "would be added" to the app and it would be possible for example to put a thread that would search patterns in that "logcat added to the app" and produce some action, same way a developer searchs for certain patterns to make the app work. This way it would be possible to get a detailed log of what have been the actions of an user that may cause an error in the app for example, withouth the need of having to plug the pone in the computer and trying to reproduce the error.
But maybe logcat is something extremely complex on its own, and what I'm saying is total nonsense for any practical purpose.
Is it feasible what I'm mentioning?
logcat is reading stack traces. You can place your project inside a try and in the catch, redirect the stack trace to a database or whatever. In our project, we have Acralyzer added as a dependency and it sends all errors to our server. Note that we aren't using that horrid CouchDB-we are custom-parsing the json ourselves. We do this on deployed apps so we have a stack trace of exactly what line in the program failed. This is almost always enough information. "null reference" is pretty explanatory for example.
Please note, unlike many other questions having the subject title "application has stopped unexpectedly", I am not asking for troubleshooting a particular problem.
Rather, I am asking for an outline of the best strategy for an Android/Eclipse/Java rookie to tackle this formidable task of digesting huge amounts of information in order to develop (and debug!) a simple Android application.
In my case, I took the sample skeleton app from the SDK, modified it slightly and what did I get the moment I try to run it?
The application
(process.com.example.android.skeletonapp)
has stopped unexpectedly. Please try
again.
OK, so I know that I have to look LogCat. It's full of timestamped lines staring at me... What do I do now? What do I need to look for?
Is there a way to single-step the program, to find the statement that makes the app crash? (I thought Java programs never crash, but apparently I was mistaken)
How do I place a breakpoint?
Can you recommend an Android debug tutorial online, other than this one?
I'm an Eclipse/Android beginner as well, but hopefully my simple debugging process can help...
You set breakpoints in Eclipse by right-clicking next to the line you want to break at and selecting "Toggle Breakpoint". From there you'll want to select "Debug" rather than the standard "Run", which will allow you to step through and so on. Use the filters provided by LogCat (referenced in your tutorial) so you can target the messages you want rather than wading through all the output. That will (hopefully) go a long way in helping you make sense of your errors.
As for other good tutorials, I was searching around for a few myself, but didn't manage to find any gems yet.
Filter your log to just Error and look for FATAL EXCEPTION
If you use the Logcat display inside the 'debug' perspective in Eclipse the lines are colour-coded. It's pretty easy to find what made your app crash because it's usually in red.
The Java (or Dalvik) virtual machine should never crash, but if your program throws an exception and does not catch it the VM will terminate your program, which is the 'crash' you are seeing.
Check whether your app has the needed permissions.I was also getting the same error and I checked the logcat debug log which showed this:
04-15 13:38:25.387: E/AndroidRuntime(694): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:555-555-5555 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{44068640 694:rahulserver.test/10055} (pid=694, uid=10055) requires android.permission.CALL_PHONE
I then gave the needed permission in my android-manifest which worked for me.
From the Home screen, press the Menu key.
List item
Touch Settings.
Touch Applications.
Touch Manage Applications.
Touch All.
Select the application that is having issues.
Touch Clear data and Clear cache if they are available. This resets the app as if it was new, and may delete personal data stored in the app.
Try the following:
Create a HelloWorld application.
Add a Log statement to the end of onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("HelloWorldActivity.onCreate()", "setContentView() completed");
}
Place a breakpoint on the Log statement.
Run the app in the emulator and note it works and step to see the Logged entry in the LogCat window of Eclipse.
Change the HelloWorldActivity to extend from ListActivity instead of Activity.
public class HelloWorldActivity extends ListActivity {
Run the app in the emulator again and note it fails to reach the Log statement.
My question is NOT why this fails. My question is, how would you go about debugging this failure? All I see in the Eclipse Debug pane is a RuntimeException. I see LogCat has a bunch of messages, but it's huge and I've searched it but can't find anything to indicate what's wrong or where in my code the exception happened. I can't find a way to display the message inside the RuntimeException or a stack trace to know which line of code initiated the exception.
I assume there must be better ways to use the tools to find errors, but I'm new and can't seem to figure out a better way to debug besides wrapping everything I code in a try/catch. I would have expected to find a message in LogCat generated by the throwing of the exception. I would have expected the Debug window to allow you to inspect the exception's contents. I'm not saying that such techniques don't exist, I'm saying I'm having trouble figuring out as a beginner how to debug and asking what techniques do exist and how do I use them?
So, simply put:
How would you find this error if you didn't already know what was causing it?
What techniques would you use to find out the root cause?
How would you go about inspecting the Exception's details?
Generally, how do you find problems in your Android code using Eclipse?
Multiple suggestions and discussion are welcomed. :)
I would have included my LogCat contents, but it's so large that's not reasonable. You should be able to easily reproduce this yourself, so I left it out. It is possible something is in LogCat to help me, but because it's so large with even running a small program, I would need a hint as to what to search for and how to interpret it when hitting an exception thrown from an API call. I see other posts that state something should be in LogCat, which while might be true, I'm not finding anything myself. If you think something should be in LogCat, please run the test yourself and copy the lines into your response that I should be finding.
Thanks.
========
Summary techniques list so far is as follows:
Invasive Techniques:
1. Place a Toast in code locations where you want to see you you've executed.
2. Place try/catch around code where you think there's a possibility of an Exception being thrown.
3. Comment out code and recompile and retest.
Non-Invasive Techniques:
1. Use the debugger. Breakpoints, variable inspection...
2. Monkey stress tester.
3. Download Android source library.
4. Use LogCat filters to see if a "Caused By" is listed.
Unclear if Available:
1. Debug version of Android library that has additional logging, assertions or other additional help.
2. Ability to inspect an Exception in Eclipse through the Debug pane or other techniques.
3. A way to define a more global try/catch exception handler.
4. Ability to debug through the Android library source code.
Not Available:
1. A non-invasive way to view the contents of an Exception or where the Exception happened.
hey,
Ineresting question. Well, first tip, you can filter what logcat tells you. For instance, you make it just show you errors by clickin in the red (e).
It also tells you where the error happened if you run your app in debug mode. It can either point you directly to your code or to android sdk. Knowing what android package caused the error is a big help.
These two just pop into my mind. hope it helps!
I was running into the same issue and found the following by Steve H. that helped out:
What happens is that when the debugger
is attached, the exception logs don't
get posted to LogCat until you
terminate the application from within
the Debug perspective. This happens
because the application doesn't
actually crash until the debugger
detaches. – Steve H Mar 31 at 15:47
------ yup, that did it. Now I see the same exception. AFTER I let the
program run through it's full crash
and exit process. It should display
that info when it halts my program and
brings up the IDE debugger screens.
Not leave me wondering and wasting my
time with more clicking around.
Eclipse has a long way to go it seems
to compete with the likes of Visual
Studio. Let's hope my patience for it
outlasts my project. Thanks for the
feedback. :) – Sebastian Dwornik Mar
31 at 17:35
Link to Question: What's wrong with debugging in Eclipse on Android?
Generally if anything throws an exception then you should probably be catering for that situation anyway, however putting try/catch blocks is a decent way of finding the specific problem.
I've found that if you don't put something in the catch block then you can't evaluate the exception in the watch variables window in eclipse. So i always put a Log call in and set a break point on that line.
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}catch(Exception exception)
{
// put break point on line below so you can evaluate exception in debug mode.
Log.e(TAG, "Set content exception "+ exception.getMessage());
// note some exceptions return null on getMessage();
}
Log.d("HelloWorldActivity.onCreate()", "setContentView() completed");
}
So if your stack trace is huge then this will help. Other developers have also found that getting the source code for the sdk means you can view where the error is thrown in the main sdk code. I've not done this though.
Exceptions not caught with try/catch are errors and break the normal flow of the program.
Running in debug mode is just clicking in the bug button. I don't know if there are any "special" debug libraries. But when working with android, all "libraries" are open source so you can pretty much view anything.
The good thing about debug mode is that when an error occurs, your app is frozen right in the limbo when the error occurs. You can set set breakpoints, change your code on the fly while your program is running, which is great (Well, you can't make drastic changes like changing a method name).
The way you treat bugs and errors in android, however, can be a bit different from .NET, since the model in each one is different.
When programming to windows, apps work like small islands. You have a much direct control over the code flow (ie: you can call a modal dialog to freeze codeflow while a user inputs some data) and you can make a totally functional program using just one thread. In android almost everything runs in it's own sync. And your app must be prepared to handle stuff like receiving a phone call in the middle of execution. So, you can apply this model to debugging also: Errors (that happen due to unforsen circumstances) tend to propagate much more than in other development ambient. The way those errors are handled is different too: this is apparent when you realize that your app still runs even after throwing an exception.
Some more useful tips:
You have a very powerful tool called Monkey, a stress tool that generates "pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-
level events".
LogCat indicates you the "cause" of the error. The line usually starts with Caused By. If you're interested in the cause rather than the consequence, you can further filter your error reports looking for "Caused by".
Last but not least, I find the old method of commenting lines and see what happens very useful to figure things out.
hope it helps
I've read the lame documentation, and checked other answers. I'd like my Android app to print some debug statements in the logcat window of Eclispe. If I use the isLoggable method on the various types of debug levels on the Log class, I find that WARN and INFO are returning true.
Log.w, and Log.i do not produce any output. Does anyone know which gotchas I've missed?
And just to vent, why should this be hard? I've published apps for iphone and bberry and while appreciate the use of java, the platform is reeking of too many "genuiuses" being involved. I suppose Activities and Intents are very flexible, but why? I just want to put up some screens, take some input and show some results. The bberry pushscreen and popscreen is a lot less pretentious.
Thanks,
Gerry
The problem with debugging with Android in Eclipse is that from Eclipse's point of view, you're debugging the emulator and not your specific app. The emulator isn't crashing, so there aren't any logs to show. What you need to use is LogCat, Android's debugging plug-in. See this answer for details on how to bring that up.
It is not clear to me what the problem is. I use "Log.d(TAG, "special message");" all the time in Eclipse in Android code running in the emulator. Since you say "Log.w" gives no output, I assume you already know about the need to import android.util.Log. Otherwise you would not have got even that far.
The only other thing I can think of is for you to check your Eclipse Preferences under Window>Preferences>Android>DDMS (DDMS is needed for Logcat). Make sure the timeout is reasonable (mine defaulted to 5000mS). Make sure the base local debugger port is open, too.