I was wondering how do i take complete logs from android device (From the point of My application initialize to any crash or till force close of my application).
Reason i am posting here is my application is crashed some point,but when i take logs using DDMS/Logcat my crash details are over written with new logs.
How do i get my crashed reason logs..
Specially looking to capture Native Code Crash.
i.e I/DEBUG (21835): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000004...
Will this adb logcat > crash.txt ensures me that write to file will happen forever?
I tried this Works Real Well,Not sure How much battery it will consume..If your Application is in Testing Stage You can use this..Before Release you got to Remove this code and Publish..
private void writeADBLogs(){
BufferedWriter bufferedWriter = null;
try {
final File file = new File(sdcardPath);
bufferedWriter = new BufferedWriter(new FileWriter(file,true));
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()));
String oneLine;
while ((oneLine= bufferedReader.readLine()) != null) {
bufferedWriter.write(oneLine);
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
The Android Log will be save only when Your application has Debugging = true in manifest (i.e when you are in Debug mode).
See Documentation at Turn off logging and debugging
So in that case if you want the log then you can implement Thread.setDefaultUncaughtExceptionHandler (Thread.UncaughtExceptionHandler handler)
This will be called always when your application is force closed due to Exception.
What you do is save the StackTrace in a file in append mode.
You can also use this in Debug Mode.
LogCat is a Queue so there are changes that you will miss your log (old Log will be automatically discarded).
I suggest you to implement setDefaultUncaughtExceptionHandler so you never miss any exception log. Also take care to delete the file after use or else your file will became very big in size by time.
install aLogCat app, then if your app crashes, launch it. Use filter (from menu) to narrow data to related to your application and eventually save it or send from device via mail to you. But it won't do much magic - if logs are phisically gone from device it won't send you more you can have with DDMS. It's however quite useful if your users report crashes.
Assuming you have access to the device:
Hook the device up to the computer.
Run DDMS, select the device, and go to the file explorer tab.
Find the file referenced in LogCat, and click on the Pull from Device button near the top.
You should use unique Logtags for your messages so you can Filter the cat for that. E.g. getPackageName() is a nice candidate and it's returning a string.
In Eclipse you can click on the green + to add another filer for LogCats View. In the window you repeat your tag in the field "by log tag".
Also it seems there's automatically a new Tab called "session filter" when I start my app from Eclipse - providing relevant Logs including but not only the ones written by the programmer.
You can also grab the log directly from the console using Android Developer Bridge. adb logcat is the command you're looking for and according to man there are also the same filter settings applicable. Though I couldn't figure out the syntax :)
You can use acra with your application.It helps you to get the crash reports in your gdoc account you can also configure it to get an email for crash report.Once your app goes live then it also helps you to get the crash report.
http://code.google.com/p/acra/
Related
I would like to be able to save and print logcat messages when my device is disconnected from the computer, and then when I plug it in, to have all the messages be printed to logcat.
Currently, when I log, this does not happen. When I plug my phone in, I can hook the device to logcat and lots of logs from the day get printed into logcat. They are listed under my app's process. My logs are not there though. How can I get them there?
Here is how I log:
public static final String TAG = "Service";
Log.v(TAG, "log");
Edit: Yesterday, I disconnected from logcat with a process running. Then today, I reconnected to logcat. Since I still had a filter for my logs on, logs from the last few hours printed. I then turned the filter on and off and all of them disappeared.
You need to build a class that will be a wrapper for the android.util.Log class, that redirects the output to both file and console. Check this answer, it is a bit outdated but you can still follow the example and build such a class. This way you get the logs written to a file that you can check whenever you want.
You may save your logs to sharedpreferences and get it value after connection
I am trying to diagnose an issue in an app I have written. The issue is a sporadic one, and occurs only under real-world conditions: in the field, away from my PC, and when I’m in the middle of something else, with no resources to spare for immediate debugging. Therefore, my best bet is collecting and analyzing log data.
Unfortunately, by the time I realize the issue has struck again and get around to debugging it, any log data has already rotated out of the Android log as I frequently have other chatty apps running at the same time. Increasing the size of the log buffer has not helped (either Android does not honor it or other apps are still too chatty) so I have abandoned this route.
For this reason, I am now considering having my app log to a separate text file in addition to the regular log.
Now I could easily double every call like
Log.i(TAG, "something happened");
adding another call that writes the same thing to the log file—but that does not seem very elegant to me.
Another option would be to replace all calls to Log with a wrapper that writes the event both to the Android log and the log file.
Question: Does the Android API provide a built-in mechanism for this, i.e. telling Log to write its data to the default log and a text file at the same time? Or do I need to code this by myself?
Edit:
Assumptions:
I know where in my code I need to generate log output (which can happen anywhere, which may or may not involve an exception) and what I want to be written to the log.
Getting log data from the device to my PC is also not a concern (one-man show, I just plug my phone into my PC and transfer the log file).
If you know the current Android API has no built-in mechanism to achieve what I want, then ”no, Android does not support this” is a perfectly acceptable answer. In that case the solution is clear—I would fall back to the wrapper function. I am specifically not looking for a different approach to the problem.
After doing some more research, it seems the Android API does not provide a standard way to do this. There are two possible workarounds:
Mirror output at the source
System.out and System.err output, which is written to the console in desktop systems, writes to the log on Android. These two can be redirected into any PrintStream of your choice, which would give you all Java console output. You can subclass PrintStream to duplicate its input, feeding it into the default stream as well as into a file of your choice.
Create a class which exposes the same methods as android.util.Log. In each method, call through to the respective android.util.Log method and additionally log the data to a file. If you call your class Log (but with a different package name, e.g. org.example.Log), then all you need to do is replace imports of android.util.Log with an import of your class, and any Log method calls will go to your class.
Caveats: This will only give you data explicitly logged by your code (i.e. for which you have the source files), as well as anything that goes to System.out or System.err. It will not include log output from JAR libraries (if you cannot modify their source code), nor any output generated by the system (such as stack traces from default exception handlers) or by other processes (some of which may be system processes and report conditions related to your process).
Read the logs from the command line
This article explains how to read the logs from within Android. In a nutshell:
Android includes a command line utility called logcat on the device, which will give you a continuous feed of log messages until stopped. (Try it by adb shelling into your device and running it. It has a bunch of command-line options to control its behavior. Not sure if it is present on all distributions, though.)
Launch this command via Runtime.getRuntime().exec("logcat"), then obtain the input stream of the process returned. This will give you an input stream of log messages.
According to the article, your app needs the android.permission.READ_LOGS permission to read logs.
I have read statements that certain versions of Android (4.2 was mentioned) do not allow this permission to be granted to non-system apps, though. According to my own tests, behavior without this permissions differ: Anbox will return the full logcat, while LineageOS (tested on 15.1) will only show log entries from the app which called it (including previous instances, presumably everything associated with the same Linux user). This can be a limitation or a welcome filter feature. YMMV.
logcat conveniently has a command line option, -f, to specify an output file. I tried
Runtime.getRuntime().exec("logcat -f " + absolutePathToLogFile);
and logcat keeps logging as long as the app’s process runs. Killing the app (by clicking the X in the title bar on Anbox) apparently also terminated the child process.
Now you can either run this code when your app starts up, or you can turn this functionality into a separate app which starts on boot and continuously collects logs for all apps.
Caveats: This may fill up your storage space quickly if you have some chatty apps running (which is why entries rotate out of the logcat so quickly in the first place). It is recommended to make log mirroring configurable (e.g. via Preferences) and/or ensure old files are deleted regularly. Also, if you keep the logcat process running until your app terminates, you will not be able to access the file over MTP as there is no easy way to run the media scanner (if you scan the file while it is still written to, it will appear truncated over MTP until another media scan runs).
You have not specified if some exception are thrown but you don't handle.
In case, take a look at this answer:
Android Handling Unhandled Exception
If you must look at a bunch of variables and objects, I'd suggest two choices:
Write a copy of your logs on a file. When your problem occurs, just ask the user to send the file to you. This is ideal during tests with self-aware users.
Obtain statistics about usage, like commercial software do. Just log user operations and send the data to your server (you would need one for this). This is the most transparent way to do remote logging.
In the case of writing log to a file, you can read and write what you want in internal memory (inside the app's sandbox) or external memory (in this case, write permission is required and explicit permission must have been granted at runtime if you are targeting Android 6 and above).
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!
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 ");
On Android I am using the android.util.Log to log within my application and during development I am using the adb logcat or Eclipse to see the logs - I use it even more then debugging...
On device I can save the logfile from my code or use some application form Android Market to save the logs - e.g. aLogCat.
Now can I do the same on the iPhone? I can use the NSLog(#"message");, but can I easily save the log file from my application and access it? Are there any ways for that?
Regards,
STeN
This is from NSFoundation reference
NSLog:
Simply calls NSLogv, passing it a variable number of arguments.
NSLogv:
Logs an error message to the Apple System Log facility (see man 3 asl). If the STDERR_FILENO file descriptor has been redirected away from the default or is going to a tty, it will also be written there. If you want to direct output elsewhere, you need to use a custom logging facility.
Thus, it is only a matter of redirecting the file-descriptor "stderr" (2) to a custom file, and you will get everything that you print using NSLog in that file.
This seems to be exactly what you want.
Note that if you want to get logs on console when you are connected to the debugger, you can wrap your code around this to avoid redirection in this case:
if (!isatty(STDERR_FILENO)) { // Not connected to any terminal
// your redirection code
}
You can access the console log from Organizer->Device->Your device->console.
If that is not powerful enough, consider using utilities like NSLogger.
The previous answers are good; also see this if you're inclined to making system calls.