On 08/20/2011 06:51 AM, Scott Danzig wrote:
> I have Django 1.3 working with Python 2.7 and MySQL 5.5 on Mac OSX Lion...
> 
> I'm betting I'm missing something straight forward, but:
> 
> I have a simple Django app in development that uses a dictConfig setting
> simpler than the default in settings.py:
> 
> LOGGING = {
>     'version': 1,
>     'disable_existing_loggers': False,
>     'formatters': {
>         'verbose': {
>             'format': '%(levelname)s %(asctime)s %(module)s %(process)d
> %(thread)d %(message)s'
>         },
>     },
>     'handlers': {
>         'console':{
>             'level':'DEBUG',
>             'class':'logging.StreamHandler',
>             'formatter': 'verbose'
>         },
>         'file':{
>             'level':'DEBUG',
>             'class':'logging.FileHandler',
>             'formatter': 'verbose',
>             'filename': 'testdjango.log',
>         },
>     },
>     'loggers': {
>         'testlogger': {
>             'handlers': ['console','file'],
>             'level': 'DEBUG',
>             'propagate': True,
>        },
>     },
> }
> 
> 
> Then later in code that I know is run... (I tried in my app's views.py
> and also the backend).. I put something like this:
> 
> import logging
> logger = logging.getLogger('testlogger')
> logger.warn('hello')
> logger.info('please appear')
> 
> 
> And I just don't see it, neither in the console, nor the file.
> 
> I have also tried something like this:
> import logging
> logger = logging.getLogger('otherlogger')
> hdlr = logging.FileHandler('newlogger.log')
> formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
> hdlr.setFormatter(formatter)
> logger.addHandler(hdlr) 
> logger.setLevel(logging.DEBUG)
> logger.warn('In settings.py!')
> 
You  have to be sure, that logging is configured before actually logging
anything.

So before your three lines:
> import logging
> logger = logging.getLogger('otherlogger')
> logger.warn('hello')
you had to be sure, that the django settings and thus the logging
configuration has really been completed.


You could for example add following two lines before:
> from django.conf import settings
> LOGGING = settings.LOGGING # force import

The second line is needed, as the first line is a 'lazy import' and will
only read the settings and configure logging  when you access the first
time a element of settings.
I just used settings.LOGGING, as it should always exist, when you try to
log.


I am not sure whether there is a more elegant solution.
When I asked this question recently on this list, I didn't receive any
other suggestions.




> And that doesn't work either, unless I put it right in settings.py.. in
> which case it appears 4 times, because, from what I understand,
> settings.py gets loaded that many times.  But then this doesn't work in
> the views.py/backend .. perhaps because the dictConfig gets loaded after
> settings.py is run?  I don't know.
> 
> I'm hoping for someone to give me a heads up about what I'm missing
> here.  Django's been pretty easy to deal with until I started to look
> into logging.
> 
> Thanks.
> 
> -- 
> 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/-/JvmqgFNPMu4J.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.


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

Reply via email to