Python Tutorial: Logging Basics – Logging to Files, Setting Levels, and Formatting
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/04/1711980026_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: 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 in 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 um 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 Wherever It would be useful um Python comes with a logging module built in Uh 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 Uh 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 Uh 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 ok So with that said Let’s go ahead and get started Uh 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 10 and 5 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 Uh 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 Ran that code We can see that the values that we printed 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 ok 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 grab this 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 Uh Maybe low dis 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 fun 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 warning error 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 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 then 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 Uh log these debug and info statements so instead we need to change Uh so First of all Let’s just say that If WE change these debug 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 um 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 log here Okay so we’ve logged out some Lines to the console but We’re not really using loging 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 Uh 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 basic config now This is Camel case And now to change that level We’re going to say level is equal to logging debug now when you set that logging level realize that this debug Here is in all caps now That’s different than our logging debug method um which We Used earlier this logging 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 infos 20 warnings 30 errors 40 criticals 50 and so on So now that We have our logging level set to 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 warning and instead I’m going to say logging debug And now Let’s go ahead and rerun this ok So now We can see that It’s printing our debug statements there in the console now So this is good so far But 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 logged 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 in 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 keywords ok 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 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 log now You can see that it has our previous values that Ran and the Uh the latest run here So now We’re going to be able to see our logged information 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 um now to change the format in our log we need to add in some special values to our basic config now to view What is 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 Uh Human readable Time We have the file name the level name Uh the Line Numbers the messages and things like that so Let’s say that we wanted to change our logging format to be the time and the level name and then the message So I’m just going to go ahead and cop each 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 in 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 name format I’ll copy that and paste that in separate that with a colon and Now we also want the 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 Uh dealing with millisecond data and then We have the log level Which is debug and then We have our message that we printed out ok 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 then Don’t worry about it too much Uh 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 small employee class here and when we create An employee it Comes in here and sets the first name and the last name Uh 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 ok 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 Uh so We’re going to go through the same process that we did in the other Uh 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 loging basic config and then within here we’ll do a file name we that file 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 um Let’s say um Let’s set this equal to info I think that the Uh our employee creation would be a good info log level And lastly now Let’s go ahead and set the format for Uh 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 Uh 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 I’ll 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 login configured here now Let’s replace our PR statement with a log info call so instead of print I’m just going to say logging 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 we’ll just call this call her Jane do save that ok So now If I run this code then nothing popped up in our console but Over Here in the file system We have this emplo log I’m going to open that Up And Here You can see that We have all of our logged information Uh 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 Uh 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 um also How to log to a file and also How to format those log 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 Uh because they all try to Share the same logger um so in the next video we’ll learn how to create separate loggers um add 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 comments section below And I’ll do my best to answer those Now If you enjoy these tutorials and would like to support them then 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 Uh 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
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Super video, thank you Corey 😊
This is awesome. thanks for sharing the basics of logging. Straight to the point. This is why i subscribed. I have never been disappointed.
bro went straight to jane doe🤐
thanks! 🙂
I might as well add you as an author to my thesis :')
6 years and the only good video tutorial on logging, that's kinda sad
You're really good!
I wish you were my professor at the university. Thanks for the great content.
Your tutorials are very useful. Thank you very much.
Still the best tutorial on Youtube about python logging, straighforward and practical, Hats Off Sir.
pls zoom in youre text editor
God
Thank you so much Mr. It was so easy to understand and you helped me out a lot.
Love your man! thanks for the straight forward video!!!!!!
Thanks brother , this helped me a lot.😇
How did you highlight duplicated codes and edit them all at once?
thanks!
The line:
logging.basicConfig(level=logging.DEBUG)
apparently doesn’t work on Python 3.9.12 – I get a trace back with:
…
AttributeError: partially initialized module ‘logging’ has no attribute ‘basicConfig’ (most likely due to a circular import)
In fairness, I also tried putting this line after:
If __name__==“__main__”:
But I still get:
…
AttributeError: module ‘logging’ has no attribute ‘basicConfig’
When I run it, though it no longer complains about the module being partially initialized. Interestingly, autocomplete (in VS Code) does show ‘basicConfig’ as a viable option after typing, “logging.b”.
Thanks for the video.. it's quite clear
I keep getting a FileNotFoundError: [Errno 2} No such file or directory: 'file path'
I want the log file to save to a specific directory, but the directory is there I made sure of it. my path is something like this: r'C:Userswadedesktoplogs' + '/' + str(date) + ' log.log'
Am I receiving this error because of the file name after I enter the directory? I have other scripts that do the exact same thing and the log configuration is set up the same way so I don't understand why this isn't working. Please help!
Big fan of your videos Corey! Great job putting 'em together!
No joke, thank you for using really simple functions "add","subtract",etc. It makes it easier for us newbies!
I really hate programming but Python made me realize that programming isnt really that hard specially when you have guidance with these amazing youtube channels specially Coreys channel.. cheers!
Excellent teacher of python and OOP concepts…crystal clear!
Great material as always. Thank you for taking the time to make these videos!
Great tutorial! Simple, easy to understand, useful info. Thank you!
If I try to learn about Python in Youtube, you are always here. lol
Very helpful! Thank you!!!!
great clear video thank you
on spyder after copying and paste code doesn,t create anything is something wrong with spyder @Corey
the best
make videos with white theme
I been watching these wonderful videos, but I wonder if he's still around.