How to use the command 'adb logcat' (with examples)

How to use the command 'adb logcat' (with examples)

The adb logcat command is a powerful tool for debugging and analyzing Android applications. It allows developers to view system logs, filter logs based on tags and priority levels, and even customize the output format. This article will provide examples of different use cases of the adb logcat command, demonstrating its versatility and usefulness in Android development.

Use case 1: Display system logs

Code:

adb logcat

Motivation: The ability to view system logs is crucial for debugging and understanding the behavior of an Android application. By using the adb logcat command without any filters, it displays a continuous stream of all system logs on the connected device or emulator.

Explanation: This command simply runs adb logcat without any additional arguments, resulting in the display of system logs.

Example output:

07-21 15:12:26.498  1659  1659 I art     : Starting a blocking GC Explicit
07-21 15:12:26.548  1659  1659 I art     : Explicit concurrent mark sweep GC freed 8950(742KB) AllocSpace objects, 3(220KB) LOS objects, 19% free, 11MB/14MB, paused 242us total 49.015ms
07-21 15:12:26.558  1659  1659 I art     : Forcing collection of SoftReferences for 98KB allocation
...

Use case 2: Display lines that match a regular expression

Code:

adb logcat -e regular_expression

Motivation: When dealing with a large volume of logs, it can be overwhelming to search manually for specific lines or patterns. The -e flag allows the user to provide a regular expression that filters the log output, displaying only the lines that match the provided pattern.

Explanation: By using the -e flag followed by a regular expression, the adb logcat command filters the log output to include only the lines that match the provided regular expression.

Example output:

07-21 15:15:43.972  4568  4568 W System.err: java.lang.NullPointerException
07-21 15:15:43.973  4568  4568 W System.err:     at com.example.myapp.MainActivity.onCreate(MainActivity.java:32)
07-21 15:15:43.973  4568  4568 W System.err:     at android.app.Activity.performCreate(Activity.java:7956)
07-21 15:15:43.973  4568  4568 W System.err:     at android.app.Activity.performCreate(Activity.java:7945)
...

Use case 3: Display logs for a tag in a specific mode, filtering other tags

Code:

adb logcat tag:mode *:S

Motivation: In a complex Android application, different components may generate logs with different tags. Filtering logs for a specific tag while silencing others can help narrow down the log output and focus on a particular component.

Explanation: By providing a tag and a mode (Verbose, Debug, Info, Warning, Error, or Fatal), followed by a colon (:), the adb logcat command filters the log output to include only the logs associated with the specified tag and mode. The *:S at the end silences logs from all other tags.

Example output:

07-21 15:18:02.841  4568  4568 D MyTag   : Debug log message
07-21 15:18:02.842  4568  4568 I MyTag   : Info log message
07-21 15:18:02.843  4568  4568 W MyTag   : Warning log message
07-21 15:18:02.844  4568  4568 E MyTag   : Error log message
07-21 15:18:02.845  4568  4568 F MyTag   : Fatal log message
...

Use case 4: Display logs for React Native applications in Verbose mode, silencing other tags

Code:

adb logcat ReactNative:V ReactNativeJS:V *:S

Motivation: When developing React Native applications, it can be helpful to filter logs specifically related to React Native modules and JavaScript code. By using the adb logcat command with specific tag filters, developers can easily debug React Native-specific issues.

Explanation: By providing the tags ReactNative:V and ReactNativeJS:V (Verbose mode) followed by *:S, the adb logcat command filters the log output to include only the logs associated with React Native modules and JavaScript code, silencing logs from other tags.

Example output:

07-21 15:21:37.635  4568  4568 V ReactNative: Initializing React XplatBridge.
07-21 15:21:37.901  4568  4568 V ReactNativeJS: Running application "MyApp" with appParams: { ... }
...

Use case 5: Display logs for all tags with priority level Warning and higher

Code:

adb logcat *:W

Motivation: When focusing on a specific issue, it can be helpful to view only the logs with a warning level or higher. This ensures that the log output is relevant to potential problems or errors.

Explanation: By using the * wildcard to include all tags and :W to specify the warning level, the adb logcat command filters the log output to include only logs with a warning priority or higher.

Example output:

07-21 15:24:13.714  4568  4568 W MyApp   : Warning log message
07-21 15:24:13.715  4568  4568 E MyApp   : Error log message
07-21 15:24:13.716  4568  4568 F MyApp   : Fatal log message
...

Use case 6: Display logs for a specific PID

Code:

adb logcat --pid=pid

Motivation: In some cases, it may be necessary to isolate and analyze the log output for a specific process running on the device. This can be helpful when debugging performance issues or specific components of an application.

Explanation: By using the --pid flag followed by the process ID (PID) of the desired process, the adb logcat command filters the log output to include only the logs associated with that process.

Example output:

07-21 15:26:37.548  4568  4568 D MyApp   : Debug log message
07-21 15:26:37.549  4568  4568 I MyApp   : Info log message
07-21 15:26:37.550  4568  4568 W MyApp   : Warning log message
07-21 15:26:37.551  4568  4568 E MyApp   : Error log message
...

Use case 7: Display logs for the process of a specific package

Code:

adb logcat --pid=$(adb shell pidof -s package)

Motivation: When dealing with multiple processes within an Android application, it can be challenging to identify and isolate the logs related to a specific package or component. By using the adb logcat command along with the pidof command, developers can easily filter the log output for a specific package.

Explanation: The adb shell pidof -s package command returns the process ID (PID) of the specified package, and the --pid flag followed by the PID filters the log output to include only the logs associated with that process.

Example output:

07-21 15:29:28.398  4568  4568 D com.example.myapp: Debug log message
07-21 15:29:28.398  4568  4568 I com.example.myapp: Info log message
07-21 15:29:28.399  4568  4568 W com.example.myapp: Warning log message
07-21 15:29:28.400  4568  4568 E com.example.myapp: Error log message
...

Use case 8: Color the log

Code:

adb logcat -v color

Motivation: By default, the log output is displayed in plain text, which can make it difficult to distinguish between different log levels, tags, and message contents. The -v color option allows developers to colorize the log output, making it more visually appealing and easier to read.

Explanation: By using the -v color option, the adb logcat command colorizes the log output, adding different colors to represent log levels, tags, and message contents.

Example output:

07-21 15:32:19.217  4568  4568  com.example.myapp: Debug log message
07-21 15:32:19.218  4568  4568  com.example.myapp: Info log message
07-21 15:32:19.219  4568  4568  com.example.myapp: Warning log message
07-21 15:32:19.220  4568  4568  com.example.myapp: Error log message
...

Conclusion:

The adb logcat command is a versatile tool for viewing and analyzing system logs in Android applications. Whether you need to display system logs, filter logs based on tags or priority levels, or customize the log output, adb logcat provides a wide range of options and features. By understanding and utilizing the different use cases of this command, developers can navigate through logs efficiently and effectively in order to debug and optimize their Android applications.

Related Posts

How to use the command ppmtompeg (with examples)

How to use the command ppmtompeg (with examples)

ppmtompeg is a command that can be used to encode an MPEG-1 stream.

Read More
How to use the command 'semver' (with examples)

How to use the command 'semver' (with examples)

The ‘semver’ command is used to parse semantic version strings. It provides various functionalities related to semantic versioning, such as checking if a version string respects the format, converting a version string, and testing if a version string matches a given range.

Read More
How to use the command 'eva' (with examples)

How to use the command 'eva' (with examples)

The ’eva’ command is a simple calculator REPL (Read-Eval-Print Loop), similar to ‘bc’.

Read More