Hi, yes, it's absolutely possible. Refer to Django docs about logging 
(https://docs.djangoproject.com/en/1.10/topics/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
 
<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',
        },
    },
    ...
}


> On 23 Jan 2017, at 12:51, Arun S <arun....@gmail.com> wrote:
> 
> 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)
> 
> 
> 
> 
> -- 
> 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+unsubscr...@googlegroups.com 
> <mailto:django-users+unsubscr...@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com 
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users 
> <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
>  
> <https://groups.google.com/d/msgid/django-users/3275bad0-405b-4bbf-afe7-d0eeb1feb77f%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/67752380-BA71-4FCC-A8B7-234E494824BB%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to