芹菜日志写入旧日志文件


import logging
from datetime import date
import time
from datetime import time
from datetime import datetime
from configuration import get_config

class Logger:
"""
Convenience class to read the service.ini and set the logger.
"""
_logger = None
@staticmethod
def get_file_path():
"""
Gets the current log path.
:return: log path
:rtype: str
"""
return 'logs//' + str(datetime.now().minute) + '.log'
#return 'logs//' + date.today().strftime("%b-%d-%Y") + '.log'
@staticmethod
def get_log_level():
"""
Returns the log level for the application
:return: log level
:rtype: int
"""
return {
'debug': logging.DEBUG,
'info': logging.INFO,
'error': logging.ERROR,
'warning': logging.WARNING,
'critical': logging.CRITICAL,
}[get_config('logger', 'level').lower()]
@classmethod
def get_logger(cls):
"""
Get the configured singleton logger. logging is a thread safe class.
:return: returns logging object
:rtype: logging
"""
name = cls.get_file_path()
if not cls._logger or name != cls._logger.__dict__['name']:
cls._logger = logging.getLogger(name)
cls._logger.setLevel(cls.get_log_level())
fh = logging.FileHandler(name)
#fh.setLevel(cls.get_log_level())
formatter = logging.Formatter('%(asctime)s [%(filename)s - %(lineno)s] -%(funcName)s '
'- %(levelname)s - %(message)s')
fh.setFormatter(formatter)
cls._logger.addHandler(fh)
return cls._logger





"""
Provides the logging system for celery task scope.
"""
import logging
from celery.app.log import TaskFormatter
from celery.signals import after_setup_logger
from log import Logger_new

@after_setup_logger.connect
def setup_loggers(logger, *args, **kwargs):
"""
Sent after the setup of every global logger. Used to augment logging configuration.
:param logger: logger from the celery system
:type logger: logging.RootLogger
:param args: celery arguments
:type args: tuple
:param kwargs: celery environment details
:type kwargs: dict
:return: no return
"""
file_handle = logging.FileHandler(Logger_new.get_file_path())
file_handle.setFormatter(TaskFormatter('%(asctime)s - %(task_id)s - %(task_name)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handle)
logger.setLevel(Logger_new.get_log_level())

def get_task_logger():
"""
This function will return a logger used for logging task details.
:return: a logger object
:rtype: logging.Logger
"""
return logging.getLogger('task_logger')

所以,这是我的代码,第一个文件具有常规日志记录设置,第二部分具有芹菜任务记录器配置。每次我点击 api-s 时,每分钟都应该根据代码生成一个新文件。但这不会发生。所有芹菜任务日志信息都写入芹菜启动后立即生成的文件中。但是我希望每次点击 API-s 时都将信息存储在一个新文件中,即每分钟应该生成一个包含最近任务详细信息的新文件。

据我所知,Celery 正在删除所有记录器并安装自己的记录器。如果要添加自定义记录器,则需要在setup_logging信号处理程序中执行此操作,如下所示:

import celery.signals
@celery.signals.setup_logging.connect
def on_celery_setup_logging(**kwargs):
# setup logging here
pass

我从不需要这个,所以我不知道所有必要的细节。有关此内容的更多信息,请参阅 Celery 文档的日志记录部分。

相关内容

  • 没有找到相关文章

最新更新