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