Python for Entrepreneurs Transcripts
Chapter: Monitoring and logging in production
Lecture: Creating the logging skeleton structure

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Let's add logging to our application. To be honest, we've been putting this off for quite a while,
0:10 just because I wanted to cover the core concepts first, in a real app, I would have started with logging sooner.
0:16 But, we've kind of seen logging a little bit, if I run our app you can see that some stuff comes out here like running in production mode,
0:22 using this database, but this logging is not going to where we wanted to go, this is just print.
0:28 So we are going to convert these to proper log messages that we can direct to different sources to rotating files directly
0:37 from out of the app to the console, maybe even to databases, we could do all sorts of things with these once we get it to logging framework.
0:44 OK, so to get started, let's go to the beginning of our app here, and we are going to have, we've got this init_mode, that's cool,
0:55 let's go ahead and add one thing at the beginning here, very first thing we're going to say is init_logging,
1:02 and we're going to write this function, and up here, I am going to define a startup_log, so the rest of these little configuration methods,
1:12 they might use this log, so we want to make sure that this runs first so that the log is around for everything, so over here we can create this method,
1:22 and I don't like having these detail methods first, I have like the main flow going and then this,
1:31 so what are we going to do in this logging? We are going to set that log file, so we are going to define a global startup_log,
1:38 and that lets us basically initialize it here, rather than working with the local variable and what I'd like to do is
1:45 I would like to put into the configuration file a way that we can configure the level of logging. You'll see that there is different levels of logging
1:55 we want to come out of the application, and there is messages that map to different levels,
2:02 so some things might be informational, some things might be warnings, some things might be errors; we can through configuration say
2:09 hey only show me warnings and errors right now, say in production. But in development mode, maybe we want to see everything,
2:17 all the little steps and the info pieces. So we can store that into a configuration file and pull that in and we'll do that in a moment.
2:25 But first, let's just assume they are there and get it out. So remember we are going to get our settings_dictionary, say log_level,
2:42 we'll get the log level and we'll say log_filename, so we get both of those,
2:53 OK great, then let's create a file that is going to actually manage the logging,
2:59 this dunder init is already getting kind of complicated and we are not done yet, so let me go over here and create one of these services,
3:07 we have services for emails, for mailing lists, for stores, e-commerce whatnot, let's add one for logging.
3:15 OK, like before this is going to follow pretty much the same pattern, we have a few sort of static variables here, so I have a staticmethod,
3:27 and this is going to take log_level, and file name, alright, so let's just do a quick pass here really quick and we'll just wire these things up,
3:40 and then we'll be ready to actually add Logbook. So we are going to need to import that of course and we'll say global_init
3:52 and we'll give it the log_level and the log_filename. Now this is going to return a logger for us, and we'll say something like this,
4:05 we'll say startup_log = LogService.startup_log, something like that. And we'll make this a property, and you can see it doesn't exist yet,
4:16 but it's going to in a moment. Now that got added as a field, I don't want it as a field, I want this as a property, and the name is going to be this.
4:32 Right now we return None, but in just a moment once we get Logbook setup we are going to return the Logbook instance.
4:41 So we almost have the skeleton of this logging setup in place, let's just go ahead and put something in our configuration file here.
4:49 So remember we had our log level and our log file name. Let's set this to be INFO, that's pretty good, and we'll have log_filename,
5:01 and let's just set that to be ./ we'll just do ./app_log.txt, something like that. OK, final thing to make sure this is all hanging together,
5:18 let's just do a little print, "Logging setup:" like this, and we'll just say we are going to print out the log_level and the filename.
5:32 OK, run it, "Logging setup: None and None". Oh yes, why is that happening, because in a little bit when we get to Rollbar,
5:43 I am going to need some more access to private keys that I can't share with you guys
5:48 so I put that into a separate startup file that's just on my desktop, let me move that settings over and try again.
5:55 Running again, what do we get? Here we go, logging setup info is going informational and above notices are going to be sent to ./app_logg.txt.
6:08 Excellent, so now we are ready to integrate Logbook.

Talk Python's Mastodon Michael Kennedy's Mastodon