 Welcome back everyone. My name is Brian and in this video we're gonna talk about logging basics. Now, what is logging? We're not cutting down trees or anything. So so far we've been using the handy print function but logging is way way cooler than printing and it is kind of the de facto but we really haven't been able to have this conversation because we haven't covered all the technology we need to to build up to this conversation. So what makes logging cooler than printing? Well printing just prints something out on the screen, but logging you can have levels for example, you can say it's a debug message info warning error critical and some of these should be self-explanatory debug is just for you the developer you want to know what's happening info is just hey something happened you may want the user to see this you may not warning is hey something bad may happen if this continues or if it's not fixed error is pretty self-explanatory and critical is worse than error. So these are the different levels to start off with we are going to import the logging module. You could also do something like this from logging import and we're going to import the root logger. The reason why I add this is you need to understand that logging by default has a root logger and when I say root I mean it's the top level you can define other loggers custom loggers but we're going to be working with the top level logger okay so I'm going to just comment this out just so people don't get confused about which one we're working with we're just importing the entire module here and I'm going to work with basic logging notice how they do not get displayed by default this is the main takeaway of this segment this is going to be super confusing if you don't pay attention to that so I'm going to define a function we're going to use this over and over again it's going to just simply be test and we're going to print some lines and let's just do 20 of them why not and then I'm going to grab that give it the good old copy and paste that way all of this is going to be separated out when we print it now from here we want to do a few things I want to say logging dot and we want to debug now this is a little confusing because we're not actually debug anything we're saying logging make a debug message or more specifically we're going to message something with the severity of debug on the root logger remember we're talking about levels of logging here debug being the lowest critical meaning hey your program's just exploded and here's what happened so we're going to say debug message here and then we can take the same pattern and just you guessed it copy and paste time say info now informational are well just that information it's not really anything super important it's just hey the file was changed or something modified warning however this is when things are starting to get a little serious you ever give like your kid a warning hey you better go clean your room or else don't condone beating your child but sometimes you just got to make them clean the room so we've got debug info warning and as we go further further this number is changing there's a number in the background that we're going to really review later on now what's higher than a warning is you guessed it an error we talked about error handling and error conditions and things like that and that's exactly what we're doing here is we're saying hey some sort of error happened but using this will not throw an error we're just logging that an error occurred and last but certainly not least is critical when you hear the word critical in computers you need to believe that it is critical meaning everything you know has just ceased to exist and you need to focus on this or everything you know has just disappeared because it has died that's basically what critical means in computer lingo now we're just going to call this test function and see what happens notice how they do not all get displayed by default let's go ahead and run all we see is warning error and critical but where's debug and info to really understand this we need to set the level and when I talk about level we're talking about this debug info warning error and critical under the hood each one of these has a number assigned to each level think of it like levels of a building starting at the bottom going all the way to the top most important to least important okay so we're going to say level equals and I want to go logging we're going to use the logging module we're going to get the level name what this is going to do is convert that number in the background to a string representation but of course it's not that straightforward we now have to go logging get log error and what this will do is get the root logger which ironically we're already working with but the thing is you can specify a name so you can have different types of loggers that do different things we're just going to get the root logger and then we're going to say get effective level get effective level is going to return what level of logging we're currently working with here from here we're just going to print that out so I'm going to say print f and let's say log level all right so our log level is currently at warning what we're going to look at the next section is how to specify that level but what I really want to talk about is what the level really means what it means is warning meaning anything more critical is going to be included so we're going to do not just warnings but also errors and critical but we don't really care about debug and info and this is one of the little gotchas about the logging class by default is people will start with this and start doing debug messages and then nothing happens and they go well why and this is why we need to set the level now that we understand the concept of logging levels let's look at how we can manipulate them we're going to get and set the logging levels this allows really for filtering and the reason why you want to do this is in a typical application you're going to have a lot of information whipping around and you don't want to see all of that on the screen at once you only want to see what's important to you so first thing we're going to do here is we're going to get the root logger now the root logger is included by default but I want to actually go out and get it just to show you that we are getting an object so I'm gonna say root log equals logger or I'm sorry logging get logger see how that's a little confusing there now if you had multiple loggers you would have the name of the individual logger if you don't have a name it's just the root or the main logger here from here we're going to just grab this because this is just horrible looking I must say print and we're going to print the level I'm gonna paste that out here so we're saying logging get level name this is a little misleading because we're not working with the main logger we're working with the logging modules we're saying the module should get the level name now we want to get that from our root logger so effectively instead of saying this we can replace with that so logging module get level name of whatever log we're working with get the effective level and under the hood the level is just a number let's go ahead and run this so level warning I'm gonna actually put something in there to split that up so we know we're at the warning level and we've known that all along but now we're going to set it to debug now what does that mean set it to debug well it means we're going to include debug and anything more critical and since up at the top here debug is the top of the chain anything's going to be included not just debug so we're going to say root log set level and then we're going to say the logging module debug then we can just call our handy test function that has all those beautiful little loggers going on and run sure enough now we're seeing the debug info warning error in critical so it is working as expected i'm gonna grab this and we can very quickly start changing the level as needed so i'm gonna change this to critical only remember critical is well critical it means like something has exploded or is about to and we're going to run this and you can see logging level is now critical there we go it's pretty easy to understand here what's going on what you need to understand though is that the levels do not make those magically disappear it just means that we are not logging them or capturing them those messages are still being fired off okay now let's just do it one more and i want to set this to warning right back where we were and critical root critical message here you might be wondering what this root is what is this root well that is the actual logger we're working with that's why i've been calling this the root logger because we're working with the root logger if we had a different logger it would be the name of the logger right there it's actually pretty elegant the way this works and you can flip these around as you need to so if you're getting a lot of verbose information and you only want to see what's critical you can set it to critical then you're not getting warnings so then you set it to warning and now you're getting the appropriate level of information you need to see now as cool as this is what would be cooler is if we could log this directly to a file so as events happen it gets dumped directly into a file and that's exactly what we're going to do but i want to show you something that will just infuriate you time and time again if you go out to google and you type in how to use the logger you're going to get something that looks like this logging that basic config setting to a file name everybody gets all super excited file mode right yes this is going to work you can even define the format this is super cool so we're going to say the level and the message and then we're going to say level equal logging dot debug notice what this is doing this is setting the config it's setting some handlers and all this fun stuff so people get super super excited and they do something like this logging dot debug and we're going to send a debug of just hello so in theory when we run this logging is going to call debug with the message of hello it's going to create the file app dot txt in write mode and then it's going to write this out in this format and we know that because we're setting a logging debug so from role nothing happened that's right absolutely nothing happened so then people get very frustrated and they do something like google why is logging not logging that's literally one of the most popular google searches for this the reason why is very simple basic config will not work if we've already configured the logger it needs to be done before we take any action and so because we've already been working with the root logger and we've already configured it basic config does nothing that's right it does absolutely nothing is so frustrating so i'm just going to put in some notes and then i'm going to comment these out we're going to show you how to get around that now if this was actually the first line like if we had this way up here it would work beautifully but because we've done all this it's just simply not going to work so we need to get a handler i'm going to say logging dot and we want a file handler and all a handler does is it says how to handle the log information as it come in i'm going to say file dot log name it whatever you want really doesn't matter now we want to define a formatter for that handler and i'm going to say formatter and you notice these are constructors so yes under the hood we're actually creating classes now this is where i'm going to get a little copy and paste happy just because we'll be here all day watching me try to type this out what we're going to do is we're going to take the time string representation the name the level and the message if you want to what name is it's the logger name so it's going to say time logger name the level and the actual message and you can define whatever formatting you want to the guide is out in the official documentation for this point being once we have a handler and a formatter we're good to go from here we can say handler dot set formatter and that's going to tell the handler how to format the information it is handling then we can simply say root and we're going to say our root log dot add handler now what we can do is we can go in and say something like let me get this back up to debug level now we can set that to logging debug then we can say root log dot debug test save run and sure enough our file that log was created and it is in the format that we wanted it in this is extremely cool and we can even call our test function and because our log levels at debug and because we have set the handler in the formatter we can go back out to our file log and there is our information it is beautifully formatted so we have the date time we have the logger we have the level we have the message and you can see how it even color codes it for us this is just super impressive i hope you enjoyed this video you can find the source code out on github.com if you need additional help myself and thousands of other developers are hanging out in the void realms facebook group this is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched and if you want official training i do develop courses out on udemy.com this is official classroom style training if you go out there and the course you're looking for is just simply not there drop me a note i'm either working on it or i will actually develop it i will put a link down below for all three of those and as always help me help you smash that like and subscribe button the more popular these videos become the more i'll create and publish out on youtube thank you for watching