blob: 6ee69c77316cc97ab6612607b42ddfa2cf98ed84 [file] [log] [blame]
Robert Lyce4d2292010-12-16 17:26:11 -08001page.title=Reading and Writing Log Messages
2@jd:body
3
4<div id="qv-wrapper">
5 <div id="qv">
6 <h2>In this document</h2>
7
8 <ol>
9 <li><a href="#logClass">The Log class</a></li>
10
11 <li><a href="#startingLogcat">Starting LogCat</a></li>
12
13 <li><a href="#filteringOutput">Filtering Log Output</a></li>
14
15 <li><a href="#outputFormat">Controlling Log Output Format</a></li>
16
17 <li><a href="#alternativeBuffers">Viewing Alternative Log Output Buffers</a></li>
18
19 <li><a href="#viewingStd">Viewing stdout and stderr</a></li>
20
21 <li><a href="#DebuggingWebPages">Debugging Web Pages</a></li>
22 </ol>
23 </div>
24 </div>
25
26 <p>The Android logging system provides a mechanism for collecting and viewing system debug
27 output. Logcat dumps a log of system messages, which include things such as stack traces when the
28 emulator throws an error and messages that you have written from your application by using the
29 {@link android.util.Log} class. You can run LogCat through ADB or from DDMS, which allows you to
30 read the messages in real time.</p>
31
32 <h2 id="logClass">The <code>Log</code> class</h2>
33
34 <p>{@link android.util.Log} is a logging class that you can utilize in your code to print out
35 messages to the LogCat. Common logging methods include:</p>
36
37 <ul>
38 <li>{@link android.util.Log#v(String,String)} (verbose)</li>
39
40 <li>{@link android.util.Log#d(String,String)} (debug)</li>
41
42 <li>{@link android.util.Log#i(String,String)} (information)</li>
43
44 <li>{@link android.util.Log#w(String,String)} (warning)</li>
45
46 <li>{@link android.util.Log#e(String,String)} (error)</li>
47 </ul>For example:
48 <pre class="no-pretty-print">
49Log.i("MyActivity", "MyClass.getView() &mdash; get item number " + position);
50</pre>
51
52 <p>The LogCat will then output something like:</p>
53 <pre class="no-pretty-print">
54I/MyActivity( 1557): MyClass.getView() &mdash; get item number 1
55</pre>
56
57 <h2 id="startingLogcat">Using LogCat</h2>
58
59 <p>You can use LogCat from within DDMS or call it on an ADB shell. For more information on how to
60 use LogCat within DDMS, see <a href="{@docRoot}guide/developing/debugging/ddms.html#logcat">Using
61 DDMS</a>. To run LogCat, through the ADB shell, the general usage is:</p>
62 <pre>
63[adb] logcat [&lt;option&gt;] ... [&lt;filter-spec&gt;] ...
64</pre>
65
66 <p>You can use the <code>logcat</code> command from your development computer or from a remote
67 adb shell in an emulator/device instance. To view log output in your development computer, you
68 use</p>
69 <pre>
70$ adb logcat
71</pre>
72
73 <p>and from a remote adb shell you use</p>
74 <pre>
75# logcat
76</pre>
77
78 <p>The following table describes the <code>logcat</code> command line options:</p>
79
80 <table>
81 <tr>
82 <td><code>-c</code></td>
83
84 <td>Clears (flushes) the entire log and exits.</td>
85 </tr>
86
87 <tr>
88 <td><code>-d</code></td>
89
90 <td>Dumps the log to the screen and exits.</td>
91 </tr>
92
93 <tr>
94 <td><code>-f&nbsp;&lt;filename&gt;</code></td>
95
96 <td>Writes log message output to <code>&lt;filename&gt;</code>. The default is
97 <code>stdout</code>.</td>
98 </tr>
99
100 <tr>
101 <td><code>-g</code></td>
102 <td>Prints the size of the specified log buffer and exits.</td>
103 </tr>
104
105 <tr>
106 <td><code>-n&nbsp;&lt;count&gt;</code></td>
107
108 <td>Sets the maximum number of rotated logs to <code>&lt;count&gt;</code>. The default value
109 is 4. Requires the <code>-r</code> option.</td>
110 </tr>
111
112 <tr>
113 <td><code>-r&nbsp;&lt;kbytes&gt;</code></td>
114
115 <td>Rotates the log file every <code>&lt;kbytes&gt;</code> of output. The default value is
116 16. Requires the <code>-f</code> option.</td>
117 </tr>
118
119 <tr>
120 <td><code>-s</code></td>
121
122 <td>Sets the default filter spec to silent.</td>
123 </tr>
124
125 <tr>
126 <td><code>-v&nbsp;&lt;format&gt;</code></td>
127
128 <td>Sets the output format for log messages. The default is <code>brief</code> format. For a
129 list of supported formats, see <a href="#outputFormat">Controlling Log Output
130 Format</a>.</td>
131 </tr>
132 </table>
133
134 <h3 id="filteringOutput">Filtering Log Output</h3>
135
136 <p>Every Android log message has a <em>tag</em> and a <em>priority</em> associated with it.</p>
137
138 <ul>
139 <li>The tag of a log message is a short string indicating the system component from which the
140 message originates (for example, "View" for the view system).</li>
141
142 <li>The priority is one of the following character values, ordered from lowest to highest
143 priority:</li>
144
145 <li style="list-style: none; display: inline">
146 <ul>
147 <li><code>V</code> &mdash; Verbose (lowest priority)</li>
148
149 <li><code>D</code> &mdash; Debug</li>
150
151 <li><code>I</code> &mdash; Info</li>
152
153 <li><code>W</code> &mdash; Warning</li>
154
155 <li><code>E</code> &mdash; Error</li>
156
157 <li><code>F</code> &mdash; Fatal</li>
158
159 <li><code>S</code> &mdash; Silent (highest priority, on which nothing is ever printed)</li>
160 </ul>
161 </li>
162 </ul>
163
164 <p>You can obtain a list of tags used in the system, together with priorities, by running
165 LogCat and observing the first two columns of each message, given as
166 <code>&lt;priority&gt;/&lt;tag&gt;</code>.</p>
167
168 <p>Here's an example of logcat output that shows that the message relates to priority level "I"
169 and tag "ActivityManager":</p>
170 <pre>
171I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}
172</pre>
173
174 <p>To reduce the log output to a manageable level, you can restrict log output using <em>filter
175 expressions</em>. Filter expressions let you indicate to the system the tags-priority
176 combinations that you are interested in &mdash; the system suppresses other messages for the
177 specified tags.</p>
178
179 <p>A filter expression follows this format <code>tag:priority ...</code>, where <code>tag</code>
180 indicates the tag of interest and <code>priority</code> indicates the <em>minimum</em> level of
181 priority to report for that tag. Messages for that tag at or above the specified priority are
182 written to the log. You can supply any number of <code>tag:priority</code> specifications in a
183 single filter expression. The series of specifications is whitespace-delimited.</p>
184
185 <p>Here's an example of a filter expression that suppresses all log messages except those with
186 the tag "ActivityManager", at priority "Info" or above, and all log messages with tag "MyApp",
187 with priority "Debug" or above:</p>
188 <pre>
189adb logcat ActivityManager:I MyApp:D *:S
190</pre>
191
192 <p>The final element in the above expression, <code>*:S</code>, sets the priority level for all
193 tags to "silent", thus ensuring only log messages with "View" and "MyApp" are displayed. Using
194 <code>*:S</code> is an excellent way to ensure that log output is restricted to the filters that
195 you have explicitly specified &mdash; it lets your filters serve as a "whitelist" for log
196 output.</p>
197
198 <p>The following filter expression displays all log messages with priority level "warning" and higher, on all tags:</p>
199 <pre>
200adb logcat *:W
201</pre>
202
203 <p>If you're running LogCat from your development computer (versus running it on a
204 remote adb shell), you can also set a default filter expression by exporting a value for the
205 environment variable <code>ANDROID_LOG_TAGS</code>:</p>
206 <pre>
207export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
208</pre>
209
210 <p>Note that <code>ANDROID_LOG_TAGS</code> filter is not exported to the emulator/device
211 instance, if you are running LogCat from a remote shell or using <code>adb shell
212 logcat</code>.</p>
213
214 <h3 id="outputFormat">Controlling Log Output Format</h3>
215
216 <p>Log messages contain a number of metadata fields, in addition to the tag and priority. You can
217 modify the output format for messages so that they display a specific metadata field. To do so,
218 you use the <code>-v</code> option and specify one of the supported output formats listed
219 below.</p>
220
221 <ul>
222 <li><code>brief</code> &mdash; Display priority/tag and PID of originating process (the default
223 format).</li>
224
225 <li><code>process</code> &mdash; Display PID only.</li>
226
227 <li><code>tag</code> &mdash; Display the priority/tag only.</li>
228
229 <li><code>thread</code> &mdash; Display process:thread and priority/tag only.</li>
230
231 <li><code>raw</code> &mdash; Display the raw log message, with no other metadata fields.</li>
232
233 <li><code>time</code> &mdash; Display the date, invocation time, priority/tag, and PID of the
234 originating process.</li>
235
236 <li><code>long</code> &mdash; Display all metadata fields and separate messages with blank
237 lines.</li>
238 </ul>
239
240 <p>When starting LogCat, you can specify the output format you want by using the
241 <code>-v</code> option:</p>
242 <pre>
243[adb] logcat [-v &lt;format&gt;]
244</pre>
245
246 <p>Here's an example that shows how to generate messages in <code>thread</code> output
247 format:</p>
248 <pre>
249adb logcat -v thread
250</pre>
251
252 <p>Note that you can only specify one output format with the <code>-v</code> option.</p>
253
254 <h3 id="alternativeBuffers">Viewing Alternative Log Buffers</h3>
255
256 <p>The Android logging system keeps multiple circular buffers for log messages, and not all of
257 the log messages are sent to the default circular buffer. To see additional log messages, you can
258 run the <code>logcat</code> command with the <code>-b</code> option, to request viewing of an alternate
259 circular buffer. You can view any of these alternate buffers:</p>
260
261 <ul>
262 <li><code>radio</code> &mdash; View the buffer that contains radio/telephony related
263 messages.</li>
264
265 <li><code>events</code> &mdash; View the buffer containing events-related messages.</li>
266
267 <li><code>main</code> &mdash; View the main log buffer (default)</li>
268 </ul>
269
270 <p>The usage of the <code>-b</code> option is:</p>
271 <pre>
272[adb] logcat [-b &lt;buffer&gt;]
273</pre>
274
275 <p>Here's an example of how to view a log buffer containing radio and telephony messages:</p>
276 <pre>
277adb logcat -b radio
278</pre><a name="stdout"
279 id="stdout"></a>
280
281 <h2 id="viewingStd">Viewing stdout and stderr</h2>
282
283 <p>By default, the Android system sends <code>stdout</code> and <code>stderr</code>
284 (<code>System.out</code> and <code>System.err</code>) output to <code>/dev/null</code>. In
285 processes that run the Dalvik VM, you can have the system write a copy of the output to the log
286 file. In this case, the system writes the messages to the log using the log tags
287 <code>stdout</code> and <code>stderr</code>, both with priority <code>I</code>.</p>
288
289 <p>To route the output in this way, you stop a running emulator/device instance and then use the
290 shell command <code>setprop</code> to enable the redirection of output. Here's how you do it:</p>
291 <pre>
292$ adb shell stop
293$ adb shell setprop log.redirect-stdio true
294$ adb shell start
295</pre>
296
297 <p>The system retains this setting until you terminate the emulator/device instance. To use the
298 setting as a default on the emulator/device instance, you can add an entry to
299 <code>/data/local.prop</code> on the device.</p>
300
301 <h2 id="DebuggingWebPages">Debugging Web Apps</h2>
302 <p>
303 If you're developing a web application for Android, you can debug your JavaScript using the console JavaScript APIs,
304 which output messages to LogCat. For more information, see
305 <a href="{@docRoot}guide/webapps/debugging.html">Debugging Web Apps</a>.</p>