Django 登录 Heroku [英] Django logging on Heroku

查看:30
本文介绍了Django 登录 Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过好几次了,但我就是无法解决这个问题.我已经花了半天时间尝试了几十种组合,现在又尝试了,还是不行.

I know this question was already asked several times, but I just can't get it to work. I already spent half a day trying dozens of combinations, and again now and it is still not working.

在我的代码中,我记录了几个部分,例如在 try-except 中或从管理命令记录一些信息.我正在做一些非常正常的事情,即在多个本地安装和一些 Nginx 服务器上工作.

In my code, I am logging at several parts, like within a try-except or to log some infos from management commands. I'm doing just very normal stuff, that is working on several local installs and on some Nginx servers.

像这样的python文件:

A python file like this one :

import logging
logger = logging.getLogger(__name__)
logger.info('some important infos')

以下是最小的settings.py(我尝试没有流指示,没有指定记录器,使用命名记录器,几乎所有可能的组合,我还尝试了更复杂的组合)

With following as minimal settings.py (I tried without stream indication, without the loggers specified, with named loggers, almost all possible combinations and I also tried much more complex ones)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'INFO',
        },
        '': {
            'handlers': ['console'],
            'level': 'INFO',
        }
    }
}

然后也简单地从 shell heroku run python

Then also simply tested from the shell heroku run python

import logging
level = logging.INFO
handler = logging.StreamHandler()
handler.setLevel(level)
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
logger = logging.getLogger('info')
logger.addHandler(handler)
logger.setLevel(level) #even if not required...
logger.info('logging test')

最后一个可能在控制台中显示为print"语句,但无论是在此处还是在命令或服务器中,heroku 日志中都不会显示任何内容....

The last one may show up as "print" statement in the console, but neither here nor from the command or the server, nothing never shows up in heroku logs....

实际上我显示了一些条目,应用程序日志如下,但不是我的:

actually I have some entries showing up, application logs like following, just not mine:

2013-09-20T15:00:16.405492+00:00 heroku[run.2036]: Process exited with status 0
2013-09-20T15:00:17+00:00 app[heroku-postgres]: source=HEROKU_POSTGRESQL_OLIVE sample[...]
2013-09-20T14:59:47.403049+00:00 heroku[router]: at=info method=GET path=/
2013-09-20T14:59:16.304397+00:00 heroku[web.1]: source=web.1 dyno=heroku [...]

我还尝试使用多个插件查找条目.我在开始时有一段时间是 newrelic,然后我也从 WSGI 启动中停用了它.不记得当时好不好用,newrelic的免费试用期比较短.

I also tried to look for entries using several addons. I had for a moment at the beginning newrelic, which I then also deactivated from the WSGI start-up. I don't remember if at that time it worked well, the free test period of newrelic is rather short.

好吧,我不知道我还能尝试什么...感谢您的提示

Well, I don't know what I could try else... Thanks for any hint

推荐答案

从 Django 登录 Heroku 一开始可能会很棘手,但实际上设置起来并没有那么可怕.

Logging on Heroku from Django can be tricky at first, but it's actually not that horrible to get set up.

以下日志定义(进入您的设置文件)定义了两个格式化程序.详细的匹配 Heroku 本身使用的日志记录格式.它还定义了两个处理程序,一个空处理程序(不需要使用)和一个控制台处理程序——控制台处理程序是您想要与 Heroku 一起使用的.这样做的原因是 Heroku 上的日志记录通过一个简单的流记录器工作,记录任何输出到 stdout/stderr.最后,我定义了一个名为 testlogger 的记录器 - 记录定义的这一部分与记录定义一样正常.

This following logging definition (goes into your settings file) defined two formatters. The verbose one matches the logging format Heroku itself uses. It also defines two handlers, a null handler (shouldn't need to be used) and a console handler - the console handler is what you want to use with Heroku. The reason for this is that logging on Heroku works by a simple stream logger, logging any output made to stdout/stderr. Lastly, I've defined one logger called testlogger - this part of the logging definition goes like normal for logging definitions.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': ('%(asctime)s [%(process)d] [%(levelname)s] ' +
                       'pathname=%(pathname)s lineno=%(lineno)s ' +
                       'funcname=%(funcName)s %(message)s'),
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'testlogger': {
            'handlers': ['console'],
            'level': 'INFO',
        }
    }
}

接下来,如何使用这个.在这个简单的例子中,您可以在 Django 项目的任何其他文件中执行以下操作,以写入我们定义的特定记录器 (testlogger).请记住,根据我们的设置文件中的记录器定义,将输出 INFO 或以上的任何日志消息.

Next up, how to use this. In this simple case, you can do the following in any other file in your Django project, to write to this specific logger we defined (testlogger). Remember that by the logger definition in our settings file, any log message INFO or above will be output.

import logging
logger = logging.getLogger('testlogger')
logger.info('This is a simple log message')

这篇关于Django 登录 Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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