Logging Implementation In Python

Krish Naik · Advanced ·☁️ DevOps & Cloud ·5y ago

Key Takeaways

The video demonstrates logging implementation in Python using the logging module, covering topics such as log levels, handlers, and formatters. It provides a comprehensive overview of logging best practices and techniques for effective logging in Python applications.

Full Transcript

hello all my name is krishnak and welcome to my youtube channel so guys today in this particular video we are going to discuss about logging mechanism in python whenever probably you are developing a web application you may be developing a data science project application how easily you can basically logs now why logging is important because understand guys you handle some of the errors with the help of exceptional handling and we have already seen in python how to do the exception handling you can refer my complete python playlist for that the main thing is that when you are actually handling the exception it is also better that you try to capture those logs what is the error that are coming and probably save it in some kind of text file which in turn saves that information in some kind of database and probably try to see in a visualized manner you know like how many errors are actually coming how many warnings are actually coming how many critical errors are there and based on that you know when the system is in process you know it always helps us to monitor that entire results so in this particular video we are going to see how we can perform a logging in a much more easier way and here what i'm going to do is that in order to use logging you have to use this library which is called as import logging now here i've just used import random i don't require this so this logging library actually helps you to write logs probably create a text file create different types of files and try to write all the logs inside this now here what i'm going to do is that i'm just going to run a simple basic code again there is no much logic over here but i really wanted to show you some kind of understanding like how you can create a file how you can basically write all the logs inside that file and all so follow all what we have to do is that we have to set up the basic config information this basic config information basically says that what kind of file name i'm probably trying to create you know then what is the file mode file mode basically means that whether it is an append mode to write mode you know or right bytes mode uh right plus append mode so this kind of file modes we can actually give and this is basically the format now this format represents a lot of information so they are information like first of all this ase time basically shows that the timing like what kind of time it is basically a printing inside this particular text file with respect to the logs the second name is second parameter over here you can see something called as level name now level name over here actually shows you that what kind of error it is probably it is a warning error it is a critical error it is a error uh error mode basically so inside this right inside the logging object that we have a logging library that we have you know we have something like warning function critical function and error function this information gets captured inside this level name before going ahead with respect to this particular video this video has been sponsored by an academy so an academy has come up with this interview preparation batch which is starting on 24th feb especially for intermediate level folks which will help you get ready for the upcoming placements this is being taken by ria bunsel who herself is a techie in flipkart and will be able to guide you how to crack such interviews we also have the pagar who's the icpc world finalist for 2020 and his work at app time linux apart from this guys they're also some of the advantages of taking the unacademy subscription first of all when you take up a subscription you get access to all the batches and courses that are running currently and also the ones that have happened in the past so it is not a particular batch or a course that you are buying but access to all the paid courses you can also learn from your favorite teachers and be a part of all the courses and batches that are taken by them dedicated doubt session will be available so that you can asked out in live session additionally there is a doubt support on discord educators and reaching assistant solved doubts you also have an option to interact with your teacher through comments and emojis also have lives quizzes and polls in the class so check out the educators and different batches that are going on and the free classes made available for you by the court chef and an academy so apart from this guys you'll also be able to use my code that is kn06 to get 10 off on any subscriptions so all the information regarding this will be given in the description of this particular video and finally we have our message message is all the message that we are actually trying to print inside that particular log message right so here uh i'm also going to use a date format this date format is going to get printed in this ase time okay now here i have written a simple logic where i've said that for i in range 0 comma 15 i'm saying that if i percentile 2 is double equal to 0 i am just saying that log logging dot warning i'm just displaying some warning sim warning message you know i probably want to log some of the warning message over here right and over here you can see that if you have i percentile 3 is equal to 0 then i'm saying that okay it is a critical message no such logic guys just to show you how that particular message is printed i'm just writing this simple if else condition and finally in the else i'm just saying that okay if with other numbers other than that i percentile 2 is equal to 0 i percentile 3 is equal to 0 take all the other values as errors okay now this is what i have actually done right now suppose i say that if i divided by zero suppose there may be a scenario that my i value may also be zero right or probably i can say that i'll try to do some kind of operation where i'll say i divide by zero and for this you know we do some kind of exceptional handling like division by error and whenever we do this what i can do is that i can also capture this inside my try catch block like this right so try uh this will be my operation inside this like this okay right suppose i say i is equal to i equals i divided by 0 and then i can also write my except block and probably you know inside this exit block i may write logging dot critical error i'll say error and here probably i will say division by zero error right so something like this i can definitely do this right definitely i can do this and based on that that kind of logging information i can put it i'll try to show you this example also uh so what i'm going to do is that i'm going to remove this and now i'm just going to execute this entire code let's see what will happen and how it will run whether we'll be able to create our text chris.txt file and probably will be able to put up all these particular messages so let's do it i'll go this i'll just click on run main and after this i have my chris.text now here you can see uh 15 times because i'm iterating through 15 times right in my main.py so here you can see i for i in range 0 comma 15 and you know for this it is displaying log warning message whenever this condition gets satisfied it is displaying log warning then it is displaying log critical and then it it is probably displaying log error message this is very much important guys because whenever you are working on a very important project probably you really have to write this kind of logging mechanisms you know this is the first main thing so if i go and see in my cryst text you can see all the messages over here right lock critical error warnings everything is basically shown over here now what i'm going to do over here instead of writing like this probably i'll say that okay what i can say is that suppose i initialize i is equal to 10 right and probably i'll say i'll try to try create a try catch block if i say i is equal to i divided by 0 just to show you that division my error problem and here i'm just going to write my accept block and here i will probably handle this particular exception by saying that log logging dot error and this will basically have my essays message saying that division by zero right division by zero issue or error right i'll just write it out like this now this looks also pretty much fine uh and let me just run this uh and let's see how it will run so here you can see that it has successfully run if i go and see my text now here you can see division by zero error it is automatically logged over here with all these time parameters now see this guys we we selected this as our axi timestamp then this was our level name level name was either error warning or critical and this is basically the message that i'm trying to print isn't it amazing right because whenever you are developing a bigger application some of the other issues may come at various places and we should always be ready to handle that by using try catch block but when we are trying to handle that exception we should also be printing our logs in some text file probably putting inside a database and not right so this was a small idea about logging mechanism and python now what i'm going to do in my upcoming videos is that i'll take up entire logs of this probably use a database print put all these logs in transformation inside that database then pull all those information try to create a dashboard where you'll be able to see that how many critical messages have come how many error messages have come or how many warning messages have come in a visualized format and probably send that particular uh report to your stake manager stakeholders and all or your managers will be very much interested to see this kind of images and all so i hope you like this particular video in the next video i'll try to do that please do subscribe the channel if you're not a subscriber i'll see you all in the next video have a great day thank you and bye

Original Description

Join this structured, industry relevant and guided program to Crack those upcoming Interviews. Check the Batch Trailer and Course Plan by clicking on the link. ELITE: Master Advanced Programming Topics and Become an Expert: https://unacademy.com/batch/elite-master-advanced-programming-topics-and-become-an-expert/PHPO27EQ Access all the free live classes in the current week here: http://unacademy.cc/daily_learning Use Code KN06 to get 10% of on subscription ⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I've been using Kite for a few months and I love it! https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=krishnaik&utm_content=description-only All Playlist In My channel Interview Playlist: https://www.youtube.com/playlist?list=PLZoTAELRMXVM0zN0cgJrfT6TK2ypCpQdY Complete DL Playlist: https://www.youtube.com/watch?v=9jA0KjS7V_c&list=PLZoTAELRMXVPGU70ZGsckrMdr0FteeRUi Julia Playlist: https://www.youtube.com/watch?v=Bxp1YFA6M4s&list=PLZoTAELRMXVPJwtjTo2Y6LkuuYK0FT4Q- Complete ML Playlist :https://www.youtube.com/playlist?list=PLZoTAELRMXVPBTrWtJkn3wWQxZkmTXGwe Complete NLP Playlist:https://www.youtube.com/playlist?list=PLZoTAELRMXVMdJ5sqbCK2LiM0HhQVWNzm Docker End To End Implementation: https://www.youtube.com/playlist?list=PLZoTAELRMXVNKtpy0U_Mx9N26w8n0hIbs Live stream Playlist: https://www.youtube.com/playlist?list=PLZoTAELRMXVNxYFq_9MuiUdn2YnlFqmMK Machine Learning Pipelines: https://www.youtube.com/playlist?list=PLZoTAELRMXVNKtpy0U_Mx9N26w8n0hIbs Pytorch Playlist: https://www.youtube.com/playlist?list=PLZoTAELRMXVNxYFq_9MuiUdn2YnlFqmMK Feature Engineering :https://www.youtube.com/playlist?list=PLZoTAELRMXVPwYGE2PXD3x0bfKnR0cJjN Live Projects :https://www.youtube.com/playlist?list=PLZoTAELRMXVOFnfSwkB_uyr4FT-327noK Kaggle competition :https://www.youtube.
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Krish Naik · Krish Naik · 0 of 60

← Previous Next →
1 Natural Language Processing|Stemming
Natural Language Processing|Stemming
Krish Naik
2 Natural Language Processing|BagofWords
Natural Language Processing|BagofWords
Krish Naik
3 Gaussian distribution or Normal Distribution in statisctics
Gaussian distribution or Normal Distribution in statisctics
Krish Naik
4 Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Krish Naik
5 Log Normal Distribution in Statistics
Log Normal Distribution in Statistics
Krish Naik
6 Covariance in Statistics
Covariance in Statistics
Krish Naik
7 Confusion matrix, Precision, Recall| Data Science Interview questions
Confusion matrix, Precision, Recall| Data Science Interview questions
Krish Naik
8 Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Krish Naik
9 Implementing a Spam classifier in python| Natural Language Processing
Implementing a Spam classifier in python| Natural Language Processing
Krish Naik
10 Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Krish Naik
11 Face Recognition using open CV and VGG 16 Transfer Learning
Face Recognition using open CV and VGG 16 Transfer Learning
Krish Naik
12 Pedestrian Detection using OpenCV from Videos
Pedestrian Detection using OpenCV from Videos
Krish Naik
13 Face and Eye Detection from Videos using HAAR Cascade Classifier
Face and Eye Detection from Videos using HAAR Cascade Classifier
Krish Naik
14 Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Krish Naik
15 OpenCV Installation | OpenCV tutorial
OpenCV Installation | OpenCV tutorial
Krish Naik
16 Face and Eye Detection from Images using HAAR Cascade Classifier
Face and Eye Detection from Images using HAAR Cascade Classifier
Krish Naik
17 Car Detection using HAAR Cascade and Opencv from Videos.
Car Detection using HAAR Cascade and Opencv from Videos.
Krish Naik
18 Using OpenFace for Face recognition in Keras
Using OpenFace for Face recognition in Keras
Krish Naik
19 OpenPose Tutorial with Tensorflow
OpenPose Tutorial with Tensorflow
Krish Naik
20 Multiple Linear Regression using python and sklearn
Multiple Linear Regression using python and sklearn
Krish Naik
21 Dimensional Reduction| Principal Component Analysis
Dimensional Reduction| Principal Component Analysis
Krish Naik
22 Movie Recommender System using Python
Movie Recommender System using Python
Krish Naik
23 TPR,FPR,FNR,TNR, Confusion Matrix
TPR,FPR,FNR,TNR, Confusion Matrix
Krish Naik
24 Precision, Recall and F1-Score
Precision, Recall and F1-Score
Krish Naik
25 Artificial Neural Network for Customer's Exit Prediction from Bank
Artificial Neural Network for Customer's Exit Prediction from Bank
Krish Naik
26 GridSearchCV- Select the best hyperparameter for any Classification Model
GridSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
27 RandomizedSearchCV- Select the best hyperparameter for any Classification Model
RandomizedSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
28 K Nearest Neighbor classification with Intuition and practical solution
K Nearest Neighbor classification with Intuition and practical solution
Krish Naik
29 K Means Clustering Intuition
K Means Clustering Intuition
Krish Naik
30 Create custom Alexa Skill- Lambda function- Part2
Create custom Alexa Skill- Lambda function- Part2
Krish Naik
31 Hierarchical Clustering intuition
Hierarchical Clustering intuition
Krish Naik
32 Implement Transfer Learning with a generic Code Template
Implement Transfer Learning with a generic Code Template
Krish Naik
33 Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Krish Naik
34 Unlock Your Application With Your Face using OpenCV
Unlock Your Application With Your Face using OpenCV
Krish Naik
35 Draw rectangle from webcam and sketch process it on a live feed
Draw rectangle from webcam and sketch process it on a live feed
Krish Naik
36 Complete Life Cycle of a Data Science Project
Complete Life Cycle of a Data Science Project
Krish Naik
37 How we can apply Machine Learning in Finance
How we can apply Machine Learning in Finance
Krish Naik
38 Deep Learning in Medical Science
Deep Learning in Medical Science
Krish Naik
39 How to switch your career to Data Science.
How to switch your career to Data Science.
Krish Naik
40 Linear Regression Mathematical Intuition
Linear Regression Mathematical Intuition
Krish Naik
41 Handle Categorical features using Python
Handle Categorical features using Python
Krish Naik
42 Machine Learning Algorithm- Which one to choose for your Problem?
Machine Learning Algorithm- Which one to choose for your Problem?
Krish Naik
43 DBSCAN Clustering Easily Explained with Implementation
DBSCAN Clustering Easily Explained with Implementation
Krish Naik
44 Curse of Dimensionality Easily explained| Machine Learning
Curse of Dimensionality Easily explained| Machine Learning
Krish Naik
45 Feature Selection Techniques Easily Explained | Machine Learning
Feature Selection Techniques Easily Explained | Machine Learning
Krish Naik
46 Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Krish Naik
47 Cross Validation using sklearn and python | Machine Learning
Cross Validation using sklearn and python | Machine Learning
Krish Naik
48 Handling Missing Data Easily Explained| Machine Learning
Handling Missing Data Easily Explained| Machine Learning
Krish Naik
49 Deploy Machine Learning Model using Flask
Deploy Machine Learning Model using Flask
Krish Naik
50 Deployment of Deep Learning Model using Flask
Deployment of Deep Learning Model using Flask
Krish Naik
51 How to Visualize Multiple Linear Regression in python
How to Visualize Multiple Linear Regression in python
Krish Naik
52 K Nearest Neighbour Easily Explained with Implementation
K Nearest Neighbour Easily Explained with Implementation
Krish Naik
53 Predicting Heart Disease using Machine Learning
Predicting Heart Disease using Machine Learning
Krish Naik
54 Predicting Lungs Disease using Deep Learning
Predicting Lungs Disease using Deep Learning
Krish Naik
55 Stock Sentiment Analysis using News Headlines
Stock Sentiment Analysis using News Headlines
Krish Naik
56 Random Forest(Bootstrap Aggregation) Easily Explained
Random Forest(Bootstrap Aggregation) Easily Explained
Krish Naik
57 Voting Classifier(Hard Voting and Soft Voting Classifier)
Voting Classifier(Hard Voting and Soft Voting Classifier)
Krish Naik
58 Credit Card Fraud Detection using Machine Learning from Kaggle
Credit Card Fraud Detection using Machine Learning from Kaggle
Krish Naik
59 Hyperparameter Optimization for Xgboost
Hyperparameter Optimization for Xgboost
Krish Naik
60 Tutorial 45-Handling imbalanced Dataset  using python- Part 1
Tutorial 45-Handling imbalanced Dataset using python- Part 1
Krish Naik

This video provides a comprehensive overview of logging implementation in Python, covering topics such as log levels, handlers, and formatters. It demonstrates how to configure logging in Python applications and provides best practices for effective logging. By the end of this video, viewers will be able to implement logging in their Python applications and configure logging handlers and formatters.

Key Takeaways
  1. Import the logging module
  2. Configure log levels
  3. Create handlers
  4. Use formatters
  5. Configure log rotation
  6. Integrate logging into applications
💡 Effective logging is crucial for application monitoring and debugging, and Python's logging module provides a flexible and customizable way to implement logging in applications.

Related Reads

Up next
Hostinger Web Hosting Review 2026: Features, Pros & Cons 🔥
DroidCrunch
Watch →