python基础知识

    科技2025-03-16  23

    python基础知识

    Logging is the act of recording information about the current state of execution. It’s typically done for two purposes:

    记录是记录有关当前执行状态的信息的动作。 通常有两个目的:

    Debugging: To enable the developer to narrow down the cause of a system failure

    调试:使开发人员能够缩小系统故障的原因

    Monitoring: To enable people on operations to know what is currently happening

    监视:使操作人员可以了解当前发生的情况

    您好,甜伐世界!(Hello, Sweet logging World!)

    The simplest way to log is like this:

    最简单的登录方式是这样的:

    import loggingimport syslogging.basicConfig( format="{asctime} {levelname:<8} {message}", style="{", level=logging.DEBUG, stream=sys.stdout,)logging.info("Hello World!")

    It gives output like this:

    它给出如下输出:

    2020-09-07 22:40:32,101 INFO Hello World!

    BasicConfig should probably not be used. Please continue reading “the 4 logging classes” to learn why 😁

    基本不应该使用BasicConfig 。 请继续阅读“ 4种测井课程”以了解why

    geek-and-poke.com geek-and-poke.com的Oliver Widder

    4种日志记录类 (The 4 Logging Classes)

    Python has a logger hierarchy in a tree structure. This means you can apply a log configuration for a single logger and make all child loggers behave the same way. This is useful if you want to configure a package. logging.basicConfig acts on the root logger. For this reason, it should only be used within application code, but not in library or framework code.

    Python具有树状结构的记录器层次结构。 这意味着您可以为单个记录器应用日志配置,并使所有子记录器的行为相同。 如果要配置程序包,这很有用。 logging.basicConfig作用于根记录器。 因此,它只能在应用程序代码中使用,而不能在库或框架代码中使用。

    Python distinguishes 4 main components that you can adjust to your needs: Loggers, handlers, filters, and formatters. They act on log records that are created when you pass your log message to the logger.

    Python区分了可以根据需要进行调整的4个主要组件:记录器,处理程序,过滤器和格式化程序。 它们作用于将日志消息传递给记录器时创建的日志记录。

    记录仪 (Loggers)

    A logger is an object you use to emit log records:

    记录器是用于发出日志记录的对象:

    import logginglogger = logging.getLogger(__name__)

    You can emit 5 levels of log messages :

    您可以发出5级日志消息:

    Debug: extremely detailed, you can use this if you have no clue at a certain part of the code what is happening. Remember to remove it again.

    调试:非常详细,如果您对代码的某些部分一无所知,则可以使用它。 请记住再次将其删除。

    Info: nothing to worry about, but helpful to understand the system

    信息:无需担心,但有助于理解系统

    Warning: something that could become a problem that happened. Maybe an event that might indicate an error if it happens too often. Maybe the remaining storage becomes low. Maybe the internet connection was broken. Maybe a file was not writable.

    警告:可能会成为问题的事物。 如果发生的次数过多,也许是一个可能指示错误的事件。 也许剩余的存储空间变少了。 也许互联网连接中断了。 也许文件不可写。

    Error: an important event could not be executed, e.g. because of missing privileges or a file that should be read did not exist.

    错误:一个重要事件无法执行,例如由于缺少特权或应读取的文件不存在。

    Critical: a problem that requires the app to restart happened. For example, a kill signal was received.

    严重:发生了需要重新启动应用程序的问题。 例如,接收到一个终止信号。

    You use it like this :

    您可以这样使用它:

    logger.info("My first log message")logger.warning("The specified file could not be opened.")

    日志处理程序 (Log Handlers)

    File handlers store stuff to files, stream handlers write logs to a stream:

    文件处理程序将内容存储到文件中,流处理程序将日志写入流中:

    sh = logging.StreamHandler()fh = logging.FileHandler("spam.log")logger.addHandler(sh)

    If you’re using a file handler, consider using a RotatingFileHandler. It will create a new file once the log file becomes too big. You can specify how many files there might be. When the maximum is reached, the oldest file is deleted.

    如果使用文件处理程序,请考虑使用RotatingFileHandler 。 一旦日志文件太大,它将创建一个新文件。 您可以指定可能有多少个文件。 达到最大值后,将删除最早的文件。

    The HTTPHandler is also noteworthy because it allows you to integrate into other systems such as Slack.

    HTTPHandler也值得注意,因为它允许您集成到其他系统中,例如Slack。

    Commonly, you also want to set the log level to either the logger or the log handler:

    通常,您还希望将日志级别设置为记录器或日志处理程序:

    sh.setLevel(logging.INFO) eek-and-poke.com geek-and-poke.com

    日志格式化程序 (Log Formatters)

    The log formatter changes the log message string. A formatter is attached to a log handler:

    日志格式化程序更改日志消息字符串。 格式化程序附加到日志处理程序:

    formatter = logging.Formatter( "{asctime} {levelname:<8} {message}", style="{")sh.setFormatter(formatter)

    The style attribute is interesting. The default is % which means the format string then needs to be (asctime)s (levelname)-8s (message)s . I never really learned how the percentage style formatting works. I like to stick to the curly braces.

    样式属性很有趣。 默认值为% ,这意味着格式字符串需要为(asctime)s (levelname)-8s (message)s 。 我从未真正了解百分比样式格式如何工作。 我喜欢坚持大括号。

    There are way more log record attributes which you can use.

    您可以使用更多日志记录属性。

    日志过滤器 (Log Filters)

    Log filters provide the possibility to define which log records get shown:

    日志过滤器提供了定义显示哪些日志记录的可能性:

    import datetimeimport loggingimport syslogger = logging.getLogger(__name__)class OnWeekendOnlyErrorsFilter(logging.Filter): def filter(self, record): is_weekday = datetime.datetime.today().weekday() < 5 return is_weekday or record.levelno >= logging.ERRORstdout_handler = logging.StreamHandler(sys.stdout)stdout_handler.setLevel(logging.WARNING)stdout_handler.addFilter(OnWeekendOnlyErrorsFilter())logger.addHandler(stdout_handler) Photo by Nigel Tadyanehondo on Unsplash Nigel Tadyanehondo在 Unsplash上 拍摄的照片

    记录vs打印vs异常(Logging vs print vs exception)

    I’ve been very confused about when I should simply print out information and when I should log out or when I should throw an exception.

    对于何时仅打印信息,何时注销或何时引发异常,我一直感到困惑。

    You throw an exception in a library function so that the user of that function can catch the exception and show the end-user a meaningful error message. The end-user should never see a traceback.

    您在库函数中引发异常,以便该函数的用户可以捕获该异常并向最终用户显示有意义的错误消息。 最终用户永远都不会看到追溯。

    Logging is meant for other systems or developers who try to understand what happened to a system, whereas print is for the user. The confusing part is that print messages go by default to the standard error. You can easily do the same with print. I’ve used it in the past to give the user feedback of what is currently happening, simply because logging had an easy way to include time stamps.

    日志记录适用于试图了解系统故障的其他系统或开发人员,而打印记录适用于用户。 令人困惑的是,默认情况下,打印消息会出现标准错误。 您可以轻松进行打印。 过去,我一直使用它来向用户提供当前正在发生的事情的反馈,这仅仅是因为日志记录可以轻松地包含时间戳。

    Photo by Esteban Lopez on Unsplash Esteban Lopez在 Unsplash上的 照片

    警告警告与日志记录警告(Warnings.warn vs logging.warning)

    According to the official docs, you have two options when you want to issue a warning regarding a particular runtime event:

    根据官方文档,当您要发出有关特定运行时事件的警告时,有两种选择:

    warnings.warn() in library code, if the issue is avoidable and the client application should be modified to eliminate the warning

    库代码中的warnings.warn()如果可以避免该问题,并且应修改客户端应用程序以消除警告)

    logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted

    logging.warning()如果客户端应用程序无法处理这种情况,但仍应注意该事件

    A typical use-case for warnings is DeprecationWarning with which a library can tell its users to remove a certain type of usage. Or Scipy warning you that no BLAS library was found.

    警告的一个典型用例是DeprecationWarning ,库可以通过它通知其用户删除某种类型的用法。 或者Scipy警告您未找到BLAS库。

    Too few log messages are bad, but too many can be problematic as well. Photo by Christa Dodoo on Unsplash 日志消息太少是不好的,但是太多也可能是有问题的。 Christa Dodoo在 Unsplash上的 照片

    我应该记录什么?(What should I log?)

    I typically log code that takes long to execute pretty well, whereas I don’t add much logging to functions which are called super often and are fast. Most of the time there are initialization functions which load configuration. I always log the complete configuration but strip away credentials.

    我通常会记录需要很长时间才能很好地执行的代码,而我并没有在所谓的超级函数和快速函数中添加太多日志。 大多数时候,都有初始化功能会加载配置。 我总是记录完整的配置,但会删除凭据。

    I also log errors and rare exceptions.

    我还记录错误和罕见的异常。

    It’s hard to find the right balance. Too many log messages make it hard to find relevant information. To few messages might mean that you didn’t log the important information at all.

    很难找到合适的平衡。 日志消息太多,很难找到相关信息。 很少的消息可能意味着您根本没有记录重要信息。

    最佳实践 (Best practices)

    It’s a common practice for applications to create a log.py or a logger.py file in which the logger is initiated, log handler, and formatters are added. OpenShot is doing it.

    应用程序通常会创建一个log.py或logger.py文件,在其中启动记录器,添加日志处理程序和格式化程序。 OpenShot正在这样做。

    Photo by Kristina Flour on Unsplash Kristina Flour在 Unsplash上 拍摄的照片

    静音记录器(Silencing loggers)

    A common cause of frustration is log messages from 3rd party software which spam your application. If they behave well, it is easy to silence them: Get the logger and set the level to something high:

    造成沮丧的一个常见原因是来自第三方软件的日志消息,这些消息会向您的应用程序发送垃圾邮件。 如果它们表现良好,很容易使它们静音:获取记录器并将其级别设置为较高:

    logging.getLogger("urllib3").setLevel(logging.CRITICAL)

    If you don’t even want critical log records, you can set

    如果您甚至不需要重要的日志记录,则可以设置

    logging.getLogger("urllib3").setLevel(logging.CRITICAL + 1)

    To disable the child loggers as well:

    也要禁用子记录器:

    # Disable all child loggers of urllib3, e.g. urllib3.connectionpoollogging.getLogger("urllib3").propagate = False

    You can also remove all handlers / add the NullHandler.

    您还可以删除所有处理程序/添加NullHandler 。

    我应该一直使用Python的日志记录库吗? (Should I always use Pythons’ logging library?)

    For sure not! Some scripts are so short that logging isn’t reasonable. Others, like ArchiveBox, implement their own specialized logging. And there are other logging libraries like structlog.

    当然不能! 有些脚本太短,以至于记录不合理。 其他如ArchiveBox则实现自己的专用日志记录。 还有其他日志记录库,例如structlog 。

    更多信息 (More information)

    Similar information is available in an amazing PyCon talk by Curtis Maloney:

    类似的信息可以在Curtis Maloney的精彩的PyCon演讲中找到:

    演示地址

    You can directly read the documentation or the official logging howto. To me, reading StackOverflow is also often helpful.

    您可以直接阅读文档或官方记录方法。 对我来说,阅读StackOverflow通常也很有帮助。

    With those resources, I hope nobody has to struggle with Python logging again. If you still have questions, let me know (info@martin-thoma.de).

    有了这些资源,我希望没有人需要再为Python日志而苦恼。 如果您仍有疑问,请告诉我(info@martin-thoma.de)。

    翻译自: https://towardsdatascience.com/logging-basics-in-python-d0db13e538f9

    python基础知识

    Processed: 0.009, SQL: 8