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