Further to my earlier post - I've put the whole branch on Launchpad:

https://code.launchpad.net/~vinay-sajip/django/logging

There's nothing much there at the moment, apart from some code as a
proof of concept. Tested with the following code tacked on at the end
of a vanilla settings.py:

import logging

def init_logging():
    #Configure root to write to file
    logging.basicConfig(level=logging.DEBUG, filename='logtest.log',
                        filemode='w', format='%(asctime)s %
(levelname)-8s %(name)-13s %(message)s')
    alogger = logging.getLogger("logtest")
    alogger.setLevel(logging.INFO)
    logger = logging.getLogger("logtest.foo")
    logger.setLevel(logging.WARNING)
    logger = logging.getLogger("logtest.bar")
    logger.setLevel(logging.ERROR)
    #Call dummy function in django.utils.log
    from django.utils.log import configure_from_dict
    configure_from_dict({'version' : '0'})
    alogger.info("Called configure_from_dict (just to show we can)")

def test_logging():
    from random import choice
    levels = (logging.DEBUG, logging.INFO, logging.WARNING,
              logging.ERROR, logging.CRITICAL)
    loggers = ('', 'logtest', 'logtest.foo', 'logtest.bar')
    for i in xrange(1000):
        level = choice(levels)
        logger = logging.getLogger(choice(loggers))
        logger.log(level, "Message #%d", i + 1)


def listener(sender, *args, **kwargs):
    logger = logging.getLogger("logtest")
    logger.info("class_prepared listener called: %s", sender.__name__)

def pre_model_callback():
    from django.db.models.signals import class_prepared
    logger = logging.getLogger("logtest")
    logger.info("Adding class_prepared listener ...")
    class_prepared.connect(listener)

def post_model_callback():
    from django.contrib.auth.models import User
    logger = logging.getLogger("logtest")
    try:
        umsg = "%(username)s (%(first_name)s %(last_name)s)"
        users = ", ".join([umsg % u.__dict__ for u in User.objects.all
()])
        logger.info("ORM works: all users: %s" % users)
    except:
        logger.exception("Unable to get all users")

BOOTSTRAP_CALLBACKS = (
    init_logging,
    #test_logging,
)

PRE_MODEL_CALLBACKS = (
    pre_model_callback,
)

POST_MODEL_CALLBACKS = (
    post_model_callback,
)
-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.


Reply via email to