我有一个Django应用程序,我使用了celener&rabbitmq执行异步任务。我已经配置了芹菜将日志写入文件,当我执行时
celery -A project_name -b <broker_url> worker -l INFO
一切正常,日志都打印在控制台和文件中。
但是,当我使用-D选项执行前面提到的命令时,即分离工作程序时,文件不再附加日志。
我的日志记录配置:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"
},
},
'handlers': {
'null': {
'level': LOGGING_LEVEL,
'class': 'logging.NullHandler',
},
'logfile': {
'level': LOGGING_LEVEL,
'class': 'logging.handlers.RotatingFileHandler',
'filename': log_file_path,
'maxBytes': 5000000,
'backupCount': 2,
'formatter': 'standard',
},
'console': {
'level': LOGGING_LEVEL,
'class': 'logging.StreamHandler',
'formatter': 'standard'
},
},
'loggers': {
'django': {
'handlers': ['console'],
'propagate': True,
'level': "WARNING",
},
'django.db.backends': {
'handlers': ['console'],
'level': "INFO",
'propagate': False,
},
'celery': {
'handlers': ['console', 'logfile'],
'level': LOGGING_LEVEL,
},
}}
The celery.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.signals import setup_logging
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_service.settings')
app = Celery('my_service')
app.config_from_object('django.conf:settings', namespace='CELERY')
@setup_logging.connect
def config_loggers(*args, **kwags):
from logging.config import dictConfig
from board_ownership_service import settings
dictConfig(settings.LOGGING)
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
我是否缺少日志记录的某些配置?
==============================================
更新
我用TimedRotingFileHandler更改了日志文件处理程序,如下所示:
'logfile': {
'level': LOGGING_LEVEL,
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': log_file_path,
'when': 'D', # this specifies the interval
'interval': 1, # defaults to 1, only necessary for other values
'backupCount': 30, # how many backup file to keep, 10 days
'formatter': 'standard',
}
现在,我得到了一个名为logs_yyyyMMdd.logs的文件,当我执行时:
celery -A project_name -b <broker_url> worker -l INFO --logfile logs20210526.logs -D
我可以在文件中看到日志。我的问题是,当一天发生变化时会发生什么?为什么在使用-D执行芹菜时,不使用日志记录配置?
我让它按如下方式工作。
我在settings.py 中创建了一个函数
def config_logging():
timestamp = datetime.now().strftime('%Y%m%d')
filename = f"logs_{timestamp}.log"
log_file_path = join(LOGS_PATH, filename)
logging_level = "DEBUG" if env("DEBUG") else "INFO"
logging_config = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"
},
},
'handlers': {
'null': {
'level': logging_level,
'class': 'logging.NullHandler',
},
'logfile': {
'level': logging_level,
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': log_file_path,
'when': 'D', # this specifies the interval
'interval': 1, # defaults to 1, only necessary for other values
'backupCount': 30, # how many backup file to keep, 10 days
'formatter': 'standard',
},
'console': {
'level': logging_level,
'class': 'logging.StreamHandler',
'formatter': 'standard'
},
},
'loggers': {
'django': {
'handlers': ['console'],
'propagate': True,
'level': "WARNING",
},
'django.db.backends': {
'handlers': ['console'],
'level': "INFO",
'propagate': False,
},
'celery': {
'handlers': ['console', 'logfile'],
'level': logging_level,
},
}
}
dictConfig(logging_config)
return filename, logging_config
log_filename, LOGGING = config_logging()
logger = logging.getLogger("celery")
使用日志记录配置,然后在celery.py中我修改了代码:
@setup_logging.connect
def config_loggers(*args, **kwags):
config_logging()
logger = logging.getLogger("celery")
现在,我可以在文件中看到日志,即使在分离工作者时也是如此。