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.

> On 23 Jan 2017, at 13:04, Arun S <arun....@gmail.com> wrote:
> 
> 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
> 
> 
> On Monday, January 23, 2017 at 3:29:25 PM UTC+5:30, Александр Христюхин wrote:
> 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 <http://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...@ <>googlegroups.com <http://googlegroups.com/>.
>> To post to this group, send email to django...@ <>googlegroups.com 
>> <http://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 
> <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/7e5ce87f-5749-48da-96cb-e410dc669cd9%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/7e5ce87f-5749-48da-96cb-e410dc669cd9%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/DFA5D90E-D755-497A-AF37-8B9BCBA0E2FE%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to