Hello guys.
So we are going to continue the discussion with respect to logging.
Now what are the things we have learned with respect to logging including multi logger.
We are just going to implement a simple application.
And then we'll be we'll try to log some information out there in some specific file.
So let's go ahead and let's do that.
So first of all this is my app.py file.
So I will go ahead and import logging.
And this time I'm actually going to do it in a py file itself.
Right now inside this I will first of all go ahead and do my basic logging settings.
So let's go ahead and do the logging settings over here.
And you know how to probably do the logging settings I have already shown you.
So let me just take one of the example over here, because again, I don't want to waste that entire
time in writing the same code.
And it's okay to do copy paste.
So here I'm actually writing my level.
So level will be logging dot debug the format.
And uh specifically the date format itself.
Right.
So all these three information is specifically there.
Now one more parameter which we can specifically work with because I can also go ahead and write my
file name where I really need to put all the logging information along with that.
And this parameter, I have not discussed it earlier, which is called as handlers.
Okay.
So here I'm going to define my handlers.
And this handler will be in the form of list.
First of all inside the handler I can go ahead and write logging dot file handler.
And this will basically be my file name right.
So let's say app one dot log will be my logging file name.
And this is how I can also go ahead and put it.
You can also go ahead and write directly file name over here.
File mode over there.
Right.
But here in with the help of handlers also we can use this.
So you just need to write logging dot file handler.
Along with this I have to make sure that whenever I get a logs right I need to probably keep on streaming
and writing in this app.
App one dot log file.
So for this I need to also initialize logging dot stream handler.
Okay, so stream handler is one method which will be responsible in putting all the logs information
inside this particular file.
Okay.
So this is my basic configuration setting that we really need to do.
Now what I'm actually going to do I'll go ahead and create my logger.
So inside this particular logger I'll just go ahead and write logging dot get handler by name okay.
So this sorry not get handler.
Let me just go ahead and call get logger right.
So that if you remember in Getlogger will specifically be used to create or call my module.
Right.
So for this entire, uh, project, I'm going to give my logger name right over here as arithmetic arithmetic
app.
Okay.
Arithmetic app.
So this will basically be my arithmetic app.
And this is my module name, the entire module name.
And with respect to this particular module I'm basically using this particular logger.
Okay.
Now let me just go ahead and add create some add function.
Let's say this will be a comma B.
And here I'm going to basically write result is equal to a plus b okay.
And now after probably creating that particular result I can return that result.
But before that I will just go ahead and write Logger.debug Logger.debug.
And inside this particular debug I can use my f string.
Okay.
And this will basically be me adding a and B or a plus b.
Okay.
Uh, and probably displaying this particular result that is nothing but result.
Okay.
So this is what we are basically going to put a put in our, uh, app one dot log file.
Similarly, I am going to do it for multiple things like this.
So I will go ahead.
And I've written my code over here.
So I will go ahead and do the same thing for subtract subtracting a minus b result.
Similarly for multiplication I'm just going to use this particular code.
So here you can see the result.
A star B.
And here I'm actually using a Logger.debug along with this I will go ahead and use divide a divide function
also.
Now with respect to this particular divide function I'm using a by b.
Whenever I give my b input.
I should also make sure that I, uh, use this division zero error because I can put this value as zero.
So whenever it probably comes, I'm going to write logger.debug with respect to dividing a by B.
Otherwise, if this particular exception is caught right inside this try catch block, I will go ahead
and write logger dot error logger is specifically the logger for this particular model.
That is nothing but arithmetic app.
Okay, and that is what we discussed in our multi-level logging, right?
Specifically that particular logger.
You can actually go ahead and use it.
Okay.
Now uh, quickly we will go ahead and perform this.
So let's go ahead and write.
I will first of all call my add function.
So let's go ahead and call my add ten comma 15.
Let's say subtract 15 comma ten.
Okay.
Then we will go ahead and call our multiply operation ten comma 20.
And then we will also go ahead and call.
Our divide operation is nothing but 2010.
Now quickly we will go ahead and execute it.
So in order to execute it I will go inside this particular folder.
So I'll do CD dot dot.
Okay.
And it is inside this particular folder.
Please make sure you go in that particular folder before executing this.
And now I'll go ahead and write a Python app dot pi.
I hope so it gets executed.
So here you can see I'm getting some error logger.debug a plus B result adding uh five minus ten okay,
remaining all the arguments has been displayed.
But here we are getting an error.
Okay.
Not all arguments converted during string formatting.
Okay I can understand this particular error.
Let's see.
Mhm.
Return a plus b.
Okay.
Now I think it should work.
Let's see.
Okay.
So now uh again I'm getting an error message.
Adding plus ten.
First of all let's go ahead and see whether the log is basically getting created or not.
So here log is basically getting created but the addition log is not happening.
Why.
Let's see the error.
What exactly is the error with respect to add uh definition.
And then I have adding a plus B okay.
This was a simple error that I can basically use.
And result should be in my another brackets.
Okay.
Now let's go ahead and execute it again F string is basically used over here.
The reason is very simple log or debug.
And here we will just go ahead and close this in my brackets.
So this kind of issues will be coming up.
So don't worry or hesitate about solving debug errors.
Write.
Debugging is very much important.
Now if I go ahead and execute it now you can see all this message is basically getting displayed.
If I go ahead and see my app dot log.
Now you can see adding ten plus 15 all this arithmetic app.
This is with respect to my modules right.
And with respect to the modules also you'll be able to see the message.
Perfect.
This looks really really good.
Now you can go ahead and create your own custom module like arithmetic app.
Let's say in another file I want to create another module.
You can create all the all the different different loggers over here itself.
Right.
Based on different different modules.
It is up to you.
Okay.
And then whether you want to use a debug or whether you want to go ahead and find out or probably display
an error message, then you can actually do it.
Let's say that I give over here as 20 comma zero.
Okay.
So here obviously I'm going to face an error right.
So if I go ahead and execute it you'll be able to see division by zero.
Error is there.
And here you'll be able to see that this particular error message is getting displayed right in my app
dot app one dot log.
So yes, I hope are you able to understand about this logging functionality.
This is really important.
Really really important for everyone out there.
So please make sure that you keep on practicing it.
And uh, we'll be seeing more about it in the upcoming videos.
So yes, this was it from my side.
I'll see you in the next video.
Thank you.
Take care.