- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to configure handling and formatting of log file in Selenium with python?
A log configuration consists of formatter and FileHandler methods. We need to import a logging package and then create an object which will be responsible for entire logging.
If we add the parameter _name_ inside the getLogger() method, we shall be able to add the test case name for which we want to create the log file. If this parameter is omitted in the argument, by default it prints root in the log file.
logger = logging.getLogger(_name_)
The different types of logger level are listed below. We can add all, some or at least one logger in our test case.
- logger.debug("Debug log")
- logger.info("Information log")
- logger.warning("Warning log")
- logger.error("Error log")
- logger.critical("Critical log")
Then we should specify the location to the logger object to print these logs. This is handled with the help of addHandler() method. This method accepts the file handler object as its arguments.
The file handler object works with the parent logging package and its method known as the FileHandler(). This method contains the name and location of the log file where the entire logging information will be available.
fileHandler = logging.FileHandler('logfile.log') logger.addHandler(fileHandler)
Next we need to define the format in which the log file is to be created. This is achieved by the Formatter() method obtained from the parent logging package. The Formatter() method takes the format to be followed as an argument.
logging.Formatter("%(asctime)s :%(levelname)s : %(name)s :%(message)s")
This has to be stored in a formatter object and it can correlate with the fileHandler object via setFormatter() method. This method accepts the formatter object.
Finally, we need to set the level of logging. This is achieved by setLevel() method.
Through this we can limit printing of all types of logger levels in the log file.
Coding Implementation of logging configuration.
import logging def loggingDmo(): # getLogger() method takes the test case name as input logger = logging.getLogger(__name__) # FileHandler() method takes location and path of log file fileHandler = logging.FileHandler('logfile.log') # Formatter() method takes care of the log file formatting formatter = logging.Formatter("%(asctime)s :%(levelname)s : %(name)s :%(message)s") fileHandler.setFormatter(formatter) # addHandler() method takes fileHandler object as parameter logger.addHandler(fileHandler) # setting the logger level logger.setLevel(logging.DEBUG) logger.debug("Debug log") logger.info("Information log ") logger.warning("Warning log") logger.error("Error log") logger.critical("Critical log")
- How to upload file with selenium (Python)?
- Downloading file to specified location with Selenium and python.
- tellp() in file handling with C++
- How to read a text file in Selenium with python?
- How to write a text file in Selenium with python?
- Matplotlib log scale tick label number formatting
- Basics of File Handling in C
- File Handling in C#
- How to do session handling in Selenium Webdriver?
- How to compare Python string formatting: % with .format?
- Basics of File Handling in C Programming
- How to configure C# ASP.NET WebAPI in web.configure file?
- File Handling in Java using FileReader and FileWriter
- How to programmatically configure Chrome extension through Selenium WebDriver?
- Set position with seekg() in C++ language file handling