- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的程序中,我在开始时定义了一个类似于此的记录器:
def start_logger():
fh = logging.handlers.RotatingFileHandler('logger.log',
maxBytes=1000000,
backupCount=100)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
fh_fmt = '%(asctime)s %(levelname)8s %(message)s [%(filename)s:%(lineno)d]'
#fh_fmt = '%(asctime)s - %(funcName)s - %(levelname)s - %(message)s'
ch_fmt = '%(asctime)s %(levelname)8s %(message)s [%(filename)s:%(lineno)d]'
#ch_fmt = '%(funcName)s - %(levelname)s - %(message)s'
fh.setFormatter(logging.Formatter(fh_fmt))
ch.setFormatter(logging.Formatter(ch_fmt))
root = logging.getLogger()
root.addHandler(fh)
root.addHandler(ch)
然后我有多个文件从我的主程序中调用。为了让它们正常工作,我需要执行以下操作:
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.debug("This debug message is ounly output when I set the level again to debug for this file. Otherwise the the log level for this file is WARNING.")
为什么我导入的所有模块的默认级别都设置为警告。当他们使用 log = logging.getLogger(name) 导入我的根记录器时,为什么我必须再次将级别设置为 DEBUG?这是跨具有不同模块的包创建日志记录模块的最佳方法还是有更好的解决方案?
最佳答案
让我们先看看 Handler.setLevel does 是什么:
Sets the threshold for this handler to lvl. Logging messages which are less severe than lvl will be ignored. When a handler is created, the level is set to NOTSET (which causes all messages to be processed).
任何低于 lvl 严重程度的消息都将被忽略。实际上,将其设置为 DEBUG 是没有意义的(除非您定义自己的日志级别),因为没有比 debug 更严重的消息。所以,这意味着处理程序不会忽略任何消息。
setLevel
是正确的想法,但您在错误的对象上调用它。看Logger.setLevel :
Sets the threshold for this logger to lvl. Logging messages which are less severe than lvl will be ignored. When a logger is created, the level is set to NOTSET (which causes all messages to be processed when the logger is the root logger, or delegation to the parent when the logger is a non-root logger). Note that the root logger is created with level WARNING.
The term ‘delegation to the parent’ means that if a logger has a level of NOTSET, its chain of ancestor loggers is traversed until either an ancestor with a level other than NOTSET is found, or the root is reached.
If an ancestor is found with a level other than NOTSET, then that ancestor’s level is treated as the effective level of the logger where the ancestor search began, and is used to determine how a logging event is handled.
您正在正确地创建子项,但它们都是根记录器的子项。它们的级别设置为 NOTSET
,并且向上传播到根,其默认值为 WARNING
。因此,您看不到任何消息。
长话短说:解决方案很简单:在记录器上设置级别,而不是处理程序。以下代码应该可以满足您的需要:
def start_logger():
fh = logging.handlers.RotatingFileHandler('logger.log',
maxBytes=1000000,
backupCount=100)
ch = logging.StreamHandler(sys.stdout)
fh_fmt = '%(asctime)s %(levelname)8s %(message)s [%(filename)s:%(lineno)d]'
#fh_fmt = '%(asctime)s - %(funcName)s - %(levelname)s - %(message)s'
ch_fmt = '%(asctime)s %(levelname)8s %(message)s [%(filename)s:%(lineno)d]'
#ch_fmt = '%(funcName)s - %(levelname)s - %(message)s'
fh.setFormatter(logging.Formatter(fh_fmt))
ch.setFormatter(logging.Formatter(ch_fmt))
logging.basicConfig(level=logging.DEBUG)
root = logging.getLogger()
root.addHandler(fh)
root.addHandler(ch)
一旦你这样做了,你就不需要在创建 child 时调用 setLevel
。
哦,回答你的其他问题:这正是你应该使用日志库的方式。 (实际上,我只是将所有内容记录到根记录器中,因为我不需要您开发的那种粒度,但是当您有理由这样做时,您会按照您应该做的去做。)
编辑:显然,setLevel 似乎不适用于根记录器。相反,在访问根记录器之前,您必须设置 basicConfig。在 logging.basicConfig
中设置级别将满足您的需要(至少,它在我的测试中有效)。请注意,这样做符合 Logging from multiple modules 中给出的示例,所以应该可以解决您的问题。
关于从 root 继承的 python 记录器级别默认设置为警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783527/
我在我的实验室中通过终端使用 ROOT,我们经常收集数据并且每小时都有一个文件夹,其中包含微小的 .root 文件。 我正在尝试创建一个每日 .root 文件,其中包含某一天获取的所有数据,目前我有许
我正在创建一些 Docker 镜像,并且正在阅读其他人是如何做到这一点的。当涉及到在容器内运行进程的用户时,我已经确定了三种一般模式: 它使用 root 用户 处理一切(在 root 下运行的容器内生
使用此命令设置密码后: mysqladmin.exe --user=root password 我在“根”用户的用户表中看到 3 行: http://i.stack.imgur.com/Y4Rkd.
我有这个秒表,我试图在计数达到 0 后重置它。但是我在使用“after_cancel”功能时遇到了问题。谁能帮忙? 发生的是它无休止地继续,我想杀死“之后”功能。 def countdown(self
if(file_exists( $_SERVER['DOCUMENT_ROOT'] . "/index.html")) echo '文件存在'; 当文件的所有者和组设置为 root:root 时,仅返
我尝试在 AWS 和 Google Drive 上的 Redhat 7 上安装 MySQL,但是在安装 mysql-server 5.7 后,我收到了相同的错误。 [root@localhost ~]
我试图仅将根域(而不是其子文件夹)重定向到另一个 URL,而不更改地址。我正在使用 .htaccess 并使用 [P] 标志进行重定向,这适用于子目录但不适用于根目录。 编写以下 .htaccess
我正在使用 Apache CGI mod 来允许通过 HTTP(S) 请求执行 python 脚本。问题是我想要执行的脚本 backup.py 在某个时刻执行一个子进程调用,其中 mysqldump
我有一个 script.sh 文件执行以下命令: chown -R apache:apache /var/www/html/my/data 如果我尝试使用非 root 用户(用户名 = marco)执
我目前正在开发一款可以在开车时读出短信/电子邮件的应用程序。许多用户希望支持 WhatsApp/KakaoTalk。 但是,由于没有“官方”方式来接收他们的消息,因此只有三个选项,都需要 root:
我已经看到这个问题问了几次,但从未得到完全回答(我能找到)。 需要说明的是,我的手机已 root。 我需要做的是在 root 拥有的目录下读取和写入文件,例如 /data。我知道我需要使用 super
我有一个 Google Compute Engine VM 实例,上面运行着 Asterisk Server。当我尝试运行 sudo 时收到此消息: sudo: parse error in /etc
我正在使用sail 并尝试以root 身份使用shell,这是不可能的。 切换到 root 用户要求输入密码,我不知道。 我尝试更改 dockerfile: ARG WWWROOTGROUP RUN
rvm 的全部意义在于能够安装多个 ruby 并从用户帐户访问它们。当您需要从 root 访问任何这些 ruby 时,您会怎么做?或者使用安装在其中一个 rvms 中的 gems? 有没有一种优雅的(
我正在尝试在 Openshift 中运行 SonarQube pod,但它似乎正在以 root 作为所有者安装持久卷。 我们如何将其更改为非 root 用户? 我用“hostPath”创建了我的持久卷
我是 Ansible 的新手,我已经创建了我的第一个 Ansible 角色剧本,当我尝试运行它时,它抛出了下面的错误,而角色之外的其他模块(如处理程序、模板)工作正常。我仅通过剧本中的角色观察到这个问
对“以非 root 用户和 root 用户身份运行 docker”感到困惑。 第一个问题(以非root用户运行):基于Post-installation steps for Linux ,要以非 ro
在我的项目中,我发现需要以分层方式打破我的聚合,使用顶级根级别聚合,以确保根级别的规则一致性,然后我的根下的对象可以分组为各种聚合。在计算根级聚合的完整性时,根验证自己的规则,然后委托(delegat
我不知道我是怎么做到的,但在过去几天的某个时候,我设法将我的 MAMP Pro 用户名和密码更改为 root:root 以外的其他名称。我一直在本地开发 Wordpress 版本,所以它一定是我当时做
update user set host='%' where user='root 之后,我失去了我的 MySQL root 用户的一些权限。所以我停止了服务器并用 --skip-grant-tabl
我是一名优秀的程序员,十分优秀!