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练习

python练习_如何使用Logzero在Python中练习记录相关推荐

  1. 零基础学python 视频_零基础入门学习PYTHON(第2版)(微课视频版)

    小甲鱼畅销图书重磅升级,针对Python 3.7,通过生动的实例,让读者在实践中理解概念,在轻松.愉快中学会Python! 本书提倡理解为主,应用为王.因此,只要有可能,小甲鱼(注:作者)都会通过生动 ...

  2. windows10安装python环境_在windows10下安装python(配置环境变量),Windows10

    建议使用:anaconda(环境管理和包管理)+Pycharm (强大的IDE开发工具) python环境变量设置: window系统高级设置-> 系统环境变量里 变量名: PYTHONPATH ...

  3. 怎么查看自己安装的python版本_教你如何检查 Python 版本

    本教程介绍如何使用 命令行检查操作系统上安装的 Python 版本.这在安装用 Python 编写的需要特定版本 Python 的应用程序时非常有用. Python 是世界上最流行的编程语言之一.它用 ...

  4. geany搭建python环境_第一章:搭建Python的开发环境

    第一步: 我们在Python的官网上下载Python的安装包,此安装包内含有Python解释器.Python运行的基础库,以及交互式运行工具--Python Shell 第二步: 安装时弹出下面对话框 ...

  5. 头条上python广告_满大街都是Python广告,真的如广告说的那样方便吗?,学精通后真能月如过万吗?业内大佬怎么看?...

    在编程领域.Python语言已经被吹得神乎其神.很多人已经产生了一种错觉:仿佛当今编程世界,唯Python莫属,其它语言皆为渣渣. 就连地产大佬潘石屹也发微博为Python背书,不愧两者都姓" ...

  6. julia与python对比_有人说Julia比Python好,还给出了5个理由

    选自medium 作者:Emmett Boudreau 机器之心编译 参与:杜伟.张倩.肖清 本文作者从速度.通用性.多重派发.适用于 ML 的程度和包管理器 5 个方面阐述了 Julia 语言相较于 ...

  7. python+[:]+切片_我从C ++到Python的方式:概念上的改变

    python+[:]+切片 by asya f 由asya f 我从C ++到Python的方式:概念上的改变 (How I went from C++ to Python: a conceptual ...

  8. sublime运行python代码_怎么用sublime运行python

    首先需要安装一个Sublime Text(http://www.sublimetext.com/)和一个Python(https://www.python.org/downloads/) 接下来打开S ...

  9. pycharm python部署_使用PyCharm配合部署Python的Django框架的配置纪实

    安装软件安装 Python 2.7.PyCharm.pip(Python包管理工具).Django ( pip install Django) 部署PyCharm 新建Django工程 完成后,其目录 ...

最新文章

  1. Android实现程序前后台切换效果
  2. 自学python能赚钱吗-学习Python多久能找到工作?老男孩Python开发培训
  3. Ubuntu 安装 opencv-nonfree
  4. 如何查找计算机开机项,怎么查看电脑启动项 电脑开
  5. 【转载】数据库操作:添加、插入、更新语句
  6. 两个前置摄像头_W21 5G性能篇 | 五摄像头组合,洞悉画面之美
  7. 25岁房贷上万濒临失业,大专小哥转行软件测试“挽救”人生
  8. linux线程退出正确姿势demo
  9. 2022爱分析・汽车行业数字化实践报告
  10. 寨板p45黑苹果10.12.6
  11. 计算机视觉教程2-8:你知道图像背景虚化效果的原理吗?(附代码)
  12. 计算机主机显卡装在哪,电脑显卡在主板上的哪个位置?怎么查看显卡的信息和更新驱动程序?...
  13. 软件测试自动感应灯,原来这么简单——四种常见LED感应灯工作原理
  14. 青蛙跳台阶(pta)
  15. Boot重抽样获取logistic回归内部验证AUC可信区间
  16. Proxifier使用代理ip教程
  17. 常测长策(20180222~)
  18. 如何组建权责明确、运营高效的数据团队
  19. mysql计算订单总金额_【写SQL语句】按照用户统计对应订单数和订单总金额?
  20. 编写Java程序时的一些经验总结

热门文章

  1. C++标准库之String
  2. 1087 有多少不同的值 (20 分)
  3. Java进阶之光!javaunicode码转字符
  4. 前端 HTML 常用标签 head标签相关内容 script标签
  5. Spring 中的 LocalSessionFactoryBean和LocalContainerEntityManagerFactoryBean
  6. Centos 7和 Centos 6开放查看端口 防火墙关闭打开
  7. 有一句说一千句,是作家....
  8. 【xml】利用OpenCV解析
  9. IOS UIPageController
  10. dll文件的c++制作dll文件的c++制作