Python Logging Example | Python Tutorial: Logging Basics – Logging To Files, Setting Levels, And Formatting

Corey Schafer

Subscribe Here





Python Tutorial: Logging Basics - Logging To Files, Setting Levels, And Formatting


Hey, there, how’s it going? Everybody in this video, we’ll be going over the basics of logging and Python. Now there’s a lot that we can do with logging in Python, so I’m actually going to break these up into two separate videos. Now in this video we’re going to look at how to get started with logging by replacing our print. Statements with log statements we’re also going to set different logging levels and also log information to files now in the next video, We’ll look at how we can use loggers throughout multiple modules and how we can configure the levels so that different information gets sent exactly where we want it to go. Okay, so first of all, if you’re not using logs right now, then you should definitely start trying to work it into your code. Where ever it would be useful? Python comes with a logging module built in so there’s. No need to install anything extra. A lot of people put off learning good logging habits because it’s easier to sometimes just put in print statements and catch your problems on the fly. And there’s nothing really wrong with that. I mean, print functions do have their place for sure, but logging is definitely something that you’ll need to have in place. Once your application grows beyond a basic project. Having good logs in place is going to allow you to, you know, look at behavior and errors over time and give us a better overall picture of exactly what’s going on, and you can also pipe it into some visualization software to get a better perspective. Okay, so with that said, let’s go ahead and get started, so I have a file here with some very super simple functions, and these aren’t really useful for anything, but I wanted to start off with something easy so that we can focus on logging instead of what the code is doing, so I have four simple functions here, just add, subtract, multiply and divide and down here at the bottom. I’m using two number variables, and these are set to ten and five for now and then I’m running through each of these functions, and then I have some print statements here that are commented out. Okay, so if I just run this code as is, then it runs successfully with no errors, but we don’t really know if it did everything correctly, so in order to print something out to the screen here, let’s uncomment out all of these print statements and let’s go ahead and rerun that so after we reran that code, we can see that the values that we print it out all showed up in our console down here and everything looks correct now. This is probably how a lot of us check our work as we go. But instead of using print statements, let’s look at adding some very basic logging into this script. Now first of all, we’re going to have to import this and logging is a built-in module. So all we have to do is just do import logging, and you don’t need to install anything to get that to work. Okay, so before we log anything out. Let’s talk about logging levels now logging levels. Allow us to specify exactly what we want to log by separating these into categories. Now there are five standard logging levels and these are debug info, warning error and critical. And I have these pulled up in my snippets here so that we can see a description of each of these, and I’m just going to copy and paste this into our file here, and I just grabbed this description directly from the Python documentation, so we can see here that Debug says that this is detailed information typically of interest only when diagnosing problems info is confirmation that things are working correctly or working as expected, warning is an indication that something unexpected happen or is indicative of some problem, maybe below this space and the software is still working as expected. Then we get down here to our errors and due to a more serious problem. The software has not been able to perform a function and then critical is a serious error, indicating that the program itself may be unable to continue running so. I’m just going to leave. These commented here as we’re walking through this video so that we can have that as a visual guide. Now the default level for logging is set to warning and what that means is that it will capture everything that is a warning or above so by default, it will log learning err and critical, and that means that it’s going to ignore debug and info log statements, so for example, down here at the bottom where we have these print statements, let’s say that we wanted to turn these into debug logging statements, So I could just say instead of print here. I’m just going to say logging Dot debug and save that now. The default behavior for these logging statements is just to log these to the console. So right, now it’s very similar to our print statement functionality. So now if I run this, you can see that it doesn’t log anything to our console. And that’s because the default logging level is warning and it’s only going to log out warnings and higher, so it’s not going to log these debug and info statements so instead we need to change so first of all, Let’s just say that if we change the debugs here to warning instead and run that now you can see that we get some output to our console now. This has a little more information than our print function, so this tells us our logging level, which is set to warning. It also. Has this root here now? Don’t worry about that root right now. And what that means we’re going to look at that in our next logging video, and now we have the message that we passed into our logs here. Okay, so we’ve logged out some lines to the console, but we’re not really using logging correctly right now, because we have these set as warnings and they’re not really warnings. We really wanted to log these lines as either debug or info, so we need to change our logging level to log those debug and info statements out, so do this. Let’s go up to the top of our file and we’re going to change some basic configurations here, and I’ll just do this below our comments and we can change these basic configurations with the basic config method, so I’m going to say logging Dot basic config. Now this is camelcase and now to change that level, we’re going to say level is equal to logging Dot debug. Now when you said that logging level realize that this debug here is in all caps now that’s different than our logging dot debug method, which we used earlier. This logging Dot Debug is all caps and it’s just a constant. That’s actually an integer in the background, and if I remember correctly, I think that all these constants are increments of 10 so I think Debug is actually just 10 Info’s 20 warnings, 30 years, 40 Criticals, 50 and so on so now that we have our logging level set the debug here. Now, let’s change all of our warnings back to debug, so I’m going to go down here where we did logging Dot warning, and instead I’m going to say logging Dot Debug, and now let’s go ahead and rerun this. Okay, so now we can see that it’s printing our debug statements. They’re in the console. Now, sir! This is good so far now you may have come to this video. Expecting to see actual log files and not just printing information to the console and log files are a great way to capture information because it allows us to see our log information over time in one place. So instead of logging this information to the console, let’s instead create a log file now to do this. It’s just as easy as specifying a file and our basic config method. So the keyword that we’re going to use here is file name and set that equal to whatever we want. Our log file to be and I’m just going to call this test log for now, and we also need a comma there to separate those key words. Okay, so now let me rerun this again now. Nothing showed up down here in our console now. I’m not sure if you can see my file system very well up here. But this new file appeared Test Dot Log and I’m going to go ahead and open this up and you can see that it logged all of the information from our script. So now if I go back to my script and I change these numbers. So instead of 10 and 5 I’m just going to use 20 and 10 and now if I rerun this and then open back up this test dot log now you can see that it has our previous values that ran and the the latest run here so now we’re going to be able to see our logged information over time and over multiple runs. OK, so one more thing that. I want to show you in this. Basic logging video is how to change the format and what I mean by the format is. If we look back at our log file here, you can see that it has the log level and the logger, which we said that we weren’t going to cover in this video. We’ll cover that in the next video, and then it has the message here now to change the format and our log. We need to add in some special values to our basic config now to view what is available to us. I have the options pulled up here over here in my browser now. These special format codes right here are what is going to allow us to change our format and you can see a description of each of these, so we have the human readable time. We have the file name, the level name, the line numbers, the messages and things like that, so let’s say that we wanted to change our logging format to be the and the level name and then the message, so I’m just going to go ahead and copy each of these so first I’ll go ahead and grab the time here and copy that and now to set this formatting. I’m going to go back to my sample file back up here to the top and then my basic configuration. I’m just going to add in another keyword and that next keyword is format and I’m going to set this equal to. I’m just going to paste this in what we copied over for the time and then I’m going to separate that with a colon there, and now I’ll go back over and grab the level name. So we want the level name format. I’ll copy that and paste that in separate that with a colon. And now we also want a message, so I’ll grab the message and go over and paste that in. So now if I save this and I run that, and now I go back to my log file. Now you can see that the format down here with our latest run. The format of our logs has changed, and now we have the time and this comma here is just dealing with millisecond data and then we have the log level, which is debug. And then we have our message that we burn it out. Okay, so now let’s take a look at another module and add some logging to it real quick, just so that we can walk over all this one more time a little bit more quickly, so the module that I have here is one that I pulled from my object-oriented tutorial series. Now if you don’t know how classes work or exactly what this code is doing and don’t worry about it too much we’re just going to use this as an example to add some logging, so just a quick overview of what’s going on. I have this really small employee class here. And when we create an employee, it comes in here and sets the first name and the last name on the instance, and then we’re printing out that we created an employee and then down here at the bottom we’re actually creating two instances of two new employees. So with this print statement in here, it should tell us when these two instances are created. So I’m going to go ahead and run this code and you can see that it tells us that our two instances were created and we have our print message there. Okay, so now let’s go ahead and add some logging. Let’s say that we wanted to see you know what employees were created over time, so we’re going to go through the same process that we and the other module, except we’re just going to go a little faster here, so we’ll go ahead and import logging and then we’ll set up our basic configuration. I could copy this over from the other file. We’ll, just go ahead and type this in real quick, so I’ll say logging basic config and then within here, we’ll do a file name and we’ll set that file name equal to employee log. Now that’s the log file that we’re going to log to. Now for our level here. Remember that the default is set to warning. Let’s say, let’s set this equal to info. I think that the our employee creation would be a good info log level, and, Lastly, now let’s go ahead and set the format for our logs so to do this. Let’s do format equals and within this string now this one. I am going to grab from the other example since these formatting codes can be a little confusing now. I’m just going to go ahead and grab the level name and the message here, so Ill. Copy that and just paste that in there. Now, remember if you want to see everything that is available to you here. Then they do have this in the Python documentation. It is up here in the Python. 3 logging library documentation down here at Log record attributes. OK, so now that we have our logging configured here now, let’s replace our print statement with a log info call so instead of print. I’m just going to say logging Dot info and pass in the exact same message and just to make sure that this is working with new information. Let’s go ahead and create one more employee down here at the bottom. I’ll call this employee 3 and well. Just call this call her Jane. Doe, save that, OK? So now if I run this code and nothing popped up in our console, but over here in the file system. We have this employee log. I’m going to open that up and here you can see that we have all of our logs information at the info level and the custom formatting that we added in OK so. I think that is going to do it for this video. We’ve gone over how to use basic logging. Instead of print statements, we also learned about the different log levels and how to change those levels also how to log to a file and also how to format those long messages so for small applications logging like this will be a good start, but there are some issues that we can run into once we start importing our other modules because they all try to share the same logger. So in the next video, we’ll learn how to create separate loggers, ad handlers and formatters to those loggers and also how we can log our information to multiple locations and I’ll leave a link to that next video in the description section below. Now, if anyone has any questions about what we covered in this video, then feel free to ask in the comment section below. And I’ll do my best to answer those. Now if you enjoy these tutorials and would like to support them, and there are several ways you can do that. The easiest way is to simply like the video and give it a thumbs up, and also it’s a huge help to share these videos with anyone who you think would find them useful. And also if you have the means you can contribute through Patreon and there’s a link to that page in the description section below, be sure to subscribe for future videos. And thank you all for watching.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more