Custom Logger

First we need to create a custom logger class (CustomLogger.py) which has methods such as log structure details (customLogger() ) and allure report logs ( allureLogs(text) ).

CustomLogger.py

import inspect
import logging

import allure


def customLogger():
    # 1.) This is used to get the  class / method name from where this customLogger method is called
    logName = inspect.stack()[1][3]

    # 2.) Create the logging object and pass the logName in it
    logger = logging.getLogger(logName)

    # 3.) Set the Log level
    logger.setLevel(logging.DEBUG)

    # 4.) Create the fileHandler to save the logs in the file under reports folder.
    fileHandler = logging.FileHandler("../reports/Skill2Lead.log", mode='a')

    # 5.) Set the logLevel for fileHandler
    fileHandler.setLevel(logging.DEBUG)

    # 6.) Create the formatter in which format do you like to save the logs
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s : %(message)s',
                                  datefmt='%d/%m/%y %I:%M:%S %p %A')

    # 7.) Set the formatter to fileHandler
    fileHandler.setFormatter(formatter)

    # 8.) Add file handler to logging
    logger.addHandler(fileHandler)

    #  9.) Finally return the logging object

    return logger

# Allure Report log method

def allureLogs(text):
    with allure.step(text):
        pass




Code Explanation

1. This helps to get the class name where we call the logger object and store the data in a log file.

logName = inspect.stack()[1][3]

2. Now create the logger object and pass the logName object in it. This helps to create the log object.

logger = logging.getLogger(logName)

3. Set the log level to Debug, So that it will print all the log levels of debug,error and warning etc.

logger.setLevel(logging.DEBUG)

4. Create the file handler object and pass two parameters. First parameter path to save the logs in a file. Second parameter as mode of data should be saved in the file.

“a” : stands for appending the data for an existing file.

fileHandler = logging.FileHandler("../reports/Skill2Lead.log", mode='a')

5. Now set the log level for the created fileHandler object.This saves the data in the path which we deafined.

fileHandler.setLevel(logging.DEBUG)

6. Create the formatter in which format do you like to save the logs.

Below format creates the log with date , time , day , class name and log text.

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s : %(message)s', datefmt='%d/%m/%y %I:%M:%S %p %A')

10/10/20 06:54:00 PM Saturday - BasePage - INFO : Element found with LocatorType: id with the locatorValue :com.skill2lead.kwad:id/ContactUs 

7. Now set this formater to file handler object.

fileHandler.setFormatter(formatter)

8. Add the file handler object to the logger object now.

logger.addHandler(fileHandler)

9. Finally now return the logger object.




How to use custom logger

1. Import the customer logger where we need to use a custom logger.

import AppiumFrameWork.utilities.CustomLogger as cl

2. Now create the variable to store the logger object which returns.

log = cl.customLogger()

3. Now call the log object with log levels such as info() in method to store success data and error() in except condition to store the error logs as below.

def sendText(self, text, locatorValue, locatorType="id"):
   try:
       locatorType = locatorType.lower()
       element = self.getElement(locatorValue, locatorType)
       element.send_keys(text)
       self.log.info(
           "Send text  on Element with LocatorType: " + locatorType + " and with the locatorValue :" + locatorValue)
   except:
       self.log.error(
           "Unable to send text on Element with LocatorType: " + locatorType + " and with the locatorValue :" + locatorValue)
       self.takeScreenshot(locatorType)

allure method

Create a method which saves data in allure reports. This method takes a string parameter.

# Allure Report log method

def allureLogs(text):
    with allure.step(text):
        pass