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