Reputation: 479
Simple python which tries to write the log information using file config
import logging
import logging.config
import os
logging.config.fileConfig('Log_parameters.ini')
log_learn = logging.getLogger('sakthi')
def complex_algorithm(items):
for i, item in enumerate(items):
# do some complex algorithm computation
log_learn.debug('%s iteration, item=%s', i, item)
k = [1,2,3]
complex_algorithm(k)
Log_parameters.ini I am setting the handler, formatter in config file.
[loggers]
keys = sakthi
[handlers]
keys = handle1
[formatters]
keys = simpleformatter
[logger_sakthi]
level = DEBUG
handlers = handle1
qualname = compiler.parser
propogate = 0
[logger_handle1]
class = FileHandler
level = DEBUG
formatter = simpleformatter
args= ('Configg_log','w')
[formatter_simpleformatter]
format=F1 %(asctime)s - %(name)s - %(levelname)s - %(message)s'
datefmt =
On execution, I get the below error, KeyError
Traceback (most recent call last): File "C:/Users/M/PycharmProjects/LOGGING/Log_data_config_file.py", line 6, in logging.config.fileConfig('Log_parameters.ini') File "C:\Users\M\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 79, in fileConfig handlers = _install_handlers(cp, formatters) File "C:\Users\M\AppData\Local\Programs\Python\Python37-32\lib\logging\config.py", line 134, in _install_handlers section = cp["handler_%s" % hand] File "C:\Users\M\AppData\Local\Programs\Python\Python37-32\lib\configparser.py", line 958, in getitem raise KeyError(key) KeyError: 'handler_handle1'
Upvotes: 2
Views: 811
Reputation: 73
handle1 is not a logger but a handler so as keys have been identified as - [handlers] keys = handle1
it will search with singular name -- so it should be handler_handle1 agree with @SkyOcean
other way --
if you want to add a new logger it self then add [loggers] keys = sakthi, handle1 and then logger_handle1 will work
Upvotes: 0