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