python练习_如何使用Logzero在Python中练习记录

python练习

Logzero is a Python package created by Chris Hager that simplifies logging with Python 2 and 3. Logzero makes it easier as a print statement to show information and debugging details.

Logzero是Chris Hager创建的Python程序包,它简化了Python 2和3的日志记录。Logzero使它更容易用作显示信息和调试详细信息的打印语句。

If you are wondering what logging is, I recommend that you read the previous article I wrote about “How to Run Machine Learning Experiments with Python Logging Module”, especially the first 3 sections.

如果您想知道什么是日志记录 ,我建议您阅读上一篇有关“如何使用Python日志记录模块运行机器学习实验”的文章 ,特别是前三部分。

In that article, you will learn:

在该文章中,您将学习:

  • What is Logging?

    什么是日志记录?
  • Why logging is important.

    为什么记录很重要。
  • Applications of logging in different technology industries.

    伐木在不同技术行业中的应用。

Logzero has different features that make it easier to use in Python projects. Some of these features are:

Logzero具有不同的功能,可以更轻松地在Python项目中使用。 其中一些功能是:

  • Easy logging to console and/or file.

    轻松记录到控制台和/或文件。
  • Provides a fully configured standard Python logger object.

    提供完整配置的标准Python记录器对象。
  • Pretty formatting, including level-specific colors in the console.

    漂亮的格式,包括控制台中特定于级别的颜色

  • works with all kinds of character encodings and special characters.

    适用于各种字符编码和特殊字符。
  • Compatible with Python 2 and 3.

    与Python 2和3兼容。
  • No further Python dependencies.

    没有更多的Python依赖项。

安装 (Installation)

To install logzero with pip run the following:

要使用pip安装logzero,请运行以下命令:

pip install -U logzero

You can also install logzero from the public Github repo:

您还可以从公共Github存储库安装logzero:

git clone https://github.com/metachris/logzero.git
cd logzero
python setup.py install

基本范例 (Basic Example)

We will start with a basic example. In the python file, we will import the logger from logzero and try 4 different logging level examples.

我们将从一个基本示例开始。 在python文件中,我们将从logzero导入记录器,并尝试4种不同的记录级别示例。

#import logger from logzero
from logzero import loggerlogger.debug("hello")
logger.info("info")
logger.warning("warning")
logger.error("error")

The output is colored so it's easy to read.

输出是彩色的,因此易于阅读。

As you can see each level has its own color. This means you can identify the level easily by checking the color.

如您所见,每个级别都有其自己的颜色。 这意味着您可以通过检查颜色轻松识别等级。

将日志写入文件 (Write logs to a file)

Most of the time Python users tend to write logs in the file. When the system is running you can save logs in the file and review them for error checks and maintenance purposes. You can also set a file to save all the log entries in legzero.

大多数时候,Python用户倾向于在文件中写入日志。 当系统运行时,您可以将日志保存在文件中,并检查它们以进行错误检查和维护。 您还可以设置文件以将所有日志条目保存在legzero中。

We will import the logger and logfile from logezero. The logfile method will help us configure the log file to save our log entries.

我们将从logezero导入记录器和日志文件。 logfile方法将帮助我们配置日志文件以保存日志条目。

Now your log entries will be logged into the file named my_logfile.log.

现在,您的日志条目将被记录到名为my_logfile.log的文件中。

#import logger and logfile
from logzero import logger, logfile#set logfile path
logfile('my_logfile.log')# Log messages
logger.info("This log message saved in the log file")

The output in the my_logfile.log contains the logging level label (for info level labeled as “I”), date, time, python filename, line number and the message itself.

my_logfile.log中的输出包含日志记录级别标签(对于信息级别标记为“ I”),日期,时间,python文件名,行号和消息本身。

[I 200409 23:49:59 demo:8] This log message saved in the log file

旋转日志文件 (Rotating a log file)

You don't need to have a single log file saving all the log entries. This results in a massive log file that is intensive for the system to open and close.

您无需保存所有日志条目的单个日志文件。 这将导致大量的日志文件,这对于系统打开和关闭而言非常耗时。

You can use the maxBytes and backupCount parameters to allow the file to roll over at a predetermined size. When the size is about to be exceeded, the file is closed and a new file is silently opened for output. Rollover occurs whenever the current log file is nearly maxBytes in length. If either maxBytes or backupCount is zero, rollover never occurs.

您可以使用maxBytesbackupCount参数来允许文件以预定大小滚动。 当将要超过该大小时,将关闭文件,并以静默方式打开一个新文件以进行输出。 只要当前日志文件的长度接近maxBytes,就会发生翻转。 如果maxBytes或backupCount为零,则永远不会发生过渡。

In the example below, we have set the maxBytes to be 1000000 bytes (1 MB). This means that when the size exceeds 1MB the file is closed and a new file is opened to save log entries. The number of backups to keep is set to 3.

在下面的示例中,我们将maxBytes设置为1000000字节(1 MB)。 这意味着,当大小超过1MB时,将关闭文件并打开一个新文件以保存日志条目。 要保留的备份数设置为3。

# Set a rotating logfile
logzero.logfile("my_logfile.log", maxBytes=1000000, backupCount=3)

设置最低日志记录级别 (Set a Minimum Logging Level)

The logging level means to set the importance level of a given log message. You can also set a different log level for the file handler by using the loglevel argument in the logfile method.

日志记录级别是指设置给定日志消息的重要性级别。 您还可以通过使用logfile方法中的loglevel参数为文件处理程序设置不同的日志级别

In the example below, we set loglevel to be warning. This means all log entries below the warning level will not be saved into a log file.

在下面的示例中,我们将loglevel设置为warning 。 这意味着低于警告级别的所有日志条目都不会保存到日志文件中。

#import logzero package
from logzero import logger, logfile
import logging# You can also set a different loglevel for the file handler
logfile("my_logfile.log", loglevel=logging.WARNING)# Log messages
logger.info("This log message saved in the log file")
logger.warning("This log message saved in the log file")

设置自定义格式器 (Set a custom formatter)

How you want the log record to be formated is up to you. There are different ways you can format your log record. You can include the date, time and logging level in your format so that you know when the log was sent and at what level.

您希望如何格式化日志记录取决于您自己。 您可以使用多种方式来格式化日志记录。 您可以采用格式包括日期,时间和日志记录级别,以便您知道何时发送日志以及处于什么级别。

The example below shows how you can configure the format of the log records.

下面的示例显示如何配置日志记录的格式。

#import logzero package
import logzero
from logzero import logger, logfile
import logging#set file path
logfile("my_logfile.log")# Set a custom formatter
my_formatter = logging.Formatter('%(filename)s - %(asctime)s - %(levelname)s: %(message)s');
logzero.formatter(my_formatter)# Log messages
logger.info("This log message saved in the log file")
logger.warning("This log message saved in the log file")

In the example above we have configured the log format by including filename, date, time, logging level name, and message.

在上面的示例中,我们通过包括文件名,日期,时间,日志记录级别名称消息来配置日志格式

This is the output in the my_logfile.log:

这是my_logfile.log中的输出:

demo.py - 2020–04–10 00:51:44,706 - INFO: This log message saved in the log file
demo.py - 2020–04–10 00:51:44,707 - WARNING: This log message saved in the log file

自定义记录器实例 (Custom Logger Instances)

Instead of using the default logger, you can also setup specific logger instances with logzero.setup_logger(..). You can configure and returns a fully configured logger instance with different parameters such as name, logfile name, formatter, maxBytes, backupCount, and logging level.

除了使用默认记录器之外,您还可以使用logzero.setup_logger(..)设置特定的记录器实例。 您可以使用不同的参数(例如名称,日志文件名称,格式化程序,maxBytes,backupCount日志记录级别)配置并返回完全配置的记录器实例

This is a working example of how to setup logging with a custom logger instance:

这是一个如何使用自定义日志记录器实例设置日志记录的有效示例:

import logzero package
from logzero import logger, logfile, setup_logger
import logging# Set a custom formatter
my_formatter = logging.Formatter('%(filename)s - %(asctime)s - %(levelname)s: %(message)s');#create custom logger instance
custom_logger = setup_logger(name="My Custom Logger",logfile="my_logfile.log",formatter=my_formatter,maxBytes=1000000,backupCount=3,level=logging.INFO)# Log messages
custom_logger.info("This log message saved in the log file")
custom_logger.warning("This log message saved in the log file")

In the example above we have set a custom logger instance called custom_logger with different configured parameter values.

在上面的示例中,我们使用不同的配置参数值设置了一个名为custom_logger的自定义记录器实例。

结语 (Wrap up)

In this article, you've learned the basics, along with some examples, of how to use the Logezero Python package. You can learn more about the features available in the documentation. Now you can start implementing the logzero package in your next python project.

在本文中,您学习了如何使用Logezero Python软件包的基础知识和一些示例。 您可以在文档中了解更多有关可用功能的信息 。 现在,您可以在下一个python项目中开始实现logzero包。

If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post! I can also be reached on Twitter @Davis_McDavid

如果您学习了新知识或喜欢阅读本文,请与他人分享,以便其他人可以看到。 在那之前,在下一篇文章中见! 也可以通过Twitter @Davis_McDavid与我联系

翻译自: https://www.freecodecamp.org/news/good-logging-practice-in-python-with-logzero/

python练习

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390434.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1736. 替换隐藏数字得到的最晚时间

给你一个字符串 time ,格式为 hh:mm(小时:分钟),其中某几位数字被隐藏(用 ? 表示)。 有效的时间为 00:00 到 23:59 之间的所有时间,包括 00:00 和 23:59 。 替换 time 中隐藏的数…

电脑棒安装linux_为什么要使用Linux? Linux很棒的11个理由

电脑棒安装linuxIn this article, well look at some of the things developers love about Linux so you can decide if its right for you.在本文中,我们将研究开发人员对Linux的一些喜爱,以便您可以确定它是否适合您。 持续有效的改进。 (Constant a…

1743. 从相邻元素对还原数组

存在一个由 n 个不同元素组成的整数数组 nums ,但你已经记不清具体内容。好在你还记得 nums 中的每一对相邻元素。 给你一个二维整数数组 adjacentPairs ,大小为 n - 1 ,其中每个 adjacentPairs[i] [ui, vi] 表示元素 ui 和 vi 在 nums 中相…

十 web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令

Scrapy框架安装 1、首先,终端执行命令升级pip: python -m pip install --upgrade pip2、安装,wheel(建议网络安装) pip install wheel3、安装,lxml(建议下载安装)4、安装,Twisted(建议下载安装)5、安装,Scrapy(建议网络…

阿里与珠海横琴新区达成战略合作,阿里云助力打造横琴智能岛

5月17日,阿里巴巴集团、蚂蚁金服集团与珠海横琴新区管理委员会签署战略合作协议,三方将围绕云计算、政务民生服务、城市治理、电子商务等领域展开深入合作,推动横琴产业发展,共同建设新型智慧城市。 (阿里巴巴集团、蚂…

chrome 开发工具_我最喜欢的Chrome开发工具提示和技巧

chrome 开发工具Chrome Developer Tools are a super powerful suite of tools for developing web applications. They can do so much, from very basic operations like traversing the DOM, to checking out network requests or even profiling your applications perform…

三十四 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解

信号一般使用信号分发器dispatcher.connect(),来设置信号,和信号触发函数,当捕获到信号时执行一个函数 dispatcher.connect()信号分发器,第一个参数信号触发函数,第二个参数是触发信号, 以下是各种信号 sig…

1713. 得到子序列的最少操作次数

给你一个数组 target ,包含若干 互不相同 的整数,以及另一个整数数组 arr ,arr 可能 包含重复元素。 每一次操作中,你可以在 arr 的任意位置插入任一整数。比方说,如果 arr [1,4,1,2] ,那么你可以在中间添…

CVE-2018-1000136:Electron nodeIntegration绕过漏洞

1周前,研究人员发现一个影响Electron所有版本的漏洞,利用该漏洞可以开启nodeIntegration,这可能会造成远程代码执行。Electron是一个使用JavaScript,HTML和CSS等Web技术创建原生程序的框架,它负责比较难搞的部分,而用户…

bash脚本 文件_如何使用Bash脚本来管理从AWS S3存储桶下载和查看文件

bash脚本 文件As you can read in this article, I recently had some trouble with my email server and decided to outsource email administration to Amazons Simple Email Service (SES). 正如您在本文中所读到的 ,最近我的电子邮件服务器遇到了一些麻烦&…

rsync(六)命令中文手册

rsync(1) rsync(1)名称rsync - 一个快速、多功能的远程(和本地)文件拷贝工具摘要Local: rsync [OPTION...] SRC... [DEST]Access via remote shell:Pull: rsync [OPTION...] [USE…

NFS共享存储服务部署

服务端部署 1、检查服务器上是否已安装nfs及rpc,没有则需要安装检查rpm -qa rpcbind nfs-utils安装(已安装略过)yum install -y rpcbind nfs-utils################################################################2、编写nfs的配置文件cat…

区块链运作机制_什么是区块链及其运作方式?

区块链运作机制If youre interested in technology, theres a good chance you’ve probably heard the terms Bitcoin, Crypto, Ethereum, or even "distributed, decentralized ledgers."如果您对技术感兴趣,那么您很有可能已经听说过比特币&#xff0c…

敏捷管理之绩效考核方案

前段时间,公司签了年终奖确认。觉得公司发放年终奖完全是凭主观发放,没有事实依据,由此产生了对如何发放年终奖的一些想法。 奖金发放作为激励员工最直接的手段,往往也是让管理人员最难抉择的,而且很多公司&#xff0c…

序言

为什么要写这篇文章? 说起架构,刚入行的新人觉得是高大上的技术,有工作经验的一些人又觉得是虚无缥缈的东西,不能落实。具体有用没用,我不给答案,想通过写这么一个例子来还原场景,让读者自己判断…

kotlin编程语言_Kotlin初学者编程基础

kotlin编程语言什么是Kotlin? (What is Kotlin?) Kotlin is a programming language developed by Jetbrains, the company behind some of the world’s most popular IDEs like IntelliJ and Pycharm.Kotlin是Jetbrains开发的一种编程语言,该公司是In…

记一个蒟蒻的绝望

感觉现在…… 怎么讲,心挺冷的。 今天一月五号了。距离省选,时间好短啊。 我还有那么多东西不懂。甚至听都没听说过。 等到真正去省选的时候,我可能跟现在一样,什么都不会。 我的名字能不能被看到都不知道。哈,还进队呢…

671. 二叉树中第二小的节点

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。 更正式地说,root.val min(root.left.val, root.right.val) 总…

CentOS查询端口占用和清除端口占用的程序

1、查询端口号占用,根据端口查看进程信息 [rootserver2 ~]# lsof -i:80COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEhttpd 5014 root 3u IPv4 14346 TCP server2:http (LISTEN)2、根据进程号查看进程对应的可执行程序 ps -f -p 进程号# p…

Android基础夯实--你了解Handler有多少?

概述 对于刚入门的同学来说,往往都会对Handler比较迷茫,到底Handler是个什么样的东西。当然,可能对于一些有工作经验的工程师来说,他们也不一定能很准确地描述,我们来看下API的介绍。 Handler是用来结合线程的消息队列…