#100DaysOfCode in Python Transcripts
Chapter: Days 31-33: Logging
Lecture: Demo: Configuring logging
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay are we ready to start adding logging to our application? There's two steps. One, we have to globally configure how we want to log things.
0:09 Do we want our log messages to go to just standard out, so the terminal or console? Do we want them to go to a file,
0:14 if it's a file do you want that file based on a date and roll as the days change, or do you want that to just be one big file?
0:21 Or do you want to send that somewhere crazy, like, email or desktop notifications, as you saw is possible. So we're going to configure logging
0:30 and then we're just going to add the log actions as a separate way. That actually turned out to be super easy;
0:35 the only thing that's a little bit complicated is setting this up. So let's write a function over here that will let us do that.
0:43 So we're going to pass in a filename, and it can be, let's give it a default value of None. It can mean nothing, or we could pass in a filename.
0:52 So what we're going to do, is we're going to go over here and we're going to actually set the level first. So here's how this works;
0:58 we're going to say our current application is operating at level of notice and above. So there's like a hierarchy of levels in the logging,
1:07 there's like, trace, which is just super-verbose stuff, there's error, which you almost never want to skip, but maybe under normal operations
1:15 you don't want to show the trace. Only the notices and the warnings and the errors. So we're going to set this here. So we're going to use logbook,
1:22 which needs to be imported at the top, which we've just had PyCharm do, and then we can come over here and let's say,
1:28 let's set it to 'trace' for just a minute. So we have it absolutely verbose, then we'll dial it back when we're done.
1:34 Alright, so we're going to choose a level, and then based on whether we have a filename or not,
1:38 we're going to assume the fact that there's no filename, or one was not specified, that that just means 'send it to the console'.
1:45 But if there is a filename, we want to do that. So we'll say this; if filename, we want to put it in the file. So we'll say; logbook, now there's
1:52 all these little handlers in here. There's a file handler, a 'fingers crossed' handler, a Gmail handler, hashing, mixing, mail, etc. etc.
2:03 The one that we want, is we want a filename that is based on the days. So it has the date involved in the filename.
2:09 When the date changes, it automatically creates a new file, and goes from there. So that's going to be real nice.
2:14 So to accomplish that, what we need to use is a time-rotating file handler. And this takes a couple of things.
2:22 We have to obviously give it the filename, we're going to set the level, be the level, and then we want to set the date format so it knows
2:29 how to roll that out, and it has a default there, and actually that default is totally fine, so we're going to leave this off but
2:36 if you want to change that it's year, month, day, is how it's going to print that out. Okay, so we'll go like this,
2:43 and that's going to create the handler and then we would just say, 'push to application'. That means every action we do with logging
2:51 is going to use this underlying system here, so if that's not the case, we're going to say, 'logbook not stream handler',
3:00 we'll give it a stream, which we need to import this at the top, 'standard out'. That's just like what happens when you say 'print'.
3:08 And the level is equal to level again, and in this case we're going to push that to the application.
3:15 That's it. Long as we call this function, things will be initialized. But before we get on, let's make our first log message to be something that says,
3:23 here's how we've configured logging. So here's a little message that we might have, we'll say, great, the logging is initialized
3:30 and the level is trace, or something like that, and the mode is either standard out mode or file mode, and we can create one of these logs
3:43 and we'll have this little startup logger, and then we can just say 'logger.' let's say it's going to be a notice.
3:50 Okay, so this should be our very first message, and let's just come down here and say we're going, before we even call main we're going to say,
3:56 'init logging but no filename'. Let's run this and see that we've got everything working. Woo-hoo, look at that!
4:05 We have our time, we have the level, 'notice', this comes from the log category, it's the start-up log, this is the message that we actually wrote.
4:18 Logging initialized, level is nine, which is a stand-in for 'trace', and the mode is standard out, that's why you see it here and not in a file,
4:26 and of course our app still works.