gpt4 book ai didi

python - 我可以在 Celery 任务中使用通用的 logging.Logger() 吗?

转载 作者:太空狗 更新时间:2023-10-29 21:06:36 26 4
gpt4 key购买 nike

医生说

A special logger is available named “celery.task”, you can inherit from this logger to automatically get the task name and unique id as part of the logs.

这还不够。有更详细的信息吗?具体来说,它默认定义了哪些处理程序和格式字符串?我为什么要继承它?我可以改用通用的 logging.Logger() 吗?从 celery 任务(不是 Django)记录到文件的最佳实践是什么?等谢谢。

最佳答案

Can I use a generic logging.Logger() instead?

是的,您可以使用通用的 Python/Django 记录器。而不是 logger = get_task_logger(__name__) 只需选择 getLogger:

import logging
logger = logging.getLogger(__name__)

Why would I want to inherit from it?

使用 celery.task 的优势是:

automatically get the task name and unique id as part of the logs.

它本身使用标准 Python logging图书馆。


This is hardly enough. Is there a more detailed info available? Specifically, what handlers and format strings it defines by default?

默认情况下它使用 WatchedFileHandler来自 logging.handlers您可以在 celery.app.log 中查看文档,它在 setup_handler 中设置方法。


What are the best practices for logging to a file from celery tasks (not Django)? etc

<强>1。直接在模块中 - 您可以简单地将所有内容放在模块的顶部 - 定义一个处理程序,并在获取记录器时分配它:

import logging

# ---- set up logging to file ---
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='/temp/myapp.log',
filemode='w')
# --- define a Handler ---
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# --- set a format which is simpler for console use ---
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# --- tell the handler to use this format ---
console.setFormatter(formatter)
# --- add the handler to the logger ---
logging.getLogger('').addHandler(console)

<强>2。使用 logging_config 字典 - dictConfig :一种更美观的做法。

import logging
from logging.config import dictConfig

logging_config = dict(
version = 1,
formatters = {
'f': {'format':
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'}
},
handlers = {
'h': {'class': 'logging.StreamHandler',
'formatter': 'f',
'level': logging.DEBUG}
},
root = {
'handlers': ['h'],
'level': logging.DEBUG,
},
)

dictConfig(logging_config)

logger = logging.getLogger()
logger.debug('often makes a very good meal of %s', 'visiting tourists')

<强>3。使用单独的日志记录格式文件 - *.ini :

[loggers]
# list of loggers
keys=root,log02

[handlers]
# list of handlers
keys=hand01,hand02

[formatters]
# list of formatters
keys=form01,form02

[logger_root]
# config for 'root' logger
level=NOTSET
handlers=hand01

[handler_hand01]
# config for handler hand01
class=StreamHandler
level=NOTSET
formatter=form01
args=(sys.stdout,)

[handler_hand02]
## config for handler hand02
class=FileHandler
level=DEBUG
formatter=form02
args=('python.log', 'w')

[formatter_form01]
# config for formatter form01
format=F1 %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter

<强>4。一个单独的类 -最后但同样重要的是,一个更优雅的解决方案是将所有这些作为单独的模块(类),并在需要时使用特定标志导入它:

import logging
from config import LOG, LOGGING

class Logger:
def __init__(self, logf=None, logger_name=None, debug=None, rotation=None):
self.logfile = logf
self.logger_name = logger_name if logger_name else logf
self.logger = self.get_logger(rotation=rotation) if rotation else self.get_logger()
self.debug = debug if debug else False
self.debug = debug if debug else LOG["debug"]

def logf(self, filename=None):
if filename is None:
filename = LOG["file"]
return "%s%s" % (LOG["dir"], filename)

def get_logger(self, rotation=False):
logger = logging.getLogger(self.logger_name)
logf = self.logf(self.logfile)
if rotation:
from logging.handlers import TimedRotatingFileHandler
self.handler = TimedRotatingFileHandler(logf, when='midnight', interval=1, backupCount=10)
else:
self.handler = logging.FileHandler(logf)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
#formatter = "%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s - %(message)s",
self.handler.setFormatter(formatter)
logger.addHandler(self.handler)
logger.setLevel(logging.DEBUG)
return logger

关于python - 我可以在 Celery 任务中使用通用的 logging.Logger() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com