关闭计划表中的日志记录

关闭计划表中的日志记录

本文介绍了关闭计划表中的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:防止时间表每次运行时记录日志.

Objective: prevent schedule from logging every time it runs.

背景:

我正在python项目中使用 logging schedule 库.

I am using the logging and schedule libraries in a python project.

我的日志文件包含有关Raspberry Pi运行的仪器的物理状态的信息,并且每10秒更新一次.

My log file contains information about the physical state of a instrument run by a Raspberry Pi, and is updated every 10 seconds.

我使用 schedule 库来安排定期日志.

I use the schedule library to schedule that periodic log.

此处是我发现的关于 schedule 的有限文档.

Here is the limited documentation I have found for schedule.

问题:

时间表库在每次运行作业时记录该语句.

The schedule library logs this statement, every time it runs a job.

2016-06-29 09:01:51,022 INFO: Running job every 10 seconds do update_log() (Last run...

计划时间表调用的函数是 update_log(),该函数计算每十秒钟运行一次的日志中包含的变量并将其记录下来(下面的示例).

The function that schedule calls is update_log(), a function that calculates the variables included in the log I run every ten seconds and logs them (example below).

2016-06-29 09:01:51,022 INFO: Dist: 12.3 m Rate: 23.8 cm/s

因为 schedule 正在生成它自己的(相当无用的)日志行,这使得我实际上尝试做的日志很难阅读.

Because schedule is producing its own (fairly useless) log line, it makes the logging I am actually trying to do very difficult to read.

目标:

防止记录第一条语句的时间表.

Prevent schedule from logging that first statement.

推荐答案

schedule 模块是专门使用称为 schedule 的记录器.您可以使用 logging 禁用将此记录器写入主记录器.

import logging
logging.getLogger('schedule').propagate = False

如果您根本不想使用 schedule 的日志,也可以通过将其日志级别设置为高于任何实际日志级别来禁用它.

If you don't want schedule's logs at all, you can also disable it by settings its log level above any real log level.

import logging
logging.getLogger('schedule').setLevel(logging.CRITICAL + 10)

如果您只希望某些消息通过,请将级别设置为常规的 logging 级别.

If you just want some messages to get through, set the level to a regular logging level.

从python2.7开始,您还可以使用 NullHandler .

Since python2.7, you can also use a NullHandler instead.

import logging
logging.getLogger('schedule').propagate = False
logging.getLogger('schedule').addHandler(logging.NullHandler())

这篇关于关闭计划表中的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:56