0

I am trying to config the logger using logging.conf file but I am getting error, In Code I am trying to replace the file path in conf file with dynamic path includes timestamp folder Following is my code

python module to configure the log -

class LogHandler: logger = None def __init__(self,file_path): conf_path = os.path.dirname(os.getcwd())+"\\config\\log_config.conf" #Setting the log file path to configuration file with open(conf_path,'r') as file: content = file.readlines() file_path = file_path.replace("\\", "\\\\") print(file_path) for line_no in range(len(content)): if re.search("args=\('.*',", content[line_no]): content[line_no] = "args=('" + file_path + "','a')\n" break file.close() with open(conf_path,'w') as file: file.write(''.join(content)) file.close() logging.config.fileConfig(conf_path) LogHandler.logger = logging.getLogger("root") LogHandler.logger.info("Testing") @staticmethod def getLogger(): return LogHandler.logger 

logging.conf module for configuration -

[loggers] keys=root [handlers] keys=consoleHandler [formatters] keys=sampleFormatter [logger_root] level=DEBUG handlers=consoleHandler [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=sampleFormatter args=('C:\\Users\\vipin\\PycharmProjects\\PythonBehaveFramework\\output\\201912100840\\output.log','a') [formatter_sampleFormatter] format=%(asctime)s : %(lineno)s - %(funcName)s - %(levelname)s - %(message)s 

Error -

Traceback (most recent call last): File "C:/Users/vipin/PycharmProjects/PythonBehaveFramework/Features/test.py", line 8, in <module> LogHandler.getInstance("C:\\Users\\vipin\\PycharmProjects\\PythonBehaveFramework\\output\\output.log") File "C:\Users\vipin\PycharmProjects\PythonBehaveFramework\src\framework\LogHandler.py", line 28, in getInstance LogHandler(filename) File "C:\Users\vipin\PycharmProjects\PythonBehaveFramework\src\framework\LogHandler.py", line 50, in __init__ logging.config.fileConfig(conf_path) File "C:\Users\vipin\AppData\Local\Programs\Python\Python36-32\lib\logging\config.py", line 84, in fileConfig handlers = _install_handlers(cp, formatters) File "C:\Users\vipin\AppData\Local\Programs\Python\Python36-32\lib\logging\config.py", line 148, in _install_handlers h = klass(*args) TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given 

1 Answer 1

1

The config for your handler_consoleHandler is inconsistent.

[handler_consoleHandler] class=StreamHandler <snip> args=('C:\\Users\\vipin\\PycharmProjects\\PythonBehaveFramework\\output\\201912100840\\output.log','a') 

fileConfig creates a StreamHandler instance, based on the class you specify. This class accepts one positional argument (other than self) upon instantiation.

class logging.StreamHandler(stream=None) 

The args you specify belong to a FileHandler resp. a subclass thereof, i.e. specify a log file path and mode.
This passes two positional arguments to the StreamHandler initializer, causing the TypeError.

If you want to log to a file, change to class of your handler to FileHandler or any of its derivatives.
If you indeed want to log to the console, either remove the args completely to stick with the default stderr or specify whichever other stream you desire, e.g. args=(sys.stdout,) of you want the handler to log to stdout instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.