Python Logging: __init__() missing 1 required positional argument: 'filename'

Question:

I’m trying to use the standard library logging module, using a config file to organise the loggers.

I tried a simple setup like so:

Config file:

[loggers]
keys=root,main,second

[handlers]
keys=consoleHandler, exportHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_main]
level=DEBUG
handlers=consoleHandler,exportHandler
qualname=simpleExample
propagate=0

[logger_second]
level=DEBUG
handlers=consoleHandler,exportHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_exportHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
filename='LogBookmain.log'


[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

File1

import logging
import logging.config
import os

logging.config.fileConfig('logging.conf')
logger = logging.getLogger('main')

os.system('test2.py')
logger.info('script was executed correctly')

File2

import logging
import logging.config

logging.config.fileConfig('logging.conf')
logger = logging.getLogger('second')

a = 8
b = 4

result = a + b
logger.debug('result of a + b is '+ result)
result2 = a / b
logger.debug('result of a / b is '+ result)

but I get an error when I try running the second file:

Traceback (most recent call last):

  File "D:\Loggingtest2.py", line 4, in <module>
    logging.config.fileConfig('logging.conf')

  File "C:\anaconda3libloggingconfig.py", line 79, in fileConfig
    handlers = _install_handlers(cp, formatters)

  File "C:\anaconda3libloggingconfig.py", line 145, in _install_handlers
    h = klass(*args, **kwargs)

TypeError: __init__() missing 1 required positional argument: 'filename'

When I look up the error message on google, it only talks about python classes, but I’m not trying to write my own class here. What went wrong, and how do I fix it?

Asked By: Nelerdeth

||

Answers:

You wrote

logging.config.fileConfig('logging.conf')

I think you wanted

logging.config.fileConfig('logging.conf',
                          defaults={'logfilename': 'LogBookmain.log'})

It’s either ‘logfilename’ or ‘filename’.
Or similarly adjust the config file.

Answered By: J_H

There were two errors in my logging.config:

  1. For Logger_main and logger_second, the qualname had the same name (‘simpleExample’), and refers to nothing in use.
  2. Error in the Handler declaration:
class=FileHandler
level=DEBUG
formatter=simpleFormatter
filename='LogBookmain.log'

to the correct :

class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('LogBookmain.log',)

This question helped me a lot, especially on how to define the args, and see how a working config looked like.

Answered By: Nelerdeth
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.