Re: settings.py gets imported twice
Am Dienstag, 6. November 2007 07:00 schrieb Matti Haavikko: > Here's an alternative solution - it's based on the idea that the > handlers are added to the logger only once. (getLogger returns the same > logger instance each time). > > import logging > logger=logging.getLogger("dws") > if not logger.handlers: > ... > logger.addHandler() Hi, I used something like this, but used the root logger (getLogger()). If there is a call to logging.log(...) before you add your handler, python automatically inserts one for you. This means the if-statement above will be false (and addHandler() would not be called), even on the first run. Thomas --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: settings.py gets imported twice
Thomas Guettler wrote: > A found this solution. > > # file logconfig.py > import logging > if not hasattr(logging, "set_up_done"): > logging.set_up_done=False > > def set_up(myhome): > if logging.set_up_done: > return > logging.set_up_done=True > Here's an alternative solution - it's based on the idea that the handlers are added to the logger only once. (getLogger returns the same logger instance each time). import logging logger=logging.getLogger("dws") if not logger.handlers: ... logger.addHandler() --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: settings.py gets imported twice
Am Montag, 5. November 2007 10:55 schrieb Malcolm Tredinnick: > On Mon, 2007-11-05 at 10:46 +0100, Thomas Güttler wrote: > > Hi, > > > > settings.py gets imported twice. This is bad, if you want to > > set up the logging module, since you register the handler twice > > and you get all log messages twice. > > [...] > > It's actually fairly hard to avoid this for a variety of reasons. I > think we used to import it more than twice in some situations. Maybe we > still do. User-defined commands and processing the --settings option > becomes more fiddly, for example. So the answer is "don't rely on it > only being imported once". OK, maybe it should be documented. The usual way to use global variables with a different module: http://effbot.org/pyfaq/how-do-i-share-global-variables-across-modules.htm did not work for me, since the modul gets imported with different __name__'s. The first time __name__ is 'logconfig' the second time it is 'myproject.logconfig'. A found this solution. # file logconfig.py import logging if not hasattr(logging, "set_up_done"): logging.set_up_done=False def set_up(myhome): if logging.set_up_done: return logging.set_up_done=True Thomas --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: settings.py gets imported twice
On Mon, 2007-11-05 at 10:46 +0100, Thomas Güttler wrote: > Hi, > > settings.py gets imported twice. This is bad, if you want to > set up the logging module, since you register the handler twice > and you get all log messages twice. [...] It's actually fairly hard to avoid this for a variety of reasons. I think we used to import it more than twice in some situations. Maybe we still do. User-defined commands and processing the --settings option becomes more fiddly, for example. So the answer is "don't rely on it only being imported once". If you need something to be only done once, no matter how many times it is imported, you will need to set a flag somewhere. For example, you could set a global variable that indicates you have already set up your logging. Or set an indicator on some other class. For logging, I would think that a call to getLogger('name') would probably do the trick. If it returns nothing, you still need to do the setup. Regards, Malcolm -- If it walks out of your refrigerator, LET IT GO!! http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
settings.py gets imported twice
Hi, settings.py gets imported twice. This is bad, if you want to set up the logging module, since you register the handler twice and you get all log messages twice. Here are the stacktraces I created for debugging this: ===> python manage.py test admin import logconfig Traceback (most recent call last): File "manage.py", line 12, in ? import settings # Assumed to be in the same directory. Traceback (most recent call last): File "manage.py", line 19, in ? execute_manager(settings) File "/localhome/user/myproject/django/core/management/__init__.py", line 277, in execute_manager utility.execute() File "/localhome/user/myproject/django/core/management/__init__.py", line 225, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/localhome/user/myproject/django/core/management/__init__.py", line 177, in fetch_command self.project_directory)[subcommand] File "/localhome/user/myproject/django/core/management/__init__.py", line 86, in get_commands for app_name in settings.INSTALLED_APPS: File "/localhome/user/myproject/django/conf/__init__.py", line 28, in __getattr__ self._import_settings() File "/localhome/user/myproject/django/conf/__init__.py", line 57, in _import_settings self._target = Settings(settings_module) File "/localhome/user/myproject/django/conf/__init__.py", line 83, in __init__ mod = __import__(self.SETTINGS_MODULE, {}, {}, ['']) File "/localhome/user/myproject/settings.py", line 147, in ? This shnippet has a workaround: http://www.djangosnippets.org/snippets/16/ (I have not tested it yet) I think it would be better to avoid a second import. Any hints? Thomas -- Thomas Güttler, http://www.tbz-pariv.de/ Bernsdorfer Str. 210-212, 09126 Chemnitz, Tel.: 0371/5347-917 TBZ-PARIV GmbH Geschäftsführer: Dr. Reiner Wohlgemuth Sitz der Gesellschaft: Chemnitz Registergericht: Chemnitz HRB 8543 --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---