How do I get the logfile from an Android device?
Asked Answered
A

14

145

I would like to pull the log file from a device to my PC. How can I do that?

Azide answered 21/5, 2010 at 13:4 Comment(0)
N
125

Logcollector is a good option but you need to install it first.

When I want to get the logfile to send by mail, I usually do the following:

Nava answered 22/5, 2010 at 13:18 Comment(4)
This commandline doesn't work for me, I get this output. logcat read: Invalid argumentTangleberry
note that logcollector doesn't work for versions of android above 4.1 because applications are now only allowed to read their own log entries. (groups.google.com/forum/#!topic/android-log-collector/…)Ornamented
Isn't there any simple method, some way to simply pull the logfile from the device once it is connected via USB as an external storage device, without the need to install adb or anything like that?Palsgrave
Make sure you're in the right directory to run adb. Usually in C:\Users\[your username]\AppData\Local\Android\Sdk\platform-tools\ Naldo
G
35

I hope this code will help someone. It took me 2 days to figure out how to log from device, and then filter it:

public File extractLogToFileAndWeb(){
        //set a file
        Date datum = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY);
        String fullName = df.format(datum)+"appLog.log";
        File file = new File (Environment.getExternalStorageDirectory(), fullName);

        //clears a file
        if(file.exists()){
            file.delete();
        }


        //write log to file
        int pid = android.os.Process.myPid();
        try {
            String command = String.format("logcat -d -v threadtime *:*");        
            Process process = Runtime.getRuntime().exec(command);

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder result = new StringBuilder();
            String currentLine = null;

            while ((currentLine = reader.readLine()) != null) {
                   if (currentLine != null && currentLine.contains(String.valueOf(pid))) {
                       result.append(currentLine);
                       result.append("\n");
                    }
            }

            FileWriter out = new FileWriter(file);
            out.write(result.toString());
            out.close();

            //Runtime.getRuntime().exec("logcat -d -v time -f "+file.getAbsolutePath());
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }


        //clear the log
        try {
            Runtime.getRuntime().exec("logcat -c");
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }

        return file;
    }

as pointed by @mehdok

add the permission to the manifest for reading logs

<uses-permission android:name="android.permission.READ_LOGS" />
Guerrilla answered 4/3, 2014 at 14:10 Comment(1)
this works fine, but to works on every device you need <uses-permission android:name="android.permission.READ_LOGS" />Ity
A
31

I would use something of this sort :

$adb logcat -d > logcat.txt

The -d option dumps the entire circular buffer into the text file and if you are looking for a particular action/intent try

$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt

Hope this helps :)

Antimagnetic answered 30/9, 2012 at 3:18 Comment(0)
C
15

For those not interested in USB debugging or using adb there is an easier solution. In Android 6 (Not sure about prior version) there is an option under developer tools: Take Bug Report

Clicking this option will prepare a bug report and prompt you to save it to drive or have it sent in email.

I found this to be the easiest way to get logs. I don't like to turn on USB debugging.

Chaste answered 26/5, 2016 at 12:28 Comment(2)
Exactly what I was looking for!Modernity
Where do i find it exactly?Detest
M
11

EDIT:

The internal log is a circular buffer in memory. There are actually a few such circular buffers for each of: radio, events, main. The default is main.

To obtain a copy of a buffer, one technique involves executing a command on the device and obtaining the output as a string variable.

SendLog is an open source App which does just this: http://www.l6n.org/android/sendlog.shtml

The key is to run logcat on the device in the embedded OS. It's not as hard as it sounds, just check out the open source app in the link.

Millymilman answered 21/5, 2010 at 13:28 Comment(4)
This dumps the output to the screen, I need it in a file.Azide
You can send logs with aLogCat. Or simply copy-paste from DDMS :PWeeks
@Weeks I concede my answer was slightly over-engineered if that's all he needs to do =)Millymilman
@BradHein You said SendLog was open source but I can't seem to find the source code. Any idea where it is?Bournemouth
T
8

Often I get the error "logcat read: Invalid argument". I had to clear the log, before reading from the log.

I do like this:

prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
Tangleberry answered 24/8, 2011 at 9:37 Comment(1)
Thanks! I was having that error for a long time, that logcat -c fixed it immediately!Overton
J
6

I know it's an old question, but I believe still valid even in 2018.

There is an option to Take a bug report hidden in Developer options in every android device.

NOTE: This would dump whole system log

How to enable developer options? see: https://developer.android.com/studio/debug/dev-options

What works for me:

  1. Restart your device (in order to create minimum garbage logs for developer to analyze)
  2. Reproduce your bug
  3. Go to Settings -> Developer options -> Take a bug report
  4. Wait for Android system to collect the logs (watch the progressbar in notification)
  5. Once it completes, tap the notification to share it (you can use gmail or whetever else)

how to read this? open bugreport-1960-01-01-hh-mm-ss.txt

you probably want to look for something like this:

------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of crash
06-13 14:37:36.542 19294 19294 E AndroidRuntime: FATAL EXCEPTION: main

or:

------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of main
Jonathanjonathon answered 21/6, 2018 at 9:19 Comment(0)
J
4

A simple way is to make your own log collector methods or even just an existing log collector app from the market.

For my apps I made a report functionality which sends the logs to my email (or even to another place - once you get the log you can do whether you want with it).

Here is a simple example about how to get the log file from a device:

Jello answered 21/5, 2010 at 14:2 Comment(0)
E
3

Simple just run the following command to get the output to your terminal:

adb shell logcat
Evapotranspiration answered 27/4, 2016 at 23:57 Comment(0)
C
3

Two steps:

  • Generate the log
  • Load Gmail to send the log

.

  1. Generate the log

    File generateLog() {
        File logFolder = new File(Environment.getExternalStorageDirectory(), "MyFolder");
        if (!logFolder.exists()) {
            logFolder.mkdir();
        }
        String filename = "myapp_log_" + new Date().getTime() + ".log";
    
        File logFile = new File(logFolder, filename);
    
        try {
            String[] cmd = new String[] { "logcat", "-f", logFile.getAbsolutePath(), "-v", "time", "ActivityManager:W", "myapp:D" };
            Runtime.getRuntime().exec(cmd);
            Toaster.shortDebug("Log generated to: " + filename);
            return logFile;
        }
        catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    
        return null;
    }
    
  2. Load Gmail to send the log

    File logFile = generateLog();
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(logFile));
    intent.setType("multipart/");
    startActivity(intent);
    

References for #1

~~

For #2 - there are many different answers out there for how to load the log file to view and send. Finally, the solution here actually worked to both:

  • load Gmail as an option
  • attaches the file successfully

Big thanks to https://mcmap.net/q/161074/-action_view-intent-for-a-file-with-unknown-mimetype for the correctly working answer

Carisacarissa answered 14/12, 2016 at 19:57 Comment(0)
P
2

Thanks to user1354692 I could made it more easy, with only one line! the one he has commented:

try {
    File file = new File(Environment.getExternalStorageDirectory(), String.valueOf(System.currentTimeMillis()));
    Runtime.getRuntime().exec("logcat -d -v time -f " + file.getAbsolutePath());}catch (IOException e){}
Pointillism answered 7/3, 2014 at 13:56 Comment(1)
where is the best place to put this code? in OnCreate?Plato
S
2

I have created a small library (.aar) to retrieve the logs by email. You can use it with Gmail accounts. It is pretty simple but works. You can get a copy from here

The site is in Spanish, but there is a PDF with an english version of the product description.

I hope it can help.

Segovia answered 28/7, 2015 at 8:58 Comment(0)
T
1

I would use something like:

$ adb logcat --pid=$(adb shell pidof com.example.yourpackage)

which you can then redirect to a file

$ adb logcat --pid=$(adb shell pidof com.example.yourpackage) > log.txt

or if you also want to see it at stdout as well:

$ adb logcat --pid=$(adb shell pidof com.example.yourpackage) | tee log.txt
Tideway answered 21/2, 2023 at 11:15 Comment(0)
A
0

First make sure adb command is executable by setting PATH to android sdk platform-tools:

export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH

then run:

adb shell logcat > log.txt

OR first move to adb platform-tools:

cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools 

then run

./adb shell logcat > log.txt
Annatto answered 9/5, 2015 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.