#100DaysOfCode in Python Transcripts
Chapter: Days 31-33: Logging
Lecture: Demo: Writing the log messages
0:00 Now that we have logging configured and we are calling that to set everything up let's go and actually create a app level
0:09 and an API level logger, so let's call this the app_log and this, we already saw me say Logbook.logger and we give it the category
0:18 so I'll call this App right and then we can use that throughout this part of our program and we'll know not just what the message was but
0:25 generally where it came from and let's do something similar over in the API section. So we'll call it api_log to import log
0:35 book again and this will be API Okay, so when we're doing an accents with the API, maybe the timing and so on we could track that over here.
0:46 Let's start with the app log. Now, I always find there's a super tricky balance between too much logging code getting
0:55 into your application and making it hard to read being too verbose and not capturing what you need so this is a pretty simple example
1:02 so it's a little bit hard to find that balance but,you can see that it's pretty easy to read. Let's deal with the error cases first.
1:09 Let's come down here and say app_log It's going to be an error. So this could either be error or it could be a
1:16 warning right like our program isn't broken it's just in an environment that it can't operate in. So I'm going to call this actually a warning.
1:24 I'll do a bore in here and it'll say same message. So we can come out here and refactor that to a variable and just call that message
1:32 more in the same little message like that. Actually, we already have a classifier there so we'll say like this... Same thing here...
1:48 You see it's getting harder to read but we will have a record of this so that's pretty cool. Now this one, like these two we anticipate right.
1:55 This is if the internet is off. This is if they don't type anything and they just hit enter. This is kind of standard stuff so that's why
2:00 it's a warning but down here this is like we didn't expect this. We have no idea what went wrong. Something went wrong.
2:07 We could say this error and give it the message with the formatted exception details added in there right? Or there's actually a way to say
2:16 there's some kind of unknown exception. Just record that as an error so we can actually say exception and just give it the exception like this.
2:23 Okay, so let's go and try to run our program now and see what we can get to happen on it. File just search for T, looks like it works
2:34 so I'll just run a few searches see if I can get that exception error to hit. Okay here, there's a very rare chance that
2:41 some random error's going to be thrown. Recall that from day 25 and I got it finally after a bunch of attempts to throw a StopIteration exception.
2:50 So here you can see it recorded as an error. It said this isn't a category app and then it just put the trace back right there.
2:57 That is... this section right here. Let's try some other errors. We know that it's an error to hit enter and that's going to be a ValueError
3:08 because we're searching for nothing. Here you can see error, you must search for a term. This is just the print statement right there.
3:16 And then here, this is the log statement that we put as a warning. Right, warning,the app says you must search
3:21 for a search term and that's because we put it in the log as warning 'cause we know it's not technically broken. The user's just using it wrong.
3:31 So the final thing to test is to turn off the internet. Alright look, we got our standard print and then we got our log message which is warning
3:41 app at the app level we could not find a server, check your network connection. Alright, let's just add one final sort of success message here.
3:55 Let's go down here and say like app_log this info trace I don't know. Let's put it at trace so this is super verbose
4:03 but we can say something to the effect of.. Clean that up a little now let's see how this works. Run it successfully, search for action.
4:14 Perfect, it worked and we logged the fact that trace app search is successful. The keyword is action. There were eight results.
4:21 Let's search for runner. Here we go, search, we got six results. Keyword runner. Alright, so I feel like we're getting some
4:30 decent logging at this level. The other thing I'd like to do before we call this done is to look in here and
4:36 maybe see if there's something we want to put in here in terms of logging at a lower level just to show how we can work with
4:43 different parts of our application.