Logging in Django on Heroku not appearing

后端 未结 4 2338
借酒劲吻你
借酒劲吻你 2021-02-19 05:22

I have created an app on Heroku and I push my Django app to it.

I monitor the logs using heroku logs --tail to see them in real time.

Then, in my

相关标签:
4条回答
  • 2021-02-19 05:34

    Your Procfile and LOGGING configuration looks fine. Django configures the logger just before the apps are imported, so if you try to log even before than (for. e.g. from settings.py file), it will not work.

    EDIT:

    LOGGING config:

    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': {
        'ex_logger': {
                'handlers': ['console', ],
                'level': 'INFO',
            }
        }
    }
    

    adding following to settings.py won't log:

    import logging
    logger = logging.getLogger('ex_logger')
    logger.info("core.settings logger")  # won't work 
    

    adding to views.py should log:

    from django.http import HttpResponse
    import logging
    
    logger = logging.getLogger('ex_logger')
    logger.info("core.views logger")  # should work                                                                                                         
    
    def url_please(request):
        logger.info("path: %s" % request.path)  # should work                                                                                               
        return HttpResponse(request.path)
    
    0 讨论(0)
  • 2021-02-19 05:39

    Your Procfile is probably at fault here:

    If you want to have gunicorn log to stdout you have to use the --logfile=- command line option (you are missing the = !) according to this answer.

    So your entire Procfile should look like this:

    web: gunicorn myapp.wsgi --log-file=-

    EDIT:

    Since print statements are working for you, but logging is not, your logging setup is probably at fault. Make sure that you set up logging during the startup of your app (where are you calling dictConfig in your code?):

    import logging
    logging.config.dictConfig(LOGGING)
    logger = logging.getLogger('MYAPP')
    logger.info("Just testing")
    
    0 讨论(0)
  • As stated in previous replies, the logger should be set up in the entry point to your app. In the case of a django based web server using gunicorn, that entry point is most likely the wsgi.py file. Try adding the required configuration to that file, e.g. setting the global basic logger:

    import logging
    import os
    
    from dj_static import Cling
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "path.to.settings.py")
    
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(name)s %(levelname)-8s %(message)s",
    )
    
    0 讨论(0)
  • 2021-02-19 05:53

    I think if you drop a logger.error you would see something.

    I had the same problem and it turns out heroku's settings tool breaks the pre-existing LOGGING setup. The logger you are using is not registered with django but is making its own way to the console using python's standard logging system.

    Try doing:

    django_heroku.settings(locals(), logging=False)
    

    Or better yet, don't use it at all anymore since this package is no longer maintained anyway.

    0 讨论(0)
提交回复
热议问题