celery的log如何传递给django,由django管理

celery自己管理log目录

celery worker --autoscale=4,1 --app=erebus.celeryapp:app -l info -f /home/admin/output/erebus/logs/worker.log -Q erebus,celery

celery beat --loglevel info --app erebus.celeryapp:app --logfile /home/admin/output/erebus/logs/beat.log

celery flower -A erebus.celeryapp:app --broker=sentinel://:password@ip:26379/6 --basic_auth=flower:flower,user:password

日志给django管理

配置EREBUS_WORKER_HIJACK_ROOT_LOGGER=False 参考配置的日志选项:http://docs.celeryproject.org/en/latest/userguide/configuration.html

django的日志配置:

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'verbose': {
        # 后缀d表示数据格式是整数,s表示数据格式是字符串
        'format': '[%(levelname)s] [%(asctime)s] [%(module)s] %(filename)s:%(lineno)d %(funcName)s '
                  '%(processName)s:[%(process)d] %(threadName)s:[%(thread)d] %(message)s'
        # 'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
        # 'style': '{',
    },
    'simple': {
        'format': '[%(levelname)s] [%(asctime)s] %(message)s',
        # 'format': '[%(asctime)s] %(message)s',
        # 后缀d表示数据格式是整数,s表示数据格式是字符串
        # 'format': '[%(levelname)s] [%(asctime)s] [%(module)s] %(filename)s:%(lineno)d %(funcName)s '
        #           '%(processName)s:[%(process)d] %(threadName)s:[%(thread)d] %(message)s',
        # 'style': '{',
    },
    'standard': {
        # 'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
        'format': '{asctime} [{levelname:7}] {name:30}: {message}',
        # 设置上面格式样式;{levelname:3}是告警级别,至少显示7个字符,少则补空格;多则正常显示
        # 这里style选择{,是指{asctime}这种形式。
        # 如果选择%,则是%(asctime)s这种形式。
        # 还有一种选择,是$,是$asctime或${asctime}这种形式。
        'style': '{',
        # 设置时间格式
        'datefmt': '%Y-%m-%d %H:%M:%S',
    },
    'operation': {
        'format': '%(message)s'
    }
},
# 'filters': {
#     # 'special': {
#     #     '()': 'erebus.logging.SpecialFilter',
#     #     'foo': 'bar',
#     # },
#     'require_debug_true': {
#         '()': 'django.utils.log.RequireDebugTrue',
#     },
# },

# Handler是决定如何处理logger中每一条消息的引擎。它描述特定的日志行为,比如把消息输出到屏幕、文件或网络socket。
# 和 logger 一样,handler 也有日志级别的概念。如果一条日志记录的级别不匹配或者低于 handler 的日志级别,
# 对应的消息会被 handler 忽略。
'handlers': {
    'default': {
        # 'level': 'WARNING',
        # 'class': 'logging.handlers.RotatingFileHandler',
        # 'filename': '{}/default.log'.format(BASE_LOG_DIR),
        # 'maxBytes': 1024*1024*5*20,  # 5*20 MB
        # 'maxBytes': 1024*5,  # 5 KB
        # 保留7天的日志,没份5M,5份大概是一个小时的日志内容,主要是kafka日志
        # 'backupCount': int(5*1*24*7/20),
        # 'formatter': 'standard',

        'level': 'WARNING',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'app.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,

    },
    'pipeline': {
        # 'level': 'DEBUG',
        # 'class': 'logging.handlers.RotatingFileHandler',
        # 'filename': '{}/pipeline.log'.format(BASE_LOG_DIR),
        # 'maxBytes': 1024*1024*5*20,  # 5*20 MB
        # 'maxBytes': 1024*5,  # 5 KB
        # 保留7天的日志,没份5M,5份大概是一个小时的日志内容,主要是kafka日志
        # 'backupCount': int(5*1*24*7/20),
        # 'formatter': 'standard',

        'level': 'DEBUG',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'pipeline.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'kafka': {
        # 'level': 'DEBUG',
        # 'class': 'logging.handlers.RotatingFileHandler',
        # 'filename': '{}/kafka.log'.format(BASE_LOG_DIR),
        # 'maxBytes': 1024*1024*5*20,  # 5*20 MB
        # 'maxBytes': 1024*5,  # 5 KB
        # 保留7天的日志,没份5M,5份大概是一个小时的日志内容,主要是kafka日志
        # 'backupCount': int(5*1*24*7/20),
        # 'formatter': 'standard',

        'level': 'INFO',
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'kafka.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'output_to_server': {
        'level': 'WARNING',  # 忽略debug/info信息
        'class': 'logging.handlers.HTTPHandler',
        'host': '127.0.0.1:8088',
        'url': '/api/v1/log',
        # 使用GET方法遇到url最大长度限制
        'method': 'POST',
        'formatter': 'verbose',
    },
    'jenkins': {
        'level': 'DEBUG',  # 忽略debug信息
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': '{}/{}.log'.format(BASE_LOG_DIR, 'jenkins'),
        'formatter': 'simple' if DEBUG else 'verbose',
        'encoding': 'utf8',
        'maxBytes': 1024*1024*5*20,  # 5*20 MB
        'backupCount': int(5*1*24*7/20),
    },
    'django': {
        # 'level': 'INFO',  # 忽略debug信息
        # 'class': 'logging.handlers.RotatingFileHandler',
        # 'filename': '{}/{}.log'.format(BASE_LOG_DIR, conf.get('log', 'name')),
        # 'formatter': 'simple' if DEBUG else 'verbose',
        # 'encoding': 'utf8',
        # 'maxBytes': 1024*1024*5*20,  # 5*20 MB
        # 'backupCount': int(5*1*24*7/20),

        'level': 'INFO',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': '{}.log'.format(conf.get('log', 'name')),
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'gunicorn_error': {
        'level': 'INFO',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'gunicorn_error.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'gunicorn_access': {
        'level': 'INFO',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'gunicorn_error.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'celery': {
        'level': 'INFO',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'celery.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'flower': {
        'level': 'INFO',  # 忽略debug信息
        'class': 'utils.graylog.graylog_django.GraylogHandler',
        'app': 'erebus',
        'file': 'flower.log',
        'host': GRAYLOG_HOST,
        'port': GRAYLOG_PORT,
        'localname': GRAYLOG_SOURCE,
    },
    'console': {
        'level': 'DEBUG' if DEBUG else 'WARNING',  # 所有的日志都会被输出到console
        # 'filters': ['require_debug_true'],
        'class': 'logging.StreamHandler',
        'formatter': 'simple'
    },
    'operation': {
        'level': 'INFO',
        'class': 'logging.FileHandler',
        'filename': '{}/{}.log'.format(BASE_LOG_DIR, 'operation'),
        'formatter': 'operation',
        'encoding': 'utf8'
    },
    'test': {
        'level': 'INFO',
        'class': 'logging.FileHandler',
        'filename': '{}/{}.log'.format(BASE_LOG_DIR, 'test'),
        'formatter': 'standard',
        'encoding': 'utf8'
    }
    # 'mail_admins': {
    #     'level': 'ERROR',
    #     'class': 'django.utils.log.AdminEmailHandler',
    #     # 'filters': ['special']
    # }
},
'loggers': {

    # 可以通过使用空字符串:''来设置'catch all' logger
    # 在以下设置中,将所有日志事件级别为WARNING及以上的日志发送给日志服务器,但配置为'propagate': False日志事件除外,
    '': {
        'handlers': ['default', 'output_to_server'],
        # 'handlers': ['default'],
        # 这样情况下的level设置是无效的,所有级别的信息都会传给handlers处理,由handlers的level界别决定
        # 'level': 'ERROR',
        'propagate': False
    },
    # 记录所有jenkins的requests相关的日志;jenkins->requests->urllib3
    'urllib3': {
        'handlers': ['jenkins'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': True
    },
    # 记录所有kakfa相关的日志kafka.conn/kafka.client/kafka.coordinator.consumer
    'kafka': {
        'handlers': ['kafka'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': True
    },
    # 这里必须使用名字django和django.request,目的是为了捕获django框架的日志内容
    'django': {
        'handlers': ['django', 'console'],
        # 当 logger 处理一条消息时,会将自己的日志级别和这条消息的日志级别做对比。
        # 如果消息的日志级别匹配或者高于 logger 的日志级别,它就会被进一步处理。
        # 否则这条消息就会被忽略掉。当 logger 确定了一条消息需要处理之后,会把它传给 Handler。
        # 把INFO及以上级别的日志传给handlers,然后由handlers根据handlers的level进一步处理日志输出
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': True,  # 若值为False,表示日志不会传到上个层级,自然也不会传到default.log里
    },
    # 使用logger = logging.getLogger('django.request'), logger.info('info'),
    # 可以把日志输出到'handlers': ['django', 'console'],
    'django.request': {
        # 即使和django的handlers一样,level也一样,也并不会产生2次相同的日志内容,应该是个并集。
        'handlers': ['django', 'console'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        # 会把日志向django.request的上层django传播
        'propagate': True,
    },
    # sql语句
    'django.db.backends': {
        # 即使和django的handlers一样,level也一样,也并不会产生2次相同的日志内容,应该是个并集。
        'handlers': ['django', 'console'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        # 会把日志向django.request的上层django传播
        'propagate': True,
    },
    # 'erebus.custom': {
    #     'handlers': ['console', 'mail_admins'],
    #     'level': 'INFO',
    #     # 'filters': ['special']
    # },
    # 名字随意起,用时,使用logger = logging.getLogger(conf.get('log', 'name'))获取,传到相应的loggers里就可以
    'operation': {
        'handlers': ['operation'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': True,
    },
    'test': {
        'handlers': ['console', 'test'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': False,  # 不要传给上一层级
    },
    'pipeline': {
        'handlers': ['pipeline'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': False,  # 不要传给上一层级
    },
    'gunicorn.error': {
        'handlers': ['gunicorn_error'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': False,  # 不要传给上一层级
    },
    'gunicorn.access': {
        'handlers': ['gunicorn_access'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': False,  # 不要传给上一层级
    },
    'celery': {
        'handlers': ['celery'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': False,  # 不要传给上一层级
    },
    'flower': {
        'handlers': ['flower'],
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': False,  # 不要传给上一层级
    }
}
}
posted @ 2019-07-10 23:58  脚本小娃子  阅读(1706)  评论(0编辑  收藏  举报