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