Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#19395 closed Cleanup/optimization (fixed)

Improve logging section by providing a SIMPLE example

Reported by: ken.nelson@… Owned by: nobody
Component: Documentation Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

New to Python and Django. We have been gifted with several Django sites to maintain. These sites are poorly written and have no debug capabilities, so i wanted to enable a primitive log to report on uncaught exceptions, as well as a place we can manually write to. I don't have all afternoon to work through all the permutations of Python logging; I just need the most basic. So, the fact that the only log config example you provided is humungous is disappointing.

I'm sure many would be grateful for a minimal single textfile logging example they can just cut'n'paste and use ASAP. Thanks.

Attachments (1)

19395.diff (1.6 KB ) - added by Tim Graham 10 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by anonymous, 11 years ago

I heard some people being very enthousiastic about logbook: http://packages.python.org/Logbook/ . Though that doesn't solve the docs, it might be worthwile to take a look on their website. They give an example right-away, and it is very clear.

comment:2 by Aymeric Augustin, 11 years ago

Triage Stage: UnreviewedAccepted

comment:3 by ken.nelson@…, 11 years ago

Thanks to commenter for the suggestion re logbook ( http://packages.python.org/Logbook/ ). Our main requirement is for something to catch and report on _untrapped_ errors, so the built-in Python/Django logging is required.

comment:4 by jkeyes, 11 years ago

I think it's unclear what a simple example would be. Logging to syslog, to stdout, ...?

The following is the logging code in settings.py generated by (django-admin.py startproject ...):

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

Is this a simple enough configuration to begin with?

Aside: For unhandled exception tracking, I think you should have a look at Sentry or Exceptional.

comment:5 by ken.nelson@…, 11 years ago

Thanks! Email notification would be a good starting point, especially for a live site. Me, I've always preferred a simple text log, especially at the development stage. I can't understand how people can develop without one, frankly, but that's just my opinion.

Here's what I ended up with, after spending some time in the Django docs:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/var/log/django/cotizador.log',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

It seems to work, and is catching untrapped exceptions, but I'm sure it could be improved. The first things I'd add are the formatter with timestamp, and nightly file rollover.

I think that an example containing both the emailer instance and a simple textfile instance would help 99% of users get something working right away. Again, thanks.

by Tim Graham, 10 years ago

Attachment: 19395.diff added

comment:6 by Tim Graham, 10 years ago

Has patch: set

comment:7 by Claude Paroz, 10 years ago

I don't think that which writes all logging to a local file is true. all request logging would be probably more correct.

comment:8 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 69f0249d7b6dfbf278bbc2d5907f598d5ed241af:

Fixed #19395 -- Added a simple example logging config.

Thanks ken.nelson at maclaren.com.

comment:9 by Tim Graham <timograham@…>, 10 years ago

In a05ca51c08d1cc4e69ca9b33a5a519e062d8236f:

[1.6.x] Fixed #19395 -- Added a simple example logging config.

Thanks ken.nelson at maclaren.com.

Backport of 69f0249d7b from master

Note: See TracTickets for help on using tickets.
Back to Top