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,  # 不要傳給上一層級
    }
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM