简体   繁体   English

Django日志记录项目和应用程序名称空间

[英]Django logging project and app namespace

I've an issue with Django logging. Django日志记录出现问题。 By the way answers to this question will help me to clarify how Django namespaces work. 顺便说一句,这个问题的答案将帮助我阐明Django名称空间的工作方式。

Here is the structure of my project: 这是我的项目的结构:

MyProject:
    -App1:
         ....
         views.py
    -App2:
         ....
    urls.py
    settings.py

I like to log all messages in one file. 我喜欢将所有消息记录在一个文件中。 Then I've setup in settings.py the following logger: 然后,我在settings.py中设置了以下记录器:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
            'verbose': {
                    #'format': '%(levelname)-8s %(remote_addr)-15s %(path_info)s %(asctime)s %(name)-20s %(funcName)-15s %(message)s'
                    'format': '%(levelname)-8s %(asctime)s %(name)-20s %(funcName)-15s %(message)s'
                    },
            },
    'handlers': {
            'normal': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'formatter': 'verbose',
            'filename': os.path.join('C:/dev/Instantaneus/Instantaneus/html/static', 'log', 'normal.log')
        },
            'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
            },
        },
    'loggers': {
        'MyProject': {
                'handlers': ['normal','console'],
                'level': 'DEBUG',
                #'filters': ['request'],
                'propagate': True,
                },
            }
       }

In urls.py: 在urls.py中:

from MyProject.App1.views import EvenementDetailView,
....
url(r'App1/(?P<pk>\d+)/$', login_required(EvenementDetailView.as_view(model=Evenement)), name='evenement_details'),

and in App1/views.py: 并在App1 / views.py中:

from django.views.generic import DetailView
import logging
logger = logging.getLogger(__name__)

....

class EvenementDetailView(DetailView):
    print __name__
    model=Evenement
    ....
    logger.debug('blabla')

In the browser, when I call http://localhost/App1/3 , the following appears in the console: 在浏览器中,当我调用http://localhost/App1/3 ,控制台中将显示以下内容:

DEBUG    2012-01-18 14:59:04,503 Myproject.evenements.views EvenementDetailView blabla
MyProject.evenements.views
evenements.views

Then my question is why the print __name__ code is executed twice and most important why outputs are not the same ? 然后我的问题是为什么print __name__代码执行两次,最重要的是为什么输出不相同?

I suppose that the DEBUG log appear only one time because evenement.views can't propagate to MyProject because in this case root is evenements 我猜想DEBUG日志只出现一次,因为evenement.views无法传播到MyProject,因为在这种情况下,根目录是evenements

Any ideas ? 有任何想法吗 ?

Solution, not with in depth explanation but it works: 解决方案,虽然没有深入的解释,但是可以起作用:

In my urls.py, I had a line url(r'App1/(?P<pk>\\d+)/activate/$', 'app1.views.activate') where 'activate' is a function in App1/views.py. 在我的urls.py中,我有一行url(r'App1/(?P<pk>\\d+)/activate/$', 'app1.views.activate') ,其中“ activate”是App1 / views中的函数.py。 I've change 'App1.views.activate' in 'MyProject.app1.views.activate' and it works fine. 我在'MyProject.app1.views.activate'更改了'App1.views.activate' 'MyProject.app1.views.activate' ,它可以正常工作。 I've only one line in the console for print __name__ . 我在控制台中只有一行可以print __name__ I think I've only one line because of the 'disable_existing_loggers': True, but what I can't explain is that this solution made my views.py only parse one time instead of two times before. 由于'disable_existing_loggers',我认为我只有一行:是,但是我无法解释的是,该解决方案使我的views.py仅解析一次,而不是之前解析两次。 To be sure of that I've added a print "blabla" at the beginning of the file. 为确保这一点,我在文件开头添加了print "blabla" In first case he's printed two times and only onces in second case. 在第一种情况下,他打印了两次,而在第二种情况下只打印了一次。

Fair warning: I'm not sure this is correct, and I'm basing some of this on stuff I remember reading a long time ago, but can't find in google now. 合理的警告:我不确定这是正确的,并且我将其中的一些内容作为我很久以前记得阅读的内容的基础,但是现在在Google中找不到。

What you're seeing is a side-effect of how the python import mechanism works. 您所看到的是python导入机制工作方式的副作用。 When a module is imported, it's put into sys.modules , however, when it's possible to import the module under two different dotted paths (in this case, with or without MyProject) it can be imported twice, once under each __name__ . 导入模块时,会将其放入sys.modules ,但是,当可以在两个不同的虚线路径(在这种情况下,使用或不使用MyProject)下导入模块时,可以将其导入两次,一次在每个__name__

The underlying fix is to make sure MyProject it not on sys.path - the directory containing MyProject should be, but not MyProject itself. 根本的解决方法是确保MyProject不在sys.path - 包含 MyProject的目录应该位于MyProject本身,而不是MyProject本身。 You can verify that this is done by starting a manage.py shell and making sure import evenements fails. 您可以通过启动manage.py shell并确保import evenements失败来验证此操作是否完成。 There are some Django internals in manage.py that could make this difficult - but the last time I ran into this was back around 1.0 or 1.1, so it may well have been fixed. manage.py中有一些Django内部结构可能会使此操作变得困难-但是我上次遇到该问题时,它的版本大约是1.0或1.1,因此很可能已得到修复。

There's an in-depth discussion here: 这里有一个深入的讨论:

http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

It's a long article, search for "two different names". 这是一篇很长的文章,搜索“两个不同的名称”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM