Python for Entrepreneurs Transcripts
Chapter: Monitoring and logging in production
Lecture: Logging dependency versions at startup

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now that we have logging set up, there is one additional thing I'd like to do at app startup that I think will make the forensic story
0:09 much stronger for our application. If you run into an error, if you are having a hard time reproducing it,
0:17 or even if you discover a security vulnerability, it would be really good to know when in your app's life cycle
0:23 is it running which package, right, which version of that package. So what I am going to do is add the ability to find all of the dependencies
0:32 of our application and log out the dependency and the version at the start. That way when you go back into the logs you can actually see
0:40 which version it's running on for all the packages and their dependencies, and their dependencies and so on, when you are looking at the log file,
0:48 so if you have to go back six months and you see some things happening, you could actually check that version of some library
0:55 that may have been giving you trouble, and see if it was just the version it was running on or if that's still long running problem with your app.
1:03 So let's go down to our logging bit here and we'll write one more function. log_package_versions. So let PyCharm write that, thank you,
1:15 but this is going to be big and ugly so I am going to put it at the very bottom, down here like this.
1:21 OK, now the implementation for this is not especially insightful, so let me just drop it in.
1:29 OK, so what we are going to do is we are going to get the startup log, and then we are going to set some requirements that we're using,
1:36 I'll show you how to get that in a second, we are going to put them in alphabetical order,
1:42 and then first of all, we are going to print out the Python version, which is great to have in your log files every time your app starts,
1:48 and then for each package, we are going to print out its version as well,
1:52 and finally we'll do just a tad bit of timing so that we'll know whether or not this is putting lots of pressure on our app startup, right,
1:59 if this takes three seconds, maybe it's not worth it, if it takes 50 miliseconds, it's probably great to have it in the logs.
2:05 OK, so how do we figure out what the requirements are? Well, remember, we are using this particular virtual environment,
2:13 blueyellow_env, now I've activated it here, so when I type "pip list", these are actually the dependencies that only this application uses,
2:25 remember this environment is tied in a one to one mapping over to this application, so what is here is what is used by the application.
2:33 If I had added something that I stopped using it, I could "pip uninstall" it, right,
2:37 but this list basically is what we need, so we can just use a different format, json.
2:44 And this will actually be something we can drop into Python and use. So here we have this list of requirements and we can just drop that in,
2:53 do a little formatting, and notice the version is listed here, we are not going to use the version.
3:01 What we are going to do instead, is we are just going to use the name part, now you'll be tempted to go through and like delete the version, whatnot,
3:10 because you want to have this nice and cleaned up, but here is the thing, as soon as you reinstall another dependency
3:17 you are going to need to rerun this code here and recopy and paste that chunk there and it's going to get messy
3:23 so my view is just leave it more or less like however it is once you just format the file.
3:28 So let's go and run this and see the logging of our versions. Excellent, look at that, OK so if we go to the top,
3:39 logging initialized, here is the Python version info, so we are running on 3.6.0 right now, which is built for Apple, fantastic,
3:49 and then the package versions, we got Chameleon 3. Remember, I deleted it here, right, we are not using the version of this list,
3:56 just let it tag along because it's a pain to maintain. docopt - we have to use docopt 0.4.0 at the current time,
4:04 because MailChimp doesn't support the newer one, I don't know why but that is really annoying, here we just installed Logbook 1.0.0 and so on.
4:12 So this is cool, right, this is all of our versions and how long did it take to generate it, 34 milliseconds, acceptable.
4:19 Also we are running in production mode, which is cool, that's because of the particular hidden ini file
4:27 that I am going to put a bunch of production settings into, and then here is our other various things, so connecting to the database
4:34 with this connection stream, and starting the server right this actually comes out of the PyCharm,
4:40 but this is a print statement that should be converted to some kind of log statement,
4:44 you can tell because it doesn't have the date, time, level and section of the app. I don't know what you think about this version stuff here,
4:52 but I really love o have this in my logs so I can go back and see what was running when.


Talk Python's Mastodon Michael Kennedy's Mastodon