Writing a custome Django Log Formatter.

202 views
Skip to first unread message

Arun S

unread,
Jan 23, 2017, 4:51:55 AM1/23/17
to Django users
Hi all,

I have a small issue with writing a Custom Log Formatter.

By default: log_format = '%(asctime)s %(module)s %(levelname)-8s- %(message)s'

This is the Log formatter being used.
But only in a few cases or rather in a few modules, i would like to include for ex: "some_id" as a seperator in the Logs.
Intended : log_format = '%(asctime)s %(module)s %(levelname)-8s- %(some_id)s %(message)s'

But this always raises a Key Error and i would not want to change each and every existing log and manually add the new log.

Is there a way to write this Custom Log format.???

I did try this:
log_format = '%(asctime)s %(module)s %(levelname)-8s- %(some_id)s %(message)s' %{'some_id': id}

This gives a Key Error "
asctime "

And During Logging setup, i setup the Logging :

formatter = logging.Formatter(data.get('logging.log_format', None))
file_handler.setFormatter(formatter)



"Александр Христюхин (roboslone)"

unread,
Jan 23, 2017, 4:59:25 AM1/23/17
to django...@googlegroups.com
Hi, yes, it's absolutely possible. Refer to Django docs about logging (https://docs.djangoproject.com/en/1.10/topics/logging/) and logging docs (https://docs.python.org/3/library/logging.config.html#logging-config-dictschema).

What you're looking for is logging formatters and filters. Here's an example:


LOGGING = {
    'formatters': {
        'detailed': {
            'format': '%(asctime)s %(module)s %(levelname)-8s- %(some_id)s %(message)s',
            ...
        },
    },
    'handlers': {
        'default': {
            'formatter': 'detailed',
            'filters': ['some_id_filter'],
            ...
        },
        ...
    },
    'filters': {
        'some_id_filter': {
            '()': 'package.module.SomeIdFilter',
        },
    },
    ...
}


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3275bad0-405b-4bbf-afe7-d0eeb1feb77f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arun S

unread,
Jan 23, 2017, 5:04:04 AM1/23/17
to Django users
Hi,

Thanks, I did this. But this applies to all the Modules in the Project.

Basically my issue is when Django is running with Celery and there are more no of workers, the Logs needs to be diffrentiatied and also not for all the Modules of the project.
So in order to apply for a particular module and handle it in the Code,


Can the Filter be applied individally and part of the Code when the Logger is being setup for a module.???

Thanks & Cheers
Arun

"Александр Христюхин (roboslone)"

unread,
Jan 23, 2017, 5:12:47 AM1/23/17
to django...@googlegroups.com
Filter applies to handlers, as you can see from my example. So what you need is a custom handler for loggers that require that some_id of yours.

Reply all
Reply to author
Forward
0 new messages