如何在芹菜中添加日志处理程序?

时间:2017-08-28 20:52:21

标签: python asynchronous logging celery celery-task

所以我的task.py在这里是一个exerpt:

import builtins
import logging
import os
import urllib
import inspect

from celery import Celery
from common.environment_helper import EnvironmentHelper
from config import log

# Logging functionality
logger = logging.getLogger(__name__)
EnvironmentHelper.set_environment(logger)


app = Celery('tasks', broker=BROKER__URL, broker_transport_options=BROKER_TRANSPORT_OPTIONS)
app.conf.CELERY_ACCEPT_CONTENT = ['json']
app.conf.CELERY_TASK_SERIALIZER = 'json'
app.conf.CELERYD_PREFETCH_MULTIPLIER = 1
app.log.already_setup=True
app.conf.CELERY_ENABLE_REMOTE_CONTROL = False
app.conf.CELERY_DEFAULT_QUEUE = queue_name

@app.task
def convert_file(file_conversion_json):
    file_conversion_json_copy = file_conversion_json.copy()

所以基本上我想从字典中取一个值并将其添加到我的日志中。我在实际应用程序中使用以下代码成功完成了此操作:

import uuid
import logging
import flask


# Generate a new request ID, optionally including an original request ID
def generate_request_id(original_id):
    new_id = uuid.uuid4()

    if not original_id:
        return new_id
    else:
        new_id = original_id
    return new_id

# Returns the current request ID or a new one if there is none
# In order of preference:
#   * If we've already created a request ID and stored it in the flask.g context local, use that
#   * If a client has passed in the X-Request-Id header, create a new ID with that prepended
#   * Otherwise, generate a request ID and store it in flask.g.request_id
def request_id():
    if getattr(flask.g, 'request_id', None):
        return flask.g.request_id

    headers = flask.request.headers
    original_request_id = headers.get("X-Request-Id")
    new_uuid = generate_request_id(original_request_id)
    flask.g.request_id = new_uuid

    return new_uuid


class RequestIdFilter(logging.Filter):
    # This is a logging filter that makes the request ID available for use in
    # the logging format. Note that we're checking if we're in a request
    # context, as we may want to log things before Flask is fully loaded.
    def filter(self, record):
        record.request_id = request_id() if flask.has_request_context() else ''
        return True

............

# log.py
import logging.config
import os

LOGGER_CONFIGURATION = {
    'version': 1,
    'filters': {
        'request_id': {
            '()': 'config.utils.RequestIdFilter',
        },
    },
    'formatters': {
        'standard': {
            'format': '%(asctime)s - %(name)s.%(module)s.%(funcName)s:%(lineno)d - %(levelname)s - %(request_id)s - %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'INFO',
            'filters': ['request_id'],
            'formatter': 'standard'
        }
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level':'INFO',
        },
        'app': {
            'handlers': ['console'],
            'level':'INFO',
        },
    }
}

logging.config.dictConfig(LOGGER_CONFIGURATION)

但它对芹菜不起作用,我不知道如何像这样添加变量。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

所以我想出如果我把它添加到我的任务中:

logFormatter = logging.Formatter('%(asctime)s - %(name)s.%(module)s.%(funcName)s:%(lineno)d - %(levelname)s - Request ID:'+ file_conversion_json['x_request_id'] + ' - %(message)s')
rootLogger = logging.getLogger()
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)

它会将我想要的内容添加到日志输出中,但现在它复制了我的日志消息,我在日志输出中收到了两次消息....