日志记录器去记录所有日志_为什么要关心日志记录?

日志记录器去记录所有日志

As a beginner, it is often observed that we tend to focus more on writing code that gets the job done and less on how it is being done. Very little thought is given to the design of the overall application and best practices that should be followed. While that is okay for a beginner, as we move forward in our programming journey, we need to consider other aspects of the application as well, before we can move the code to production. Proper logging is one of those aspects that differentiates intermediate or advanced developers from a newbie. In this article, we will learn the basics of ‘Loggingmodule of Python. It is important to note that although we are showing Python examples, the overall concept is not limited to just Python and can be applied to other languages as well.

作为一个初学者,经常观察到我们倾向于将重点更多地放在编写完成任务的代码上,而不是在如何完成任务上。 很少考虑整体应用程序的设计和应遵循的最佳实践。 虽然这对于初学者来说是可以的,但是随着我们在编程过程中的前进,我们还需要考虑应用程序的其他方面,然后才能将代码投入生产。 适当的日志记录是区分中级或高级开发人员与新手的方面之一。 在本文中,我们将学习Python的' L ogging '模块的基础知识。 重要的是要注意,尽管我们展示的是Python示例,但整个概念不仅限于Python,也可以应用于其他语言。

测井及其重要性 (Logging and its importance)

As noobs, we always use the mighty print() function to print things out in the console, perhaps for the purpose of debugging or displaying intermediate variables and output values. That’s all good during the coding phase, but when your application is Live in a Production environment, they need proper tracing & monitoring. Logging is the process of providing information about an application as it performs different tasks or events. Logging offers benefits such as:

作为菜鸟,我们总是使用强大的print()函数在控制台中打印出内容,也许是为了调试或显示中间变量和输出值。 在编码阶段这一切都很好,但是当您的应用程序在生产环境中运行时,他们需要适当的跟踪和监视。 日志记录是在执行不同任务或事件时提供有关应用程序信息的过程。 日志记录具有以下优点:

Issue Diagnosis: Let’s say a bug was reported by a user and you want to replicate that scenario in your development environment. Now if you had logged the user activity in a log file, it might be easier for you to refer those logs and replicate it in a non production environment for further analysis.

问题诊断:假设用户报告了一个错误,您想在开发环境中复制该方案。 现在,如果您已将用户活动记录在日志文件中,则可以更轻松地引用这些日志并将其复制到非生产环境中以进行进一步分析。

Image for post
Xkcd Xkcd

Analytics: By analyzing a website logs, one could get more information on the rush hours for the site, or what region the traffic is coming from, or how much time a user spends on a particular page etc. By recording more data in the log files, you can further analyze user behavior that could potentially improve the users’ overall experience.

分析:通过分析网站日志,可以获取有关网站繁忙时间,流量来自哪个区域,用户在特定页面上花费了多少时间等信息。通过在日志中记录更多数据文件,您可以进一步分析可能会改善用户整体体验的用户行为。

Image for post
Xkcd Xkcd

我应该在日志中包括什么? (What should I include in my logs?)

The information to be logged is subjective and depends on the context of the particular application. However, it is advisable to include at least the following information:

要记录的信息是主观的,并且取决于特定应用程序的上下文。 但是,建议至少包含以下信息:

When: The timestamp of the logged eventWho: The application name or user nameWhere: The context of the event (login module, file upload module etc.) What: The type of activity or error category (Database error, File I/O etc.)

:应用程序或用户名: 其中 :记录的事件的时间戳事件的上下文(登录模块,文件上传模块等) 什么 :类型的活动或错误类别(数据库错误,文件I / O的等等。)

Do keep in mind not to include any sensitive information in your logs.

请记住不要在日志中包含任何敏感信息。

记录级别 (Logging Levels)

Python offers a built in module for this purpose that we will explore below. Before getting into the code, let’s take a look at the various levels of logging this package offers. The 'logging levels' basically talks about the type of information to be included in our logs. There are 5 such levels:

Python为此提供了一个内置模块,我们将在下面进行探讨。 在研究代码之前,让我们看一下该程序包提供的各种日志记录级别。 “ 日志记录级别 ”基本上是讨论要包含在日志中的信息类型。 有5个这样的级别:

Image for post
Docs 文件

Based on our specific needs, we could choose one or more of these logging levels. They have a numeric value associated with each one of them. Typically we select one of these levels and all the other levels greater than or equal to the selected logging level are captured in our log files. Let’s see this with the help of an example.

根据我们的特定需求,我们可以选择一个或多个这些日志记录级别。 它们具有与它们每个相关的数值。 通常,我们选择这些级别之一,所有其他级别大于或等于所选日志记录级别的其他级别都会捕获到我们的日志文件中。 让我们借助示例来了解这一点。

Image for post
Docs 文件

In the code snippet below, between lines 1–7 we are importing the python logging module, creating a logger object with the name ‘MY_FIRST_LOGGER’ and setting the logging level to ‘WARNING’. This tells the program not to capture the logs that are below ‘WARNING’ level (i.e. DEBUG & INFO).

在下面的代码段中,在第1至7行之间,我们将导入python 日志记录模块,创建一个名称为' MY_FIRST_LOGGER '的记录器对象,并将记录级别设置为' WARNING '。 这告诉程序不要捕获低于“警告”级别的日志(即调试和信息)。

From line number 9–13, we are simply specifying a file handler and attaching it to our logger object.

从第9-13行开始,我们只需指定一个文件处理程序并将其附加到我们的logger对象。

In lines 15–19, we are sending messages to all the different logging levels listed above.

在第15-19行中,我们正在将消息发送到上面列出的所有不同的日志记录级别。

Let us see the contents of our log file (my_log1.log) after running this code. As shown below, it only has the records printed for level ‘WARNING’ & above. If we had set the logging level to ‘DEBUG’, all the lines would have been present in the log file.

运行此代码后,让我们查看日志文件(my_log1.log)的内容。 如下所示,它只打印了“警告”及以上级别的记录。 如果我们将日志记录级别设置为“ DEBUG”,则所有行都将出现在日志文件中。

Image for post
Contents of my_log1.log file (Source: Author)
my_log1.log文件的内容(来源:作者)

The current level of logger can be checked using the parameter logger.level.

可以使用参数logger.level检查当前的记录器级别。

print(logger.level)
30

Next, let us add this logger function to a program which performs division of two integers and returns a remainder. We will also add more information to our log record like timestamp, context etc. The log records used here are just for the purpose of illustration and maybe not necessarily be that relevant. It is a common practice to add these logging commands in your exception catch block to make your logs more comprehensive.

接下来,让我们将此logger函数添加到一个程序中,该程序执行两个整数的除法并返回余数 。 我们还将在日志记录中添加更多信息,例如时间戳记,上下文等。此处使用的日志记录仅出于说明目的,可能未必与此相关。 通常的做法是在异常捕获块中添加这些日志记录命令,以使日志更加全面。

The function ‘division’ is called 3 times with different parameters. Let us check what the log file looks like after execution.

具有不同参数的函数“除法”被调用3次。 让我们检查执行后日志文件的外观。

Image for post
Contents of my_log2.log file (Source: Author)
my_log2.log文件的内容(来源:作者)

As can be seen above, the 3rd call to the function results in an ZeroDivisionError error due to the divisor being zero. Looking at the logs, we can tell that the program failed to execute fully (because the ‘Remainder calculated.’ is not printed due to failure). However, since we logged the input parameters in the log file, we get a fair starting point on what might have caused the issue and pick it up from there. Clearly this is an oversimplification, but for a more complicated application with tens of methods, this is something that we must consider.

如上所示,由于除数为零,因此对该函数的第三次调用会导致ZeroDivisionError错误。 查看日志,我们可以知道该程序无法完全执行(因为由于失败而未打印“计算得出的余数”)。 但是,由于我们将输入参数记录在日志文件中,因此我们可以很清楚地了解导致问题的原因并从那里开始。 显然,这是一个过分的简化,但是对于具有数十种方法的更复杂的应用程序,这是我们必须考虑的事情。

Now, we can choose one or more of the above levels depending on our specific needs and based on the selected logging level they will be displayed in the log files. Usually in production, it is set to info and above.

现在,我们可以根据我们的特定需求并根据所选的日志记录级别选择以上级别中的一个或多个,这些级别将显示在日志文件中。 通常在生产中,它设置为info及以上。

In conclusion, we explored the motivation behind using logging modules in our development process. We also ran through the different levels of logging and how to implement them in our Python code.

总之 ,我们探讨背后我们的开发过程中使用日志模块的动机。 我们还介绍了不同级别的日志记录以及如何在我们的Python代码中实现它们。

Thank you very much for reading and I hope this article leaves you with a basic understanding and knowledge to include loggers in your next project.

非常感谢您的阅读,希望本文对您有所帮助,使您对下一个项目中的记录器有所了解。

You might also like:

您可能还喜欢:

参考资料和进一步阅读: (References and Further Reading:)

翻译自: https://towardsdatascience.com/why-should-you-care-about-logging-442a195b80a1

日志记录器去记录所有日志

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值