Logging7:16 with Kenneth Love
One of the first things programmers do when software starts misbehaving is to stick in `print()` calls. This is great if you don't mind extra output, but there's a better way to get diagnostic info back from your app!
- level - The level that you want to start logging messages at. Any message at this level or above will be logged.
- Logging Levels:
import logging try: [1, 2, 3].remove(4) except ValueError: logging.error("tried to remove an invalid value") print("Sorry, that value doesn't exist.")
[MUSIC] 0:00 We've gotten pretty good at printing things out to the screen when we 0:04 make scripts and games. 0:07 Quite often though you don't want to print everything out to the user, 0:09 you want to collect information about how something is running, or 0:12 create a report when something blows up. 0:15 This is where logging comes in. 0:18 Let's go back to workspaces and see how to use this really handy tool. 0:20 So this dd_game.py is a slightly more advanced version of 0:23 the game that we built in Python collections. 0:28 The one where your in a maze, trying to find the door. 0:30 It draws the map, 0:33 like before, but it shows some breadcrumbs for where the player has been. 0:34 So we'll scroll down here, and you can see here we're, 0:39 we're drawing like a dot if they've been there, 'kay. 0:42 So I'd like to know where the player, monster and door start out but 0:46 obviously I don't wanna tell the player where the door and the monster are then 0:52 they can just get away easily, right, so this is a great time to use logging. 0:57 To do this we have to import the logging library. 1:02 I'll do this to the top and since I'm actually really picky 1:06 I'm gonna put it before random because l comes before r, 1:11 alphabetical yeah, it's, it's kind of ridiculous. 1:17 So I can actually use it, so let's go ahead and use it, and 1:22 we'll just do logging.info You won't see this, and 1:26 then we'll do logging.warn OH NO. 1:33 All right, so we're gonna save those and 1:35 let's go down here, and run our script and oh, look. 1:40 There's our message, it says, OH NO. 1:47 So that came out, 1:49 but the one that said, You won't see this didn't, didn't come out. 1:50 We didn't get the info message let's go and quit. 1:53 So what can we do? 1:56 First of all our, our info one didn't show up we want these things to 2:01 show up if we log them, and the second one is that it printed directly to the screen, 2:06 which means it'd be shown to the player and we don't want to show it to 2:11 the player, we want to print it to, maybe a file cuz we gotta keep our secrets. 2:15 We gotta keep our secrets, secret, right? 2:19 So to do that, we're gonna use this basic config method 2:22 This is one place where Python's foolish consistency kinda comes through. 2:26 Some really old packages use camel 2:30 case names You kinda just have to remember it, or, or look it up each time. 2:35 So let's do logging.basicConfig, and 2:40 we're gonna give it a filename, and we're gonna say game.log, 2:45 and we're gonna give it a level and it's gonna be logging.DEBUG. 2:52 Okay, so the file name argument as 2:59 you can probably guess tells logging what file to log into. 3:02 By default it does standard out which is why things get printed. 3:08 We want it to log into a file called game.log. 3:11 The path, the file name is relative, so game.log is gonna be in the same directory 3:14 as our script, if you wanted to go to say /temp or something like that, 3:19 then you'd put that in as well. 3:24 So filename makes sense, but what's with level? 3:27 What does level mean? 3:30 Well, level tells the logger what level to start paying attention to, 3:31 for messages, it'll ignore any messages with a lower level. 3:37 Well, what levels are available? 3:42 Well, there are six log levels and 3:46 their names make it pretty obvious what they're for. 3:47 They're critical, error, warning, info, debug, and notset. 3:49 You won't use notset very often, I'd almost say never. 3:57 Debug and info are for information about the running of an application, so 4:01 it's your debug information. 4:05 This just massive stream of information to keep track of what everything is doing. 4:07 Info is like oh, this is a good thing to monitor. 4:12 Warning is for 4:16 keeping track of questionable things that happen during your app like You know, 4:17 maybe an exception occurs, and you want to remember that it happened, and 4:22 then error and critical are for when things go horribly wrong. 4:27 We want our logger to catch any and all messages that have a level set. 4:32 So we're setting the level to debug. 4:36 Okay, so let's blog something. 4:39 Let's go down here, to line 73, or so, where's that at? 4:42 [BLANK_AUDIO] 4:48 It's down here. 4:51 Yeah, 69 okay, close enough. 4:53 We've got these three variables, monster, door and player location. 4:56 I wanna record what these things are, so 5:00 we're gonna call logging.info, which if you haven't noticed calling.info, .debug, 5:03 .warning, whatever, that logs a message at that particular level. 5:09 So we're gonna say monster door and 5:14 player, and then we're gonna call .format on that, and 5:23 we're gonna pass in monster, door and player location, 'kay. 5:28 So, that should log the starting position. 5:37 Let's test it out. 5:40 Right, cuz that's what we do, we test things out. 5:43 So python dd.game. 5:46 All right, nothing got printed out, which is exactly what we wanted. 5:47 We don't wanna give the players an unfair advantage, this time. 5:51 So let's go ahead and quit actually, and if we do an ls here, 5:56 we see there's this file game.log. 6:02 Now sometimes, things wont show up over here right away, so 6:06 let's just go ahead, and we'll just do cat game.log and that'll print out game.log, 6:16 and we see that we have an info message logged from the root logger. 6:22 Monster's at 0,1, doors at 0, 0, player's at 1, 2. 6:27 So this can be really tricky getting to that door instead of hitting that monster. 6:30 So, there's our message, that's great! 6:36 I'm sure as you look through this dd_game.py you'll find other places to 6:39 add in more logging. 6:43 You can log the current number of moves that the players made, 6:45 or if you made a version where the monster moves, 6:48 maybe log where the monster is after each player move. 6:50 As you can see, logging is a really useful tool to use with your scripts. 6:54 You can record an information passed in by the user, and then ask for 6:58 those logs if something doesn't work like you expected it to. 7:01 You can also log exceptions in your except blocks. 7:04 There's really nothing more frustrating than trying to debug a script when you 7:07 can't see what's going on. 7:10 Actually that brings me to our next and final topic the Python debugger. 7:12
You need to sign up for Treehouse in order to download course files.Sign up