- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
医生说
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/
作为一个新人中的菜鸡,最近在看一个项目的代码其中有这么一段看得我很是费解 public class AssembleMsgService { private static Logger logger
我已更改 Gradle 构建脚本以使用 Spring 2.0.1 而不是 1.5.9。当我运行 gradle build 时,出现错误:找不到符号 Logger logger = Logger.get
这是我的示例代码: public class Logs { private static Logs logHandler; public static Logs handler() {
我目前正在使用 Java 日志记录。我有一个记录器 cwh.A 和 cwh.B。是否可以通过 Java 从记录器 cwh 获取这些记录器,而不知道它们的名称? 最佳答案 可以通过日志管理器获取所有记录
在我的应用程序中,有一次我将 java.util.logging 的根 Logger 及其所有处理程序设置为 Level.FINEST,如下所示: Logger.getLogger("").setLe
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
我刚刚将 Java 插件升级到 1.7.0_25,这导致我几年前编写的一个小程序出现 fatal error (此后一直在维护)。 在小程序的 init() 方法的早期,我调用了 logger = L
logger.debug 和 logger.info 有什么区别? logger.debug什么时候打印? 最佳答案 我建议你看看这篇名为 "Short Introduction to log4j"
Fixed I tried reinstalling the plugin but that didn't work so i completely cleared my inteljii idea
我定义了一个记录器实例如下: private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); 我有一个要记录的
我正在 Windows 下使用 Python 3.4.2。就我而言, import logging logger = logging.getLogger('logger') logger.setLev
我目前正在使用 C# 开发一个新的 Prism 项目,并想在我的 Bootstrapper 类中创建一个 NLog Logger。不幸的是,无论我做什么,它都拒绝构建,并告诉我... 'Logger.
我已经有几年没有使用 Log4J 了。我现在正在使用 org.jboss.logging 包在 JBoss 5 下开发一个应用程序。当我以前这样做时,将 logger.info() 消息包含在日志记录
ActiveRecord::Base.logger = Logger.new(STDOUT) 的 redis 等价物是什么?我想看看redis在做什么。 最佳答案 $redis.client.logg
我正在尝试使用 log4js 创建日志文件。完成所有配置后运行 Node app.js 时,我收到以下错误。 错误: TypeError: logger.setLevel is not a funct
我在一个拥有大约 50 多名开发人员的 IT 部门工作。它曾经有大约 100 多名开发人员,但由于经济衰退而被削减。 当我们的部门规模扩大时,我们雄心勃勃地努力建立一个特殊的架构小组。 这个小组决定做
我正在尝试找出跨多个模块使用 python 日志记录的最佳实践。我在这里看到:http://docs.python.org/2/howto/logging#logging-from-multiple-
我想将 Logger 注入(inject)到在 WildFly 10 上运行的 Maven 动态 Web 项目中的 bean 中。 我在 POM 中添加了依赖项。 WildFly 中提供了范围:
Java 9 引入了一个新的记录器,即 java.lang.System.Logger但我们总是有java.util.logging.Logger . 此记录器有什么新功能以及它有何改进? 最佳答案
我在 eclipse Mars 中配置了我的环境,但是 log4j 出现错误。我使用 spring 4 和 Maven 作为依赖管理器。部分代码: Eclipse 在这一行显示了一个错误,等于后的第二
我是一名优秀的程序员,十分优秀!