Python's Logging Function

sentdex · Beginner ·☁️ DevOps & Cloud ·13y ago

Key Takeaways

Explains Python's built-in logging function

Full Transcript

hello and welcome to my tutorial concerning the logging ability of python which is actually built right on in so you don't have to download anything you just have to import logging uh once you began building uh larger programs incorporating a lot of variables it can be pretty useful to utilize logging the logging feature um also if you you're maybe like adding to the program over a long period of time and you might forget some of the older stuff that you had done even and all that and not quite remember how your program typically goes uh also if you're working like with a team it can be really really helpful like when you're collaborating to use logging just so someone can figure out what why something isn't quite driving with your um function or whatever um it is possible to do pretty much all of the same logging stuff uh with just you know saving and appending to a notepad file all the stuff that I've already shown you guys how to do um but what's nice about the logging feature is it has one built quote unquote levels or hierarchical or hierarchies um for you know what kind of log that string will be um so it kind of saves you a lot of time down the line uh if you want to change the amount of logging so to speak that you do because right when you make the program you're going to do what's called debugging and you're going to need to find you know exactly where everything's going wrong and so you're going to do either a lot of printing out or a bunch of logging to a file but then over time once you've you know uh worked out a lot of the bugs you're going to not want to be printing out a bunch of stuff or logging a bunch of stuff to a file and so that's where this logging hierarchy comes in then you can also kind of customize what level of logging you really want to do uh logging is also really helpful for applications that you deploy especially with things like beta testing so if the user is having problems they can simply turn on debugging mode and this will hopefully log everything that the user is doing and will'll find you exactly where the user is having a problem instead of having to use like process of elimination and guess work um and you can also like if you have some sort of like user settings uh you can have the triggering of a warning error or critical error log change the logging level that's set right so normally by default hit logs warning and above uh which is a good a good thing once you've finished your debugging that makes sense um but like say the user uh encounters an critical warning or something like that um then that can trigger the logging setting to drop down to level one debug and it logs everything again um so anyway it's just really helpful and um it's definitely something you want to have in your Arsenal as time goes on so let's go ahead and get into it uh all you have to do is import logging uh the next thing you'll you want to do is specify a file you don't have to save it to a file you can just print out on the console um but generally you're going to want to have a logging file so you do login. basic config this is you know basic configuration and here you'll specify the file name that you want to be the log file it could be anything you can have a text or whatever um but just for the sake of it we're going to call it log file . log and then you specify the level equals and what this is is is that level I was just referring to this is the level of logging that you want to do so when you first make the program it's probably going to be debug in all caps so this is going to log everything right and there are five levels of debugging and uh the first one level one is just debug and this is just a lot of detailed information the next level is two this is info and this is just confirmation uh that things are going according uh to plan the next one is is the third level and that's warning that's the default it'll be set to if you don't specify anything here it's just going to say warning that's going to be the level uh warning is is reserved for something unexpected has occurred uh number four is error some function failed and then finally number five is critical um something failed in the application uh must close because of this critical failure so those are the levels and you so right in here you can use these debug info warning error critical obviously written in all caps um to decide what level of stuff you want to do but for this uh we're just going to use debug um oh we need to close off our parentheses so once you do that now let's let's go ahead and make a main Loop we'll just Define main this we're going to have a try and we're going to also have an accept exception e all the stuff we've already uh done before then we'll call the main Loop here now let's put some stuff within the TR Loop like let's purposely fail this TR Loop so say you've got something like you're you've you're going to Define a variable we're going to call this math fail equals 1 / 0 and you can't divide by zero so this is obviously going to break but this is the main part I mean this is this is it for our main Loop so our whole main Loop's going to fail this means the application's definitely going to fail so in this exception down here you're going to want to do logging do critical and uh you'll just want to put in the string version of e um whatever that exception was but what you're with this you say logging so we know know we want to log this information if it's if it's at the correct level anyway and this is the level that we're calling it you could call this level warning but this is actually a critical error because the application will not run so we're saying this is a critical error uh please send it in so we'll save this and this is our logging here obviously there is no logging file but it will show up um just a moment whenever we begin our um our little program here so we'll go ahead and run it and sure enough we didn't actually see any error or anything like that but obviously we got an error so we'll close this and we'll come back we'll come over here uh to our log file and we're going to open this up and here you have a critical error root integer division or modulo by zero so division by zero um so it's it's logged in this little file for us that we had a critical error now let's say uh that was deeper or something in the um program so let's say try and since we're in the main TR Loop let's go ahead and log uh debug um we are in the main TR Loop that way if you do get an error you know the well what's the last thing we did well we entered the main TR Loop and then we failed so again if we were to run this we just ran it it failed oops made it big close it come over to our log file we open that up now keep in mind it's going to pend so this was the old lay um but so this is the new stuff right here debug rout we are in the main uh TR Loop so we can close that out U we're not going to save and let's go ahead and delete it and so we can start a new and then down here what we're going to do is we're going to say if one is greater than two uh logging dotinfo entered into the uh first if statement uh and then else logging dotinfo entered into the first lse statement right this is just we're just saying what what are we doing we're just making some info um this is where we are you could also just say this is also debug right like this is probably more likely to be debug information to be honest so you could say that and then we can you know do some things like print hello yeah these are awesome little functions going on so in this sense we can save this we'll run this one and nothing really happened but we'll come over to our log file here we'll open up the log file and you know okay we're in the main Loop still same thing as before couldn't divide by zero now let's do you know one more one more example and I think you'll probably pretty much have the uh get the point um so let's say we want to continue with this if statement if one is is less than two do these things print hello and then try um we'll try URL lib 2. open or yeah it's UR Ur L open is correct thing URL open HTTP google.com read we're going to yeah we're going to read that but oops we didn't import Ur lib 2 so finally accept now see in here this isn't going to this isn't something that's going to break the entire script um it could I suppose if you if you didn't have these try and accept here that would indeed break the entire script but uh in this case it's not going to break the entire script and the only reason here it did it did break the entire script is nothing in our script was able to run um so we called it a critical error but in this one I wouldn't really call this a critical error it's more like um what is called an error uh so you would say you know like logging error because this function whatever we were attempting to do here has failed but the rest of the script is going to continue working and everything else works fine but this is kind of messed up so you would just say logging error and then I would say um you know this probably we would just call it a function probably but we would say the URL of two um URL visit failed failed um for the reason of and then you use percent s we get we'll start getting really fancy and then we'll say say um oops not that this and then we're going to say percent uh E I guess we'll say string e this so it's going to log um this right going to say url li2 url visit failed for the reason of percent s which just means this some sort of string and then this is saying this string and then the string version of e so you know we've done this before with the how to work with exceptions and just print out that exception so we're going to pop back on over here and let's save this let's run it let it do its thing it's going to saved to that log file we're going to close out of this come over to our log file open up our log file ah and we didn't even get past it because of this stupid uh division by zero error so let's let's pop back over to our division by zero comment that out I was getting in our way before as well so we printed out our hello so we know we at least got this bar um you can also use the debugging uh to print to the console as well um so anyway we're going to close this out we'll come back over to our log file open it up and then we see in our log file that eror route url li2 url visit failed for the reason of and then this is the error that is saved to the exception right this is kind of like the stuff that would be printed out in red on your uh IDE so you can even save you know variables that are generated dynamically into your log file and with that I'm just going to show you uh let's say you're you you're pass the debug so like we only want to log warning now so we'll save this let's close out uh the uh this we'll just delete this come back to it in a second we'll minimize that save it run it print it out our hello probably got to ur lib to failed so let's bring that back up here's our log file open that up and sure enough the only thing it printed out for us it didn't print the debugging stuff it didn't print the info stuff it only printed out our error problem and which was of course the URL up to the visit failed because of this reason here so anyway um that's basically it to the loging function there's a few more um intermediate to experience details that you can use with this logging function but for the most part uh this is it this is what you want to use logging for it's definitely a great little addition to any big program that you've got and then also again if you're going to be deploying Out programs to use you definitely want to have logging brought into it and it also saves time uh as far as debugging purposes are concerned you know whenever you want to debug you can just boom flip this into debug mode instead of warning mode um and you're set

Original Description

Sentdex.com Facebook.com/sentdex Twitter.com/sentdex The basics of Python's built in logging / log abilities
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 3 of 60

1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related AI Lessons

Up next
How to Open KRP Files (Krita Document)
File Extension Geeks
Watch →