본문 바로가기
User Guide

Logcat으로 로그를 작성하고 보기 Write and View Logs with Logcat (34)

by 각종 잡상식 지식 모음 2016. 6. 10.
반응형

Logcat으로 로그를 작성하고 보기
Write and View Logs with Logcat

Android Studio includes a logcat tab inside Android Monitor that prints system events, such as when a garbage collection occurs, as well as messages your app prints with the Log class to assist with debugging. Logcat displays messages in real time and also keeps a history so you can view older messages.

To display just the information of interest, you can create filters, modify how much information is displayed in messages, set priority levels, display messages produced by app code only, and search the log. By default, the logcat Monitor shows the log output related to the most recently run app only.

When an app throws an exception, the logcat Monitor shows a message followed by the associated stack trace containing links to the code. This feature can help you fix errors and improve app operation.

Writing Log Messages


The Log class allows you to create log messages that appear in the logcat window. Generally, you should use the following log methods, listed in order from the highest to lowest priority (or, least to most verbose):

You should never compile versbose logs into your app, except during development. Debug logs are compiled in but stripped at runtime, while error, warning and info logs are always kept.

For each log method, the first parameter should be a unique tag and the second parameter is the message. The tag of a system log message is a short string indicating the system component from which the message originates (for example, ActivityManager ). Your tag can be any string that you find helpful, such as the name of the current class.

A good convention is to declare a TAG constant in your class to use in the first parameter. For example, you might create an information log message as follows:

private static final String TAG = "MyActivity";
...
Log.i(TAG, "MyClass.getView() — get item number " + position);

Note: Tag names greater than 23 characters are truncated in the logcat output.

logcat Message Format


The printed log message appears in logcat as follows:

date time PID-TID/package priority/tag: message

For example, the Log.i() code example above has a priority of I (for "information") and a tag of MyActivity:

12-10 13:02:50.071 1901-4229/com.google.android.gms I/MyActivity: MyClass.getView() — get item number 1

PID stands for process identifier and TID is thread identifier; they can be the same if there’s only one thread.

Displaying a Running App in the logcat Monitor


To display the log messages for a particular app:

  1. Meet the prerequisites and dependencies.
  2. Open an app project.
  3. Run the app on a hardware device or emulator.
  4. Display Android Monitor.
  5. Click the logcat tab.
  6. By default, the logcat Monitor displays just the log messages for your app running on the device or emulator:

    To change this default, see Filtering logcat Messages.

  7. Optionally select a different device, emulator, or process.

Setting the Log Level


You can control how many messages appear in the logcat Monitor by setting the log level. You can display all messages, or just the messages indicating the most severe conditions.

Remember that the logcat Monitor continues to collect all messages regardless of the log level setting. The setting just determines what the logcat Monitor displays.

To set the log level:

  • In the Log level menu, select one of the following values:
    • Verbose - Show all log messages (the default).
    • Debug - Show debug log messages that are useful during development only, as well as the message levels lower in this list.
    • Info - Show expected log messages for regular usage, as well as the message levels lower in this list.
    • Warn - Show possible issues that are not yet errors, as well as the message levels lower in this list.
    • Error - Show issues that have caused errors, as well as the message level lower in this list.
    • Assert - Show issues that the developer expects should never happen.

Searching logcat Messages


To search the messages currently displayed in the logcat Monitor:

  1. Optionally select Regex if you want to use a regular expression search pattern.
  2. Type a character sequence in the search field Search icon.
  3. The logcat Monitor display changes accordingly.

  4. Press Enter to store the search string in the menu during this session.
  5. To repeat a search, choose it from the search menu. Select or deselect Regex as needed (the setting isn’t remembered).

Filtering logcat Messages


One way to reduce the log output to a manageable level is to restrict it by using a filter.

Note: The filter applies to your full logcat history, not just those messages currently displayed in the logcat Monitor. Make sure your other display options are set appropriately so you can see the filter output you want to examine.

To define and apply a filter:

  1. In the filter menu, select a filter option:
    • Show only selected application - Display the messages produced by the app code only (the default). The logcat Monitor filters the log messages using the PID of the active app.
    • No Filters - Apply no filters. The logcat Monitor displays all log messages from the device, regardless of which process you selected.
    • Edit Filter Configuration - Create or modify a custom filter. For example, you could create a filter to view log messages from two apps at the same time.

    After you define filters, you can also select them in the menu. To remove them from the menu, delete them.

  2. If you selected Edit Filter Configuration, create or modify a filter.
    1. Specify the filter parameters in the Create New Logcat Filter dialog:
      • Filter Name - Type the name of a filter you want to define, or select it in the left pane to modify an existing filter. The name can contain lowercase characters, underscores, and digits only.
      • Log Tag - Optionally specify a tag. For more information, see logcat Message Format.
      • Log Message - Optionally specify log message text. For more information, see logcat Message Format.
      • Package Name - Optionally specify a package name. For more information, see logcat Message Format.
      • PID - Optionally specify a process ID. For more information, see logcat Message Format.
      • Log Level - Optionally select a log level. For more information, see Setting the Log Level.
      • Regex - Select this option to use regular expression syntax for that parameter.
    2. Click + to add the filter definition to the left pane.
    3. To remove a filter, select it in the left pane and click -.

    4. When you’re finished, click OK. If you click Cancel, any filter additions or modifications are lost.
  3. Make sure you see the log messages you want to examine.
  4. If you don't think you see the log messages you want, try selecting No filters and searching for particular log messages.

Configuring the logcat Header Display


To customize the header display to show just the information you’re interested in:

  • Select Use Soft Wraps Use Soft Wraps icon to see the entire message and prevent it from running off of the right edge.
  • Click Configure Logcat Header Configure Logcat header icon to specify elements of the messages that you want to show or hide, and then click OK.

For more information about message elements, see logcat Message Format.

Moving Up and Down the Stack Trace


When the app throws an exception, the message includes a stack trace of method calls. logcat Monitor lets you quickly locate stack traces in the log and view the associated code in the Code Editor. If needed (and possible), the decompiler derives source code that you can view.

To move up and down the stack trace, and view the associated code in the Code Editor:

  • Click Up the Stack Trace Up the Stack Trace icon to move to the previous method in relation to the current position in the log.
  • Click Down the Stack Trace Down the Stack Trace icon to move to the next method in relation to the current position in the log.

Moving to the End of the Log


Clicking a particular message stops the display of messages.

To quickly move to the end of the log to see the real-time message flow:

  • Click Scroll to the End Scroll to the End icon.
  • Press the End key.
  • Scroll or press the Page Down key until you reach the end.

Printing and Writing to a File


To preserve log information, you can send the log to a printer, write the log to a PDF file, or copy and paste the log into a text file.

To print the log or write it to a PDF file:

  1. Click Print Print icon.
  2. In the Android Studio Print dialog, optionally change print parameters, and then click Print.
  3. In the operating system Print dialog, optionally change print parameters, and then click Print.
  4. You can set the parameters to send the log to a printer or write it to a PDF file.

To copy the log to a text file:

  1. In the logcat Monitor, select and then copy log text.
  2. Press Ctrl+A (⌘A) to select all.

  3. Open a text editor and paste the text into a file.

Clearing and Restarting the Log


To clear (flush) the entire log:

  • Click Clear logcat Clear logcat icon.

If there's a problem and the log is no longer progressing, you can restart the log:

  • Click Restart Restart icon.


반응형

댓글