askance
askance

Reputation: 1147

Logging error involving .conf+main.py modules

I think I'm missing something big and for the life of me, I can't figure it out. I have a logging.conf file that I am trying my main (say, xyz.py) file to read. But I am getting this weird error. I have the traceback below followed by the configuration file - logging.conf and then the relevant part in xyz.py.

File "xyz.py", line 28, in <module>
log = logging.config.fileConfig('/Users/Username/Desktop/logging.conf')
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 78, in fileConfig
handlers = _install_handlers(cp, formatters)
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 156, in _install_handlers
h = klass(*args)
TypeError: __init__() takes at most 7 arguments (23 given)

The configuration file - full path = /Users/Username/Desktop/logging.config (I followed the instruction from http://docs.python.org/release/2.5.2/lib/logging-config-fileformat.html)

[loggers]
keys=root

[handlers]
keys=handlersmtp, handlerfile

[formatters]
keys=formatter

[formatter_formatter]
format=%(asctime)s %(name)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter

[logger_root]
level=NOTSET
handlers=handlersmtp, handlerfile

[handler_handlersmtp]
class=handlers.SMTPHandler
level= INFO
formatter=formatter
args=(('localhost', 25),'[email protected]', ['[email protected]'],
                        'The log')


[handler_handlerfile]
class=handlers.RotatingFileHandler
level= INFO
formatter=formatter
backupCount=1440
args=('alogger.log')

The part in main file -xyz.py

import logging
import logging.config

log = logging.config.fileConfig('/Users/Username/Desktop/logging.config')

I looked at the Python is logging/config.py module but couldn't follow why it was raising this. It's a pretty big file.

EDIT:

@VineySajip's answer removed the error above but I am working on this new one now.

[handler_handlerfile]
class=handlers.RotatingFileHandler
level= INFO
formatter=formatter
args=('alogger.log', mode='a', maxBytes=25000, 
backupCount=0, encoding=None, delay=0) #New line to fit 
                  #this page but code has it all in 1 line

The new traceback:

Traceback (most recent call last):
  File "cpu6.py", line 29, in <module>
    log = logging.config.fileConfig('/Users/Username/Desktop/logging.ini')
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/
  Versions/2.7/lib/python2.7/logging/config.py", line 78, in fileConfig
    handlers = _install_handlers(cp, formatters)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/
  Versions/2.7/lib/python2.7/logging/config.py", line 155, in _install_handlers
    args = eval(args, vars(logging))
  File "<string>", line 1
    ('alogger.log', mode='a', maxBytes=25000, 
    backupCount=0, encoding=None, delay=0)
                                     ^
  SyntaxError: invalid syntax

Upvotes: 0

Views: 631

Answers (1)

Vinay Sajip
Vinay Sajip

Reputation: 99345

In your config, ('alogger.log') is not a valid argument tuple, and in fact the whole section looks wrong. RotatingFileHandler has the following arguments:

filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0

and you need to specify an argument tuple which reflects this. You haven't specified a maxBytes value, so rollover would never occur; and 1440 looks like an odd number of backup log files to keep. Review the documentation to make sure you're using the correct arguments for the handler's __init__.py.

Update: Leave out the parameter names, like so:

args=('alogger.log', 'a', 25000, 0, None, 0)

Upvotes: 3

Related Questions