Django logging to console

前端 未结 3 729
一生所求
一生所求 2020-12-24 06:14

I\'m trying to set up a logger that will log to the console (I want this because I\'m using Heroku with Papertrails (Heroku\'s logging addon) and stuff written to the consol

3条回答
  •  爱一瞬间的悲伤
    2020-12-24 07:07

    I am writing this for easy understanding dummies like me.

    In settings.py

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
            },
        },
        'loggers': {
            'app_api': {
                'handlers': ['console'],
                'level': 'INFO',
            },
        },
        }
    

    Somewhere in your application views

    import logging
    logger = logging.getLogger('app_api') #from LOGGING.loggers in settings.py
    
    try:
        one = 1/0
    except Exception as e:
        logger.error(e)
    

提交回复
热议问题