Home > Software design >  Log files are not getting created in python
Log files are not getting created in python

Time:12-21

Created Logger file and accessing the same in another file. But not getting any logging detailes.

import logging

class LogGen:
    @staticmethod
    def logGen():
        logging.basicConfig(filename=".\\Logs\\automation.log",
                            format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
        logger=logging.getLogger()
        logger.setLevel(logging.INFO)
        return logger

On access this from the below code, this is not creating the logfile in my project.

import pytest
from selenium import webdriver
from pageObjects.LoginPage import Login
from utilities.readProperties import Readconfig
from utilities.customLogger import LogGen

class Test_001_Login:
    baseurl=Readconfig.getApplicationURL()
    username=Readconfig.getUseremail()
    password=Readconfig.getUserpassword()
    logger=LogGen.logGen()

    def test_homepage_title(self,setup):
        self.logger.info("**************Test_Case_001_HP********")
        self.driver=setup
        self.driver.get(self.baseurl)
        act_title=self.driver.title

        if act_title=="Your store. Login":
            assert True
            self.logger.info("**************Test case 1 pass *******")
            self.driver.close()
        else:
            self.driver.save_screenshot(".\\Screenshot\\" "test_homepage_title.png")
            self.logger.error("**************Test_Case_001_Fail********")
            self.driver.close()
            assert False


    def test_login(self,setup):
        self.logger.info("**************2********")
        self.driver=setup
        self.driver.get(self.baseurl)
        self.lp=Login(self.driver)
        self.lp.setUserName(self.username)
        self.lp.setPassword(self.password)
        self.lp.clickLogin()
        print("test case2")
        actual_title=self.driver.title
        if actual_title=="Dashboard / nopCommerce administration1":
            assert True
            self.logger.info("**************Test_Case_002_Pass********")
            self.driver.close()
        else:
            self.driver.save_screenshot(".\\Screenshot\\"   "test_login.png")
            self.logger.error("**************Test_Case_002_Fail********")
            self.driver.close()
            assert False





Created Logger file and accessing the same in another file. But not getting any logging detailes.

CodePudding user response:

If you change the path from:

filename=".\\Logs\\automation.log",

to:

filename="./Logs/automation.log",

I think it might work!

CodePudding user response:

It worked with force=True Use the below syntax. Thanks ALL

logging.basicConfig(filename="./Logs/automation.log", format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', force=True)

  • Related