How do I write outputs to the Log in Android?
Look into android.util.Log
. It lets you write to the log with various log levels, and you can specify different tags to group the output.
For example
Log.w("myApp", "no network");
will output a warning with the tag myApp and the message no network.
Log output to file and system log at the same time
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
andSystem.err
output, which is written to the console in desktop systems, writes to the log on Android. These two can be redirected into anyPrintStream
of your choice, which would give you all Java console output. You can subclassPrintStream
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 respectiveandroid.util.Log
method and additionally log the data to a file. If you call your classLog
(but with a different package name, e.g.org.example.Log
), then all you need to do is replace imports ofandroid.util.Log
with an import of your class, and anyLog
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 byadb shell
ing 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).
Output an object to the Logcat console
You cannot print an object to the console in Java as you would in javascript.
You have three solutions.
1) Use debugger. Place a breakpoint and debug in android studio. Then you can inspect the full object in scope.
2) Serialize your object (for example to JSON) and print the result to console.
3) Override toString
method of your object to give you all the information you want and call Log.d("myTag", yourObj.toString())
I highly recommend first method. I used to avoid Debugger but learning how to use the debugger was the best thing I did. It increases your efficiency and makes debugging super easy
How to output full global reference log for android app with XAMARIN
I found the solution here:
https://developer.xamarin.com/releases/android/xamarin.android_5/xamarin.android_5.1/#GREF_Logging_Changes
(But I'm using Samsung S6, so run-as command doesn't work :()
Why Log.d and Log.v do not print any output?
This might help
Huawei, logcat not showing the log for my app?
Dial
*#*#2846579#*#*
and you will see a hidden menu. Go to the Project Menu > Background Setting > Log setting and define the log availability (log switch) and level (log level setting).
You might have to enable both AP and CP logs for debug and verbose logs to show.
PS : probably only for Huawei devices.
How to print to the console in Android Studio?
Run your application in debug mode by clicking on
in the upper menu of Android Studio.
In the bottom status bar, click 5: Debug
button, next to the 4: Run
button.
Now you should select the Logcat
console.
In search box, you can type the tag of your message, and your message should appear, like in the following picture (where the tag is CREATION
):
Check this article for more information.
Related Topics
Setresult Does Not Work When Back Button Pressed
How to Repeat a Method Every 10 Minutes After a Button Press and End It on Another Button Press
Android: How to Make an Activity Return Results to the Activity Which Calls It
Error:Cause: Unable to Find Valid Certification Path to Requested Target
Not Able Disable Home Button on Specific Android Devices
How to Hide Underbar in Edittext
How to Add Multiple Widgets in the Same App
Webview Showing Err_Cleartext_Not_Permitted Although Site Is Https
Android Apps, Communicating with a Device Plugged in the Usb Port
Difference Between Ontouch and Onclick Android
Extract Code Country from Phone Number [Libphonenumber]
How to Get Child of Child Value from Firebase in Android
Could Not Find Method Android() for Arguments
Datepicker Shows Wrong Value of Month
Differencebetween @Id and @+Id
Sign APK Without Putting Keystore Info in Build.Gradle
Upload a File Through an Http Form, via Multipartentitybuilder, with a Progress Bar
Android N Requires the Ide to Be Running with Java 1.8 or Later