I have created a customize logger function in Python file test.py:
import logging
def logger1():
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('employee.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
And created another test1.py file where I doing below operation and trying to log data
from utility.test import logger1
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logger1.logger.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
But when I run this Python file test1.py, I get the following error:
Traceback (most recent call last): File "C:/python/SeleniumFramework/utility/test2.py", line 21, in <module> emp_1 = Employee('John', 'Smith') File "C:/python/SeleniumFramework/utility/test2.py", line 10, in __init__ logger1.logger.info('Created Employee: {} - {}'.format(self.fullname, self.email)) AttributeError: 'function' object has no attribute 'logger'
Note: Without creating a function of customize logger it is working file.
You forgot the brackets behind logger1...
This means to get logger1 you will need to call it and not pass the functions reference:
logger1().info("Look I am log")
Btw with this function you will always create a new logging instance which is not your intention I think.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.