#100DaysOfCode in Python Transcripts
Chapter: Days 31-33: Logging
Lecture: Concepts: Logging

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that you've seen logging and logbook
0:01 in action, let's quickly review some of the concepts.
0:06 So, we're going to start by importing the logbook,
0:08 And if we're going to not use a file,
0:10 we're going to need to also import sys,
0:12 so we can get to standard out.
0:15 Then we're going to maybe get a file name,
0:17 that can come from, like a configuration file,
0:21 the environment, you could hard-code it, whatever you want.
0:24 We're going to set a logging level here.
0:26 Now I didn't actually demonstrate it,
0:28 but if we went and switched that to say warnings,
0:31 you would no longer see any of the messages that were trace,
0:34 right? Only the stuff warning and above would appear in the
0:37 log file, or in the console, and nothing else.
0:39 So you can dial this up and down,
0:41 depending on whether you're running a debug build,
0:44 or production build, or release build, things like that.
0:47 So, set this, and that may also be based on configuration,
0:50 or version, type of app, right?
0:53 Like it's production or development and so on.
0:56 Then we have to install a handler,
0:58 so we're either going to use a stream handler,
1:00 set that to standard out,
1:01 set the level of push it to the application.
1:03 Or, if you want to go to a file, I recommend
1:06 the timed rotating file handler, and do the same thing.
1:10 Once this is set up, then it's basically ready to go.
1:15 So remember this level acts as a filter here,
1:17 so you can always go logger.trace, logger.info,
1:22 but only if the level is low enough,
1:25 will it actually show on these handlers.
1:29 Now, you do that, what we just saw, once,
1:32 at the beginning of your app, and then,
1:34 anytime you want to log something,
1:35 you're going to create one of these loggers,
1:37 like, heres a start up logger.
1:39 And we have this little info message, like,
1:41 hey, we're getting started like this.
1:43 Now this just a string, nothing logging about it.
1:45 But then we can go to our startup log and say notice,
1:48 this message, right? And that's a pretty high level of
1:52 log message there, this notice level.
1:55 And this is going to go, probably near the front,
1:58 so it'll tell you what level you're logging at,
2:00 which will help you understand what
2:01 the rest of the log means.
2:03 We have different options, we have notice,
2:05 info, trace, warn, error, and critical.
2:08 So critical is even more of an error than error, right?
2:11 We also have exception, if you just want to show
2:14 a straight up exception.
2:15 And then the output looks like this,
2:16 it has the time, it has the level, here, notice,
2:19 it has the log category, log name, in this case, app,
2:23 and then it finally has the actual message.
2:26 Really nice, really easy to set up,
2:27 and it's super, super flexible.