Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Adding logging to Rock Paper Scissors

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up we want to add basic logging. In the previous video you saw how we're using logging on the background services
0:08 that organize and manage the video files at top life and training. We're going to do something less ambitious. We're just going to keep a history
0:15 of what's happened with the game. So okay if there's an error or something weird happens or we just want to see who's played and done what
0:20 we can go back and check that out. Let me go down here to the bottom and write a function like def log and it's going to take a message.
0:28 And for now, its just going to print not really and then the message. Not really going to log it we're going to figure out how to do that in a second
0:35 but the first thing we want to do is just use this function in various places. So lets go over here in main and we'll say log, app starting up.
0:45 Like that, and over here we printed out those before, remember that? Let's just log we voted those rolls.
0:51 And we can even do it better from, we could do I guess we could get our file, os.path.basename(filename) This will unroll the full directory
1:01 and just say the short directory. That's probably good, cause the full path is really long and we don't want to see that. Okay, so we are logging that.
1:12 Let's go over here and we can print out that there's a new game happening. Let's see, maybe do that at the top.
1:18 Right here we could have the player that logged in. We could log, player one as logged in. That would be cool.
1:26 And then down here it probably makes sense to do it inside this function. We can do a log here we can say new game starting between
1:39 player one and player two. Let's just see that something is happening when we run this. Okay, not really. App starting up.
1:45 Not really, loaded these from rolls.json My name is Michael, look, not really, Michael is logged in. We are going to get rid of this not really.
1:52 I just want to have some kind of distinguishing thing to remind me, yeah yeah we've still got to go put that in a file. We shouldn't be seeing it here
1:58 it should be going to a file. But let's get the messages together and then we're going to save them to a file.
2:03 Alright, starting a new game between Michael and the computer. I'll just play this out real quick. Because you know I'm going to lose.
2:09 Oh! Oh! Oh! I could win! Nope, of course I don't win. Of course I don't win, because the computer always beats me.
2:15 Alright, so over here we are going to play around. Let's do, this stuff, you know where we have the print.
2:22 Usually we see the print, maybe we went along. Maybe we don't. We could have some interesting things
2:28 in our log function that say sometimes we want to show that both the screen as well as print it out as well as save it to the file.
2:37 Now we will just put a quick message here. Put this all on one line. And let's log this out here. If we have this twice, probably don't want to have it
2:51 duplicated so we can do a little introduce a variable. And call this a message, and then print out the message here. That's a cool trick right?
2:59 And then, same thing here. The noise work. We can make it. Alright, that good. And then, I think we are going to have
3:12 maybe we want to have this come out as well. And then finally we will log this. And again this should really be not duplicated, right?
3:26 I'm really starting to come around to the idea of having the log message have the option to send it to multiple places, when you break lines.
3:32 Okay, that's not a big deal. Alright, I think that might be it. Let's go ahead and run this one more time just to see that we got the messages.
3:45 Okay, app starting up. We know our rolls and the rules. My name is Michael. Michael is logged in. Our role is starting round one.
3:51 Okay, so in this round Michael rolled rock. Computer rolls sponge, I pound out that rock. I take the round. Score is 1, 0.
4:00 And let's just finish this out, and maybe I can move up in the leaderboard. Nope, of course I can't. Though, there we go.
4:07 And then we are logging that computer wins the game and then maybe log, game over or app exiting up here at the top.
4:14 Alright, so this is what we are going to do with our logging let's do, log game over. Cool. Now, this is great we got all this in place
4:20 but again the not really bit means we're actually not doing anything interesting with it just yet. It's going to be what we do next.
4:28 Let's call this part done, for now.

Talk Python's Mastodon Michael Kennedy's Mastodon