Hello guys.
So we are going to continue the discussion with respect to Python.
Already in our previous video we have seen about Sqlite3.
Now in this video we are going to talk about like how you can work with logging specifically in Python.
Now logging is a very important concept.
Whenever you are creating an end to end project, it is very much necessary.
You have to probably log each and every thing that is happening with respect to the execution.
Whether you are getting a warnings, whether you are getting errors, everything needs to be logged
some or the other way so that whenever we debug the entire application, we'll be able to see what kind
of error, if any, error is occurring in the application, we'll be able to understand it.
So in this video I will be talking about logging itself.
I'll be showing you like many, many different types of example with respect to code.
And I hope by just seeing this video and afterwards when you follow the end to end projects, you will
be able to understand it.
Okay.
So quickly let's go ahead.
And now initially over here you can see logging is crucial aspects of any application providing ways
to track event errors and operational information.
Python built in logging module offers a flexible framework for emitting log messages for Python program.
In this lesson, we will cover the basics of logging, including how to configure logging, logging
levels, and the best practices for using logging in Python application.
So first of all we will go ahead and import logging over here.
Now whenever we basically import logging first of all we need to configure the basic logging settings.
Okay.
We need to configure this.
Now in order to configure it I will go ahead and write logging dot basic config.
Okay.
So basic config is a function.
And here we go ahead and set a level.
Right now with respect to level there are different different levels like it is having debug.
It has error right.
It has over here you can see error is also there.
So let's say that since I'm developing the application over here, I will just keep it to debug so that
I'm trying to put this particular basic settings to Logging.debug.
Right.
It's just giving an indication.
Hey, I am right now yet developing my entire application.
So this is the initial basic logging settings that you really need to do right now.
Whenever I want to probably do or log messages.
Right.
So I can go ahead and use different, different ways.
So first of all I will go ahead and use Logging.debug.
Let's say over here I go ahead and put a message.
This is a debug message okay.
Debug message over here okay.
And then similarly uh I've used Logging.debug here.
Also you have logging.info.
And here with respect to this I will also go ahead and write.
This is an info message okay.
Then you have logging dot warning.
Warning okay.
And here I will go ahead and write.
This is a warning message okay.
And finally here you can see I will also go ahead and write logging dot error.
Right.
We'll discuss about all these things.
Uh but these are some you can basically say logging message with uh.
Different with different severity levels.
Okay.
So if I really want to use different, different levels I can go ahead and use this.
So I will go ahead and like logging dot error.
And I'll go and say this is an error message okay.
Some logs I'm actually going to put along with this.
I will go ahead and write logging dot.
Uh critical.
So critical is one more uh level which we can specifically use.
And here I can go ahead and write.
This is a critical message okay.
So once I execute this here you can see that how it is putting all this information.
So I have actually kept this logging doc debug as my root level.
So here you will be able to see this is by default the date uh date time that you will be able to see
over here.
Then you have this debug first message.
This is a debug message.
Then you have info.
This is an info message.
So with respect to time you are able to get all this information.
So this is the default structure with respect to any logging message that is probably getting displayed
where you can use your date, your timestamp, and what is the route and what kind of level we are specifically
using.
Okay, so just one basic example, but if I go ahead and show you what are the different different levels.
So here I've actually written it down.
Python logging module has several log levels indicating the severity of events.
The default are debug detailed information, typically of interest only when diagnosis problem.
So let's say we are trying to diagnose some problem.
You want to put some kind of message after execution of some some functions or let's say within the
function itself.
Then you have this info confirmation that things are working as expected.
So we basically use this kind of, uh, level in case of warning, an indication that something unexpected
happened or indicative of some problem in the near future.
Okay.
Some of the examples are like display slow the stop, but you'll be able to see.
With respect to warning, the software will still be running right.
Your application will still be running.
Then you have error.
Due to your most serious problem, the software has not been able to perform some function and critical.
A very serious error indicating the program itself may not be or may be unable to continue running.
Okay, now this is fine.
Here we have done this.
Now let's focus more on configuring the logging.
Right.
So right now you'll be able to see by default I'm able to see some kind of date time over here and all
these messages.
But I can go ahead and configure this entire like how the logging message needs to be displayed.
So for this I will go ahead and configure logging okay.
Again here I'm just going to write logging and logging dot basic config okay.
And now inside this basic config I can put different different information.
One is level.
So let's say I will go ahead and write logging dot debug okay.
So this will be my first thing uh over here Logging.debug okay.
After putting this I also need to make sure that what format my date time stamp should be looking like.
So I will go ahead and write the format over here.
And with respect to format, I will be using a different characters over here.
So let me just go ahead and show you I'll use percentile.
Okay.
Uh, a s c time.
Right.
So I'll go ahead and write like this.
This is my time itself.
The first thing you'll be able to see.
Right.
Let me just go ahead and write this.
And here you'll be able to find out all the information.
Then I'll go ahead and write name.
Name you'll be able to understand okay.
Then based on this you can actually go ahead and give your own format.
Then this will basically be my level name.
What kind of level name is there okay with this S string.
And then you have this message okay.
Me SS age okay fine.
So this is what is the format I'm going to follow right now.
You will not be able to understand what exactly this is.
But uh, let's see, once we display the logs you'll be able to understand it.
Okay.
so just for another five minutes, then we are going to use my date format.
In the date format I specifically want different different dates.
Let's say I want in year format.
So I will go ahead and write percentile y.
Then I want month then I want day.
Uh, once I'm able to put this along with this, you'll be able to see that, uh, I'll also go ahead
and use percentile h hour.
This will be percentile m and then you will be able to see percentile s.
Right.
So this s is nothing but it is seconds.
Okay.
We are going to specifically use this.
So I hope you are able to see this entire format.
And this is how it looks like.
Okay let me just go ahead and execute it.
Uh it says logging has no attribute debug.
The simple reason is that I have to use this as a capital letter.
So this will be basically logging on DBA.
Now let's go ahead and execute it.
So this is my logging dot basic config.
Now what I'm actually going to do I'll just go ahead and take this entire message.
Different severity levels.
And let's go ahead and print this.
Okay.
So here you can basically see that I have set up the level format and date format, right.
Now if I go ahead and execute this right.
So I am basically getting all this information and I'm also getting the debug.
And this is the debug message with respect to the display.
Now let me do one thing right.
Because at the end of the day if we are logging we really need to save it in a file.
Or we probably want to log the entire logging details in my in a separate file altogether.
Right.
So what I will do over here quickly is that I will just go ahead and create a file name.
Okay.
So for that I will go ahead and use a parameter which is like file name.
And inside this let's go ahead and write app dot log.
Okay.
Now see the magic over here.
What will happen.
Okay.
So I will just go ahead and write comma.
Okay.
So once I probably go ahead and write my file name I will also go ahead and update one more thing that
is called as file mode.
And here I will say write mode.
Okay.
That basically means I will go ahead and probably write in this app dot log.
Now understand one thing guys.
Once you probably do the basic config setting, you can see that I have actually done the basic config
setting over here to Logging.debug, right?
And again, if I try to do the basic config setting, it won't work because previously we have done
this kind of setting.
So what I will do I'll just go ahead and restart my kernel okay.
And now directly I will go ahead and execute this message.
Now once I execute it, what is basically going to happen is that it is going to create an app dot log
file okay.
And inside this particular file you will be able to see that I will be trying to put some information
over here okay.
Uh, this entire debug message that you are able to see info warning error message, this all will be
getting appended inside this particular file.
So let me just go ahead and delete this app dot log.
Now you can see that I don't have any file over here.
Now let's go ahead and execute it over here.
So once I execute it this has got executed successfully.
If I go ahead and see this is my app dot log file.
Right.
So here you can see this again.
Let me just go ahead and execute once again.
And now any number of log messages that I go ahead and write right.
It should be able to write in that specific file, right.
So here you can see any number now based on the execution, whatever things are specifically executing
it.
If you are just using this log logging, dot debug info warning and whatever message you really want
to put in the logging file, you will be able to put it right.
So this is how simple.
With the help of this logging functionality in Python, you are able to do all these things right?
And uh, again uh, guys, uh, you will be facing an error or you will not be seeing any logs if I,
if you execute this and you do not restart it, because once we set up the basic config, unless and
until the kernel is not restarted, we cannot change the basic config.
And right now I've actually showed you where I have written file name, file mode and all everything
as such.
Now any packages that you specifically use you will be able to do it.
So let me just show you one example with respect to that type also.
So let's say I'm inside my logging folder.
I will just go ahead and create my folders like this.
Logs Okay.
And inside this logs let me create my logs.
Dot py or logger.py.
And this is how we are going to probably create it.
You know now with respect to logger.py I'll go to logger this one.
I'll go ahead and configure this entire thing with respect to my logging okay.
So once we do go ahead and configure it, uh, here you will be able to see that I am just going to
use the app dot log.
And again inside this logs folder only that folder file will also get created okay.
Now let's say I go ahead and create my another file which is like test.py okay.
And let's say inside this test.py I go ahead and import.
Okay.
Um before importing I will also do like this.
So I'll go ahead and import or I'll go ahead and write from logger okay.
Import logging okay.
So I'm going to basically use this functionality called as logging from this logger.py file okay.
And then now let me just go ahead and call this particular function let's say definition add.
And I'm just going to create two numbers or a comma B.
Okay.
And this will basically return okay a plus b.
Okay.
So here I can just go ahead and write like this.
And here I'll go ahead and write logger.
Sorry.
Logging dot debug.
Okay.
So debug will be my message.
The addition operation is taking place is taking place.
Perfect.
So this is just to log it in my app.
Dot log p app dot log file.
Okay.
So now I will just go ahead and call this.
And now let's go ahead and call this function where I am going to give my some values.
Let's say ten comma 15.
Okay.
And here also I will just go ahead and put one more log message just to show it to you whether we are
able to track it or not.
So Logging.debug here I'm just going to write the addition function is called.
Okay.
Just to give you an example.
Okay.
It's called.
Okay.
Perfect.
Now let's run this quickly.
In order to run it, I will open my terminal.
Make sure to go inside this path itself.
Right.
This test.py file is available in 12th logging in Python.
Inside that there is a logs folder.
Okay, so I will just go ahead and write CD 10th Data analysis okay.
This is data analysis.
Let's see 11th working with data phase.
Let's see 12th.
Let's see which is the folder path.
So 12th it is okay.
So here I will say CD 12th logging in Python.
And now let me clear my screen and just let me go ahead and run this Python test.py.
That is how we run right.
Uh, no such file or directory.
Okay, I have to go inside my logs cd.cd logs okay, CD logs.
Now I'll go ahead and run it.
So I'll go ahead and write Python test.py.
So here you can see it has got executed successfully.
I have not displayed anything, but if I go ahead and see my logs.
See inside this logs folder app dot log is created and here you can see addition function is called.
An addition operation is taking place.
So this entire information along with this route here you can see debug right.
Your day date time.
Everything is basically getting created.
Isn't it amazing.
And this is the main funda behind, uh, you know, the entire logging mechanism.
But always make sure that in logging you have to make sure you have to put your basic config.
And now after you put it in one file, you'll be able to use it anywhere just by importing it, which
I have actually shown you.
And just use the levels.
You can use similarity levels like debug error warnings, whatever things you really want, and you
can continue your execution.
So I hope you were able to understand this particular video.
This was it from my side and this was about logging.
Go ahead and practice it.
I'll see you all in the next video.
Thank.