python登录界面_登录Python

python登录界面

Logging is a very useful tool in a programmer’s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing.

日志记录是程序员工具箱中非常有用的工具。 它可以帮助您更好地理解程序的流程,并发现您在开发过程中甚至可能没有想到的方案。

Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through. They can store information, like which user or IP accessed the application. If an error occurs, then they can provide more insights than a stack trace by telling you what the state of the program was before it arrived at the line of code where the error occurred.

日志为开发人员提供了额外的支持,他们可以不断查看应用程序正在经历的流程。 他们可以存储信息,例如哪个用户或IP访问了该应用程序。 如果发生错误,则通过告诉您程序到达错误发生的代码行之前的状态,它们可以提供比堆栈跟踪更多的见解。

By logging useful data from the right places, you can not only debug errors easily but also use the data to analyze the performance of the application to plan for scaling or look at usage patterns to plan for marketing.

通过从正确的位置记录有用的数据,您不仅可以轻松调试错误,还可以使用数据来分析应用程序的性能以计划扩展或查看使用模式以计划营销。

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application. In this article, you will learn why using this module is the best way to add logging to your application as well as how to get started quickly, and you will get an introduction to some of the advanced features available.

Python提供了一个日志记录系统作为其标准库的一部分,因此您可以快速将日志记录添加到应用程序中。 在本文中,您将了解为什么使用此模块是向应用程序添加日志记录的最佳方法,以及如何快速入门,并且将向您介绍一些可用的高级功能。

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

免费奖金: 关于Python精通的5个想法 ,这是针对Python开发人员的免费课程,向您展示了将Python技能提升到新水平所需的路线图和心态。

记录模块 (The Logging Module)

The logging module in Python is a ready-to-use and powerful module that is designed to meet the needs of beginners as well as enterprise teams. It is used by most of the third-party Python libraries, so you can integrate your log messages with the ones from those libraries to produce a homogeneous log for your application.

Python中的日志记录模块是一个易于使用且功能强大的模块,旨在满足初学者和企业团队的需求。 大多数第三方Python库都使用它,因此您可以将日志消息与这些库中的日志消息集成在一起,以为您的应用程序生成同类日志。

Adding logging to your Python program is as easy as this:

将日志添加到Python程序非常简单:

 import import logging
logging

With the logging module imported, you can use something called a “logger” to log messages that you want to see. By default, there are 5 standard levels indicating the severity of events. Each has a corresponding method that can be used to log events at that level of severity. The defined levels, in order of increasing severity, are the following:

导入日志记录模块后,您可以使用一种称为“记录器”的东西来记录您要查看的消息。 默认情况下,有5个标准级别指示事件的严重性。 每个方法都有对应的方法,可用于记录该严重性级别的事件。 定义的级别按严重程度从高到低依次为:

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL
  • 调试
  • 信息
  • 警告
  • 错误
  • 危急

The logging module provides you with a default logger that allows you to get started without needing to do much configuration. The corresponding methods for each level can be called as shown in the following example:

日志记录模块为您提供了一个默认的记录器,使您无需进行大量配置即可开始使用。 可以调用每个级别的相应方法,如以下示例所示:

The output of the above program would look like this:

上面程序的输出如下所示:

 WARNING:root:This is a warning message
WARNING:root:This is a warning message
ERROR:root:This is an error message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message
CRITICAL:root:This is a critical message

The output shows the severity level before each message along with root, which is the name the logging module gives to its default logger. (Loggers are discussed in detail in later sections.) This format, which shows the level, name, and message separated by a colon (:), is the default output format that can be configured to include things like timestamp, line number, and other details.

输出显示每条消息之前的严重性级别以及root ,这是日志记录模块赋予其默认记录器的名称。 (记录器是在后面的部分详细地讨论。)这种格式,其示出了电平,名称,和消息由冒号分隔( : ),是一种可以被配置为包括诸如时间戳,行号默认的输出格式,并且其他详情。

Notice that the debug() and info() messages didn’t get logged. This is because, by default, the logging module logs the messages with a severity level of WARNING or above. You can change that by configuring the logging module to log events of all levels if you want. You can also define your own severity levels by changing configurations, but it is generally not recommended as it can cause confusion with logs of some third-party libraries that you might be using.

请注意, debug()info()消息未记录。 这是因为,默认情况下,日志记录模块记录严重性级别为WARNING或更高的消息。 您可以通过配置日志记录模块以记录所有级别的事件(如果需要)来进行更改。 您也可以通过更改配置来定义自己的严重性级别,但是通常不建议这样做,因为它可能会与您可能正在使用的某些第三方库的日志混淆。

基本配置 (Basic Configurations)

You can use the basicConfig(**kwargs) method to configure the logging:

您可以使用basicConfig(** kwargs )方法来配置日志记录:

“You will notice that the logging module breaks PEP8 styleguide and uses camelCase naming conventions. This is because it was adopted from Log4j, a logging utility in Java. It is a known issue in the package but by the time it was decided to add it to the standard library, it had already been adopted by users and changing it to meet PEP8 requirements would cause backwards compatibility issues.” (Source)

“您会注意到,日志记录模块违反了PEP8样式指南,并使用camelCase命名约定。 这是因为它是从Java的日志实用程序Log4j中采用的。 这是程序包中的一个已知问题,但是当决定将其添加到标准库中时,它已经为用户所采用,并且对其进行更改以满足PEP8的要求将导致向后兼容性问题。” (资源)

Some of the commonly used parameters for basicConfig() are the following:

basicConfig()一些常用参数如下:

  • level: The root logger will be set to the specified severity level.
  • filename: This specifies the file.
  • filemode: If filename is given, the file is opened in this mode. The default is a, which means append.
  • format: This is the format of the log message.
  • level :根记录器将设置为指定的严重性级别。
  • filename :这指定文件。
  • filemode :如果指定了filenamefilemode该模式打开文件。 默认值是a ,这意味着附加。
  • format :这是日志消息的格式。

By using the level parameter, you can set what level of log messages you want to record. This can be done by passing one of the constants available in the class, and this would enable all logging calls at or above that level to be logged. Here’s an example:

通过使用level参数,您可以设置要记录的日志消息的级别。 这可以通过传递类中可用的常量之一来完成,这将使该级别或更高级别的所有日志记录调用都可以被记录。 这是一个例子:

 DEBUG:root:This will get logged
DEBUG:root:This will get logged

All events at or above DEBUG level will now get logged.

现在将记录所有DEBUG或更高级别的事件。

Similarly, for logging to a file rather than the console, filename and filemode can be used, and you can decide the format of the message using format. The following example shows the usage of all three:

同样,要登录到文件而不是控制台,可以使用filenamefilemode ,并且可以使用format决定消息的format 。 以下示例显示了这三个方法的用法:

 root - ERROR - This will get logged to a file
root - ERROR - This will get logged to a file

The message will look like this but will be written to a file named app.log instead of the console. The filemode is set to w, which means the log file is opened in “write mode” each time basicConfig() is called, and each run of the program will rewrite the file. The default configuration for filemode is a, which is append.

该消息看起来像这样,但是将被写入名为app.log的文件而不是控制台。 filemode设置为w ,这意味着每次调用basicConfig()basicConfig()以“写入模式”打开日志文件,并且每次运行该程序都会重写该文件。 对于FILEMODE默认配置为a ,这是附加。

You can customize the root logger even further by using more parameters for basicConfig(), which can be found here.

您可以通过为basicConfig()使用更多参数来进一步自定义根记录程序,可以在此处找到。

It should be noted that calling basicConfig() to configure the root logger works only if the root logger has not been configured before. Basically, this func

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值