Logging in python flask applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome to team python Series in this video we're gonna talk about how to do logging in Python flask applications so why is logging important in web applications logging is really important because with proper logging you can understand what's happening in each request and you can also know what's happening in the application for logging in flask the good news is that flask uses the standard python login module if you are new to flask or if you want to know more about developing web applications in Python flask module I've already created a playlist on that and I will leave the link of this playlist in the description of this video if you don't know how to do logging in Python I've already created a video on creating logs in Python and log rotation and compression of Rotator locks already and I will leave the link of that video in the description and one more thing while using logs in flask is that you don't need to create a logger object separately flash creates its own logger object when you create a flask application so you don't need to create a logger object you can use the flasks logger object so how can you do a basic logging in flask it's really simple just before creating the flask application configure the root logger and flask will automatically copy all the root logger handlers to its logger Handler so let's try to do that with an example I'm going to take a blank folder and I'm going to open it with vs code I'm going to create a new file and I'll name it server.pi and it's going to be your flash python server so first let's create a simple flask application all right this is a really simple flask application where I have created a server instance and it's running at localhost Port 1500 and I have created a root which results to the root URL which is like slash and it will return hello world in response so let's try to run this simple flash application now now let's visit the servers URL which is localhost port 1500. and here you can see Hello World and notice something interesting in the terminal you're already getting locks if I reload this page again you can see one more log is getting generated that means flask already does some default logging so since I already have a logger in flask application let's try to use that logger now so I'm gonna close this terminal and let's try to use the flask logger so since we have the flask application in this variable app I can just write app dot logger and this will be the logger of the flask application and I can now create logs like infolock logger.info I can just say from Route Handler so I've used the logger of the flask application and I'm creating an info log from there so it's that simple let's try to run this flask application and see what happens so now our flash competition is running and let's try to reload this home page and now you can see I've got a log being generated here from Route Handler so just like that we have used the flasks logger to create logs in the flask application all right so far so good what if I want to configure The Flash logger to customize the logging Behavior something like I want to change the logging format or I want to send logs to a file or I want to send logs to email something like that so what you've got to do is before creating the flask application configure the root logger because while the flask application is being created all the log handles of the root logger will be copied to the flasks logger object so let's try to do that now so before creating the flask application I'm gonna configure the logging module so that I can configure the root logger here logging is not imported so let's try to import login model so import login and now I did a basic config where I am telling to send the logs to this file called logs.log and I'm changing the format of the logs being generated so now using the single line using the basic config I have configured the root lover and then I am creating the flask application so as per this basic config the log should go into a file called logs.log you can see there is no file currently let's try to save this and run this application and then you can see the logs.log file being generated and the log format is as per the format which we have set in this basic config so let's try to again reload our application and you can see new log being generated and the log format is as per our format and log is from root Handler which you have created in this server.pi and that's how using a single line I was able to configure The Flash login so if you remove this file name equal to logs.log and just run this then your log should be coming to the console instead of file so that's why logs are coming to the console now if I just reload this file you can see again the logs are coming here because I didn't set the file name I just set the format so by default logs will go into the console all right what if I want to send logs to multiple things like I want to send logs to the console and also the file in those scenarios instead of using basic config you can get the root logger object and configure it so let's try to do that now so I'm going to comment out this in fact I'm going to delete this line and I'm going to access the root logger so the root logger will be logger equal to login Dot getlogger and now if you don't Supply any name to the get logger it will be the root logger and now let's configure this true blogger so to configure the root logger I need to create two handers which is the console based Handler and the file based Handler so this is a console Handler where amazing login dot stream Handler to create a Handler that sends logs to the console so I just created a console Handler and added that Handler to the logging object so now I have added a new file Handler which is a rotating file Handler which is imported from blogging dot handlers and this rotating file Handler with rotate locks for each 1 KB of logs and then it will retain only the latest 100 logs so the logs will be sending to a file called logs.log and I've added this new Handler to the logger object so if you want to set format to the log handlers I have to create a formatter object so just using login.formator I'll set the log format and I have even changed the date format of the timestamp and then now I can use this formatter for both the handlers so I can write console Handler dot set formatter and this format will be the log formator which you have created just now and the same thing will be applicable for the file handle so I'm going to write file Handler dot set formatter log formatter so now I have access the root Handler and then I have set the format for both the handlers and I've set up both the handlers which send logs to console and file and then I have created a flask application so let's try to save this and run this and now you can see the log format is changed and the locks will be sent to the logs.log also and now let's try to reload the page again and here you can see new logs are being generated and or log from root Handler is also being generated and logs have sent both to a file and the console also all right the next topic is how to inject contextual information like what is the request URL what is the remote address Etc into the logs so you are adding additional contextual data into your logs so this can be achieved easily using a custom log formatter as shown here so in this example we have created a formatter called request formatter and it's basically inheriting from the logging dot format or class and here we are overriding a method called format which actually uses the log record and you can modify that log record and send the modified record in return so let's try to create a custom log formatter like this now so I'm going to create a class called new formatter and it's going to inherit from logging Dot formatter and let's try to override the method which is format and since it's a class method you want to write self and here the record will be the log record and in return you have to send the super class which actually does the default behavior and just say format of the record God which you got so basically when you do this you're just saying just do what the parent does now in between you have to modify your login Behavior so if you want to add new fields to this record object I can just write record dot URL equal to I can get the request URL and use it here so I can just import request from The Flash module and say request dot URL and if you want to add remote address I can write record.remote equal to request dot remote address and now just like that in my log record I'm adding two extra attributes from the flasks request module which is URL and the remote address and this is really valuable information while you're generating logs because it can be easily useful for debugging who is doing the request at what URL the requests are coming and for what URL this log has been generated something like that but there is a cache here request.url and request.remote address will be valid only if these logs are generated inside a root Handler right locks can be generated out of the root Handler also so how to check how this function can be aware whether this log is being generated in a root Handler or not for that there's a very useful import from flash called has request context this function tells whether the request object is now containing contextual information regarding the root Handler or not so I can just write if has request context and just wrap it inside this if and then in the else now you know that there is nothing inside this request object you can set these two attributes URL and remote to none now and that's it just like that we got a new log formatter which injects contextual data of the request URL and request remote address into the logs and it's even aware whether the log is being generated in the context of a flask request or not so now instead of using this log formatter let's try to use our formatter which is the new formatter so I'm gonna write new formatter instead of logging Dot formatter and now since I've got two attributes extra which is URL and remote address let's try to use them here so I'm gonna write URL and the remote address also and now I got my extra information in the logs so let's try to save this and run this and here I got my logs and you can see none and none because these logs are generated did not in a request context right so if you just try to reload this request page and then here you can see a valid log attributes which is the path of the URL which is request.url and the remote address which is the localhost or 127001 so this way using a custom login formatter we are able to inject extra data into our flash logs which can be really helpful in debugging The Flash applications and adding more information into the logs all right let's jump into the next topic where we need to access the app logger inside a blueprint file on an extension file so let's try to see that with an example now here I have registered a blueprint of API page and which is imported from a file called API so I got a file called API dot pi and in that I am creating a blueprint and I am registering that blueprint to this flask application so this is a simple blueprint which I have created if you don't know what flash blueprints are and how to use a flash blueprints I've already created a video on that and I will leave the link of that video in the description of this video so this is The Flash blueprint called API each and there is a URL called slash info and this slash info will return hello world so let's try to just return hello API so that we can distinguish between the root URL and this URL and I have registered the blueprint which is the API page I have just imported it in my python flask server and I'm registering that here at the URL called API so if I go to localhost Port 1500 slash API slash info I should get hello API so let's try to run this server file now let's try to go to the URL localhost Port 1500 slash API slash info and here you got hello API so this blueprint is basically working but what if I want to access the apps logger and create logs in this blueprint file I don't have access to the app variable here so how can I do that so there's an awesome import from flask which is called current APP and this gives you the app variable inside the context of a request so I can just write current APP dot logger Dot info and just write from blueprint and that's it let's try to run this and let's try to reload this page localhost Port 1500 slash APA slash info and then you got the logs from blueprint and I did not access the server variable I have access the current app which gives me the access to server variable inside the context of a request in another file also so if you have a scenario where you don't have the access to server variable directly you can use something like current APP so that's it Guys these are some of the basic scenarios where you can easily do logging in flashed applications you can say I've already created a blog post on logging in Python flask applications I've also given the source code and notes so that you can copy paste and practice it in your own computer I've also given you the references to the official documentation and particularly this official documentation on flash logging is amazing and it's definitely a must read page so I highly recommend you to go through this flasks login documentation please ask questions or leave your valuable feedback in the comments section hope you like this video guys thank you for watching peace
Info
Channel: Learning Software
Views: 7,737
Rating: undefined out of 5
Keywords: python, logging, flask
Id: _Nq_n6Uk8WA
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Fri Dec 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.