Sunday, October 21, 2012

Re: how to use a customized logging formatter class

As it happens I was looking into this yesterday because my request object includes a couple of attributes that I would like to see listed in the string representation of the request object. 

Your LOGGING settings  needs to have a fomatter entry that looks something like:
'formatters': {
  'my_formatter_name': {
    '()': my_project.module_name.UniqueRequestIdFormatter
  }
}

Make sure to set the formatter attribute of your handler to be the name you gave to your customer formatter. In my example above it would be 'my_formatter_name'. The '()' is a special key which tells the logger that a "Factory" will be used. You can read more about it here: http://docs.python.org/library/logging.config.html#logging-config-dict-userdef

You may also want to look at Custom error reports: https://docs.djangoproject.com/en/1.4/howto/error-reporting/#custom-error-reports (This is what I ended up using instead of a custom formatter)

On Saturday, October 20, 2012 9:59:17 PM UTC-4, Ali wrote:
I have written this custom formatter which inherits from logging.Formatter, how can I configure logging settings so that I can use this for a logger. I see the example in docs where I can update the formatter string but dont know how to link a class to do that.

class UniqueRequestIdFormatter(logging.Formatter):
    def format(self, record):
        record.message = record.msg % record.args
        record.asctime = self.formatTime(record)
        format = u'%s %s %-10s %s +%s %s' % (
        record.levelname,
            record.request.META['REMOTE_ADDR'],
            record.request.user.id if record.request.user.id else record.request.user,
            record.asctime,
            record.request.method,
            record.request.path,
            record.message
            ).encode('utf-8')
        pprint.pprint(record.__dict__)
        return format % record.__dict__

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/aazN4siEBv0J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate