简单登录到django 1.3+的文件示例 [英] Simple Log to File example for django 1.3+

查看:88
本文介绍了简单登录到django 1.3+的文件示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发行说明说:


Django 1.3为Python的日志记录模块添加了框架级的
支持。

Django 1.3 adds framework-level support for Python’s logging module.

很好我想利用这一点。不幸的是,文档不会将它全部交给我在一个银盘上完整的工作示例代码的形式,演示了这是多么简单和有价值。

That's nice. I'd like to take advantage of that. Unfortunately the documentation doesn't hand it all to me on a silver platter in the form of complete working example code which demonstrates how simple and valuable this is.

如何设置这个时髦的新功能,以便我可以用

How do I set up this funky new feature such that I can pepper my code with

logging.debug('awesome stuff dude: %s' % somevar)

,并查看文件/tmp/application.log填写

and see the file "/tmp/application.log" fill up with

18:31:59 Apr 21 2011 awesome stuff dude: foobar
18:32:00 Apr 21 2011 awesome stuff dude: foobar
18:32:01 Apr 21 2011 awesome stuff dude: foobar

默认的Python日志记录与此框架级支持有什么区别? p>

What's the difference between the default Python logging and this 'framework-level support'?

推荐答案

我真的非常喜欢这里,这里是你的工作示例!认真的这是真棒!

I truly love this so much here is your working example! Seriously this is awesome!

开始把它放在你的 settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': SITE_ROOT + "/logfile",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'MYAPP': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    }
}

现在这是什么意思?


  1. 格式化器我喜欢它出现与./manage.py runserver相同的样式
  1. Formaters I like it to come out as the same style as ./manage.py runserver
  2. Handlers - I want two logs - a debug text file, and an info console. This allows me to really dig in (if needed) and look at a text file to see what happens under the hood.
  3. Loggers - Here is where we nail down what we want to log. In general django gets WARN and above - the exception (hence propagate) is the backends where I love to see the SQL calls since they can get crazy.. Last is my app were I have two handlers and push everything to it.

现在如何启用MYAPP使用它...

Now how do I enable MYAPP to use it...

按<一个href =https://docs.djangoproject.com/en/dev/topics/logging/#using-logging =noreferrer>文档将其放在文件的顶部(views.py )..

Per the documentation put this at the top of your files (views.py)..

import logging
log = logging.getLogger(__name__)

然后得到一些东西。

log.debug("Hey there it works!!")
log.info("Hey there it works!!")
log.warn("Hey there it works!!")
log.error("Hey there it works!!")

一个href =https://docs.djangoproject.com/en/dev/topics/logging/#making-logging-calls =noreferrer>这里和纯python 这里

Log levels are explained here and for pure python here.

这篇关于简单登录到django 1.3+的文件示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆