I am writing a custom logging module which can be imported into all my codes to create standard logs. Here is my logging module log_info.py:
class CustomLogger:
def custom_logger(self, loglevel= logging.INFO):
logger_name= inspect.stack()[1][3]
logger= logging.getLogger(logger_name)
logger.setLevel(loglevel)
log_message_format= logging.Formatter('%(levelname)s:%(name)s,:%(message)s')
file_handler= logging.FileHandler('sla_data.log')
file_handler.setFormatter(log_message_format)
logger.addHandler(file_handler)
return logger
And here is my code where I am importing the above module:
from log_info import CustomLogger
#code to do a bunch of things
...
#calls the custom logging module as
logs= CustomLogger.custom_logger(loglevel= logging.INFO)
logs.info(f'logs: started_by: {windows_user}, started_at: {etl_start},\
ended_at: {etl_end}, with no errors')
But I get this error: TypeError: custom_logger() missing 1 required positional argument: 'self'.
Update: I removed 'self' from custom_logger() and it worked. I probably don't understand the usage of 'self' too well. I'd like to understand more and also how can this code be improved?