Opened 14 years ago

Closed 10 years ago

#15917 closed Bug (wontfix)

Logging dictionary config works wrong at least with SMTPHandler

Reported by: Veniamin Albaev Owned by: nobody
Component: Core (Other) Version: 1.3
Severity: Normal Keywords:
Cc: Jesaja Everling Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

/django/utils/dictconfig.py:218 has method BaseConfigurator.convert() wich converts dictionaries, tuples and lists to it ConvertingDict, ConvertingType, ConvertingList analogies. But in /logging/handlers.py:808 SMTPHandler.init() method argument types checks for full equality with particular type (ex. if type(credentials) == types.TupleType).
As result it's imposiible to pass mailhost with port and credentials to SMTPHandler througth dictionary config

Attachments (1)

tests.py (896 bytes ) - added by Veniamin Albaev 14 years ago.
Tests demontrates incorrect behaviour

Download all attachments as: .zip

Change History (7)

comment:1 by Aymeric Augustin, 14 years ago

Resolution: needsinfo
Status: newclosed

I could not reproduce this with the SVN version of Django and Python 2.6 or 2.7. The following settings load correctly:

LOGGING = {
    'version': 1,
    'handlers': {
        'email': {
            'class':'logging.handlers.SMTPHandler',
            'mailhost': 'localhost',
            'fromaddr': 'django@example.com',
            'toaddrs': ['django@example.com'],
            'subject': 'oops!',
            'credentials': ('root', 'root'),
        },
    },
    'django': {
        'handlers':['email'],
    },
}

The logging code is directly taken from Python itself, I could not find a similar issue in Python's bug tracker.

Please attach a failing test case and reopen the ticket.

by Veniamin Albaev, 14 years ago

Attachment: tests.py added

Tests demontrates incorrect behaviour

comment:2 by Veniamin Albaev, 14 years ago

Resolution: needsinfo
Status: closedreopened

test.py (in attachment) fails with next configuration

LOGGING = {
    'version': 1,
    'disable_existing_loggers' : True,

    'handlers': {
        'mail': {
            'level': 'DEBUG',
            'class':'logging.handlers.SMTPHandler',
            'mailhost': ('mailhost.test', 321),
            'fromaddr': 'testuser@domain.com',
            'toaddrs': ['testuser@domain.com',],
            'subject': 'Test email',
            'credentials': ('testuser1', 'testpassword1',),
        },
    },

    'loggers': {
        'test.logger': {
            'level': 'DEBUG',
            'handlers': ['mail',],
            'propagate': False,
        },        
        
    },
}

comment:3 by Aymeric Augustin, 14 years ago

Triage Stage: UnreviewedAccepted

OK, I was expected an exception, actually it's rather a silent misconfiguration. I confirm that django.utils.log.dictConfig with Python 2.6 and logging.config.dictConfig in stock Python 2.7 behave differently.

>>> from django.utils.log import dictConfig   # Django
>>> dictConfig({
...     'version': 1,
...     'handlers': {
...         'send_email': {
...             'class':'logging.handlers.SMTPHandler',
...             'mailhost': ('localhost', 25),
...             'fromaddr': 'django@example.com',
...             'toaddrs': ['django@example.com'],
...             'subject': 'oops!',
...             'credentials': ('root', 'root'),
...         },
...     },
...     'loggers': {
...         'django': {
...             'handlers':['send_email'],
...         },
...     },
... })
>>> import logging
>>> handler = logging.getLogger('django').handlers[0]
>>> handler.mailhost
('localhost', 25)
>>> handler.mailport
>>> handler.username
>>> handler.password
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: SMTPHandler instance has no attribute 'password'
>>> from logging.config import dictConfig   # Python 2.7

(snip - same commands as above)

>>> handler.mailhost
'localhost'
>>> handler.mailport
25
>>> handler.username
'root'
>>> handler.password
'root'

comment:4 by Jesaja Everling, 13 years ago

Cc: Jesaja Everling added
UI/UX: unset

I also ran into this issue, because I was trying to specify the SMPT-port (for TLS).
You are supposed to pass mailhost = (HOST, PORT) to SMTPHandler. However, as albenik said, SMTPHandler.init does type-checking which fails due to mailhost being of type django.utils.dictconfig.ConvertingTuple:

/usr/lib/python2.6/logging/handlers.py:819

if type(mailhost) == types.TupleType:
    self.mailhost, self.mailport = mailhost

This issue doesn't exist in Python 2.7:
/usr/lib/python2.7/logging/handlers.py:841

if isinstance(mailhost, tuple):
    self.mailhost, self.mailport = mailhost

comment:5 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:6 by Berker Peksag, 10 years ago

Resolution: wontfix
Status: newclosed
  1. django.utils.log.dictConfig now uses logging.config.dictConfig, and django.utils.dictconfig has been deprecated.
  2. Django 1.7 and newer versions are no longer supports Python 2.6.
  3. Django 1.6 is now in security-fix-only mode, so I'm closing this ticket as "wontfix".
Note: See TracTickets for help on using tickets.
Back to Top