1 00:00:00,390 --> 00:00:03,976 Another common way to investigate our app while it's running is to use 2 00:00:03,976 --> 00:00:05,490 the Android log. 3 00:00:05,490 --> 00:00:08,240 We can add calls to different log methods that will write 4 00:00:08,240 --> 00:00:10,540 data to the Android system log. 5 00:00:10,540 --> 00:00:15,170 We can then view the log in our IDE using logcat, while the app is running or 6 00:00:15,170 --> 00:00:17,250 even at a later time. 7 00:00:17,250 --> 00:00:18,890 Even if our app crashes, 8 00:00:18,890 --> 00:00:23,850 the log remains, often holding helpful information about why the app crashed. 9 00:00:23,850 --> 00:00:24,440 Let's check it out. 10 00:00:25,670 --> 00:00:28,950 Let's start by logging some information from our app. 11 00:00:28,950 --> 00:00:33,410 Right now, we're showing a toast when FunFactsActivity is first created. 12 00:00:33,410 --> 00:00:36,508 But our users don't really care about that, do they? 13 00:00:36,508 --> 00:00:40,560 Let's comment out our Toast and write a message to the log instead. 14 00:00:40,560 --> 00:00:43,380 We write to the log using the log Class and 15 00:00:43,380 --> 00:00:48,180 then one of its static methods, just like we did with Toast.makeText. 16 00:00:48,180 --> 00:00:50,820 These methods also don't return anything, so 17 00:00:50,820 --> 00:00:53,288 we don't need to store them in a variable. 18 00:00:53,288 --> 00:00:56,060 Just like toasts, we can add these log method calls 19 00:00:56,060 --> 00:01:00,140 all over the place to log information about how our app is running. 20 00:01:00,140 --> 00:01:03,710 Let's take a quick look at the documentation for the log class. 21 00:01:03,710 --> 00:01:07,670 This is the reference for the log class from the Android Developers site. 22 00:01:07,670 --> 00:01:09,690 If we look at the class overview, 23 00:01:09,690 --> 00:01:12,558 it tells us that we generally use one of five methods. 24 00:01:12,558 --> 00:01:17,630 Log.v, d, i, w, or e. 25 00:01:17,630 --> 00:01:20,431 These letters stand for different levels of logging. 26 00:01:20,431 --> 00:01:25,382 E is for errors, w is for warnings, i is for informational, 27 00:01:25,382 --> 00:01:28,598 d for debugging, and v is for verbose. 28 00:01:28,598 --> 00:01:31,397 Which is used when you're troubleshooting something and 29 00:01:31,397 --> 00:01:34,610 want to spam as much information to the log as possible. 30 00:01:34,610 --> 00:01:38,620 Android does this so we can chose the appropriate level for our log entries. 31 00:01:38,620 --> 00:01:42,560 We can filter only the log levels we care about when viewing a log. 32 00:01:42,560 --> 00:01:45,300 For example, we may be troubleshooting an error and 33 00:01:45,300 --> 00:01:47,550 only want to look at error messages. 34 00:01:47,550 --> 00:01:52,280 If we log information about the error with the log.e method, 35 00:01:52,280 --> 00:01:56,540 then we can filter the log to see just error messages and ignore everything else. 36 00:01:57,560 --> 00:02:01,410 If we scroll down, we can see some of the constants and other methods. 37 00:02:06,990 --> 00:02:11,500 Here's one called wtf, that stands for What a Terrible Failure. 38 00:02:11,500 --> 00:02:14,810 That's only for [LAUGH] extreme errors, though, and you'll probably never use it. 39 00:02:15,830 --> 00:02:19,140 All right, let's take a look at how to use the log methods. 40 00:02:19,140 --> 00:02:21,100 They're all used the same way. 41 00:02:21,100 --> 00:02:22,680 So let's take a look at the debug method. 42 00:02:24,240 --> 00:02:27,255 On a new line, let's type log.d and 43 00:02:27,255 --> 00:02:32,860 autocomplete shows us that method requires two parameters. 44 00:02:32,860 --> 00:02:36,810 The second parameter is the string data we want to write to the log. 45 00:02:36,810 --> 00:02:40,650 But the first parameter is what's known as a tag, also a string. 46 00:02:40,650 --> 00:02:43,840 The tag is used to identify the source of a log message. 47 00:02:43,840 --> 00:02:48,710 It usually identifies the class or activity where the log call occurs. 48 00:02:48,710 --> 00:02:52,380 The log contains a lot of information, which we'll see in a moment. 49 00:02:52,380 --> 00:02:57,310 Remember, the log contains messages from all apps and the operating system. 50 00:02:57,310 --> 00:03:00,980 So, supplying a tag lets us know where the message came from. 51 00:03:00,980 --> 00:03:04,330 And it also lets us filter by the tag and the logcat. 52 00:03:04,330 --> 00:03:09,210 For the first parameter, let's type FunFactsActivity inside quotation marks, 53 00:03:09,210 --> 00:03:12,500 and first let me hit Alt+Enter to import the log. 54 00:03:12,500 --> 00:03:15,390 Okay, "FunFactsActivity". 55 00:03:17,730 --> 00:03:22,873 Then add a comma and our log message will be, 56 00:03:22,873 --> 00:03:29,830 "We're logging from the onCreate() method!");. 57 00:03:29,830 --> 00:03:31,911 All right, let's run the app to see it in action. 58 00:03:38,645 --> 00:03:41,780 Okay, but where did our log message go? 59 00:03:41,780 --> 00:03:43,660 Let's take a look at the logcat. 60 00:03:43,660 --> 00:03:47,010 It usually lives here at the bottom under the code editor. 61 00:03:47,010 --> 00:03:51,260 But if it's missing, just go down here and click on Logcat. 62 00:03:51,260 --> 00:03:54,250 If that's missing too, click this little box to make it show up. 63 00:03:55,480 --> 00:04:01,510 This is the Logcat, and it has all the log data from the connected device, 64 00:04:01,510 --> 00:04:05,050 which we can see right here is the emulator. 65 00:04:05,050 --> 00:04:08,030 And if we look carefully, we can even see our log message. 66 00:04:12,040 --> 00:04:15,810 It's easy to find because the log is currently only showing messages 67 00:04:15,810 --> 00:04:20,118 from the selected application, which up here, is set to our FunFacts app. 68 00:04:20,118 --> 00:04:22,180 But that's no fun. 69 00:04:22,180 --> 00:04:26,750 Let's change that from Show only selected application to No Filters 70 00:04:28,120 --> 00:04:30,340 to see what the log really looks like. 71 00:04:30,340 --> 00:04:34,070 Now that we've got a ton of log messages, let's do some filtering. 72 00:04:34,070 --> 00:04:37,080 Let's select the Log Level dropdown, and pick Debug. 73 00:04:38,320 --> 00:04:43,570 This filters the log to show messages that are of Debug level importance or higher. 74 00:04:43,570 --> 00:04:47,450 This means that, by selecting Debug, we'll not only see Debug messages, but 75 00:04:47,450 --> 00:04:51,190 also Info, Warn, Error, and Assert messages. 76 00:04:52,230 --> 00:04:54,880 Lets switch back to Verbose and look at another filtering method. 77 00:04:56,500 --> 00:04:59,581 In this text field, we can search the log. 78 00:04:59,581 --> 00:05:03,852 I'm not sure why it's so small, but it's here so let's click in it. 79 00:05:03,852 --> 00:05:08,910 And let's search for our tag of FunFactsActivity. 80 00:05:10,580 --> 00:05:13,160 And wow, that is really frustrating that we can't see it, and 81 00:05:13,160 --> 00:05:15,210 I can't seem to make it any bigger. 82 00:05:15,210 --> 00:05:16,875 So I guess we're stuck with it. 83 00:05:16,875 --> 00:05:21,260 Anywho, it looks like there's a ton of messages that say FunFactsActivity. 84 00:05:21,260 --> 00:05:23,640 So instead, let's search for our message. 85 00:05:23,640 --> 00:05:25,104 I'll hit the x to clear my box. 86 00:05:31,327 --> 00:05:37,860 So I'll type we're and once we finish the first word, there's our message, nice. 87 00:05:37,860 --> 00:05:40,600 Now let's hide this and go back to our code. 88 00:05:40,600 --> 00:05:45,960 A common Android practice is to add a special field to our class called tag. 89 00:05:45,960 --> 00:05:46,850 Let's scroll up. 90 00:05:46,850 --> 00:05:50,770 And here, after we declare our class, let's add a line and 91 00:05:50,770 --> 00:05:55,680 type public static final String and then TAG. 92 00:05:58,610 --> 00:06:06,170 Let's set it = "FunFactsActivity" just like we typed below. 93 00:06:06,170 --> 00:06:11,070 Remember that static final means that this tag is always available, 94 00:06:11,070 --> 00:06:14,010 static, and that its value will never change. 95 00:06:14,010 --> 00:06:16,640 Final, in other words, it's a constant. 96 00:06:16,640 --> 00:06:18,220 Let's make one more change. 97 00:06:18,220 --> 00:06:22,360 What would happen if we changed the name of our FunFactsActivity class to 98 00:06:22,360 --> 00:06:24,000 some other activity? 99 00:06:24,000 --> 00:06:27,408 Well, now our tag would be a little bit misleading. 100 00:06:27,408 --> 00:06:30,520 Instead of hardcoding the class name like we've done here, 101 00:06:30,520 --> 00:06:33,680 we can use a special method to get the class name automatically. 102 00:06:34,700 --> 00:06:38,120 Let's delete FunFactsActivity and the quotes. 103 00:06:38,120 --> 00:06:42,876 And instead, type FunFactsActivity, with no quotes, 104 00:06:42,876 --> 00:06:45,980 and then .class.getSimpleName. 105 00:06:45,980 --> 00:06:51,640 We pick getSimpleName because getName includes the whole package name, whereas 106 00:06:51,640 --> 00:06:57,240 simpleName leaves that out and gives us just the class name, FunFactsActivity. 107 00:06:57,240 --> 00:07:01,140 Now you might be thinking, we just typed FunFactsActivity again. 108 00:07:01,140 --> 00:07:04,490 But this time, since it isn't inside a string, 109 00:07:04,490 --> 00:07:08,320 the refactoring tools will recognize it and make the change. 110 00:07:08,320 --> 00:07:13,700 So if I Refractor rename on FunFactsActivity and 111 00:07:13,700 --> 00:07:21,160 rename it to some other activity, notice that it changes here and my tag too. 112 00:07:21,160 --> 00:07:24,760 I don't actually want to rename this, though, so let's hit Escape to cancel. 113 00:07:26,280 --> 00:07:29,139 Now, notice that our log constant is still gray, 114 00:07:29,139 --> 00:07:32,760 because we haven't updated it down here in our debug log call. 115 00:07:34,250 --> 00:07:37,840 So instead of this hardcoded string, let's use our tag constant. 116 00:07:42,030 --> 00:07:44,191 Then let's run the app to make sure it still works. 117 00:07:50,487 --> 00:07:56,995 And there we go, a new log message with the same FunFactsActivity tag as before. 118 00:07:56,995 --> 00:08:01,240 Logging can be incredibly helpful for troubleshooting problems with our apps. 119 00:08:01,240 --> 00:08:04,090 It's also a great way to make sure everything is running like we 120 00:08:04,090 --> 00:08:05,220 expect it to. 121 00:08:05,220 --> 00:08:08,070 Especially when whatever we're testing isn't visible on the screen.