python练习_如何使用Logzero在Python中练习记录
python練習(xí)
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創(chuàng)建的Python程序包,它簡(jiǎn)化了Python 2和3的日志記錄。Logzero使它更容易用作顯示信息和調(diào)試詳細(xì)信息的打印語(yǔ)句。
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.
如果您想知道什么是日志記錄 ,我建議您閱讀上一篇有關(guān)“如何使用Python日志記錄模塊運(yùn)行機(jī)器學(xué)習(xí)實(shí)驗(yàn)”的文章 ,特別是前三部分。
In that article, you will learn:
在該文章中,您將學(xué)習(xí):
- What is Logging? 什么是日志記錄?
- Why logging is important. 為什么記錄很重要。
- Applications of logging in different technology industries. 伐木在不同技術(shù)行業(yè)中的應(yīng)用。
Logzero has different features that make it easier to use in Python projects. Some of these features are:
Logzero具有不同的功能,可以更輕松地在Python項(xiàng)目中使用。 其中一些功能是:
- Easy logging to console and/or file. 輕松記錄到控制臺(tái)和/或文件。
- Provides a fully configured standard Python logger object. 提供完整配置的標(biāo)準(zhǔn)Python記錄器對(duì)象。
Pretty formatting, including level-specific colors in the console.
漂亮的格式,包括控制臺(tái)中特定于級(jí)別的顏色 。
- works with all kinds of character encodings and special characters. 適用于各種字符編碼和特殊字符。
- Compatible with Python 2 and 3. 與Python 2和3兼容。
- No further Python dependencies. 沒有更多的Python依賴項(xiàng)。
安裝 (Installation)
To install logzero with pip run the following:
要使用pip安裝logzero,請(qǐng)運(yùn)行以下命令:
pip install -U logzeroYou can also install logzero from the public Github repo:
您還可以從公共Github存儲(chǔ)庫(kù)安裝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.
我們將從一個(gè)基本示例開始。 在python文件中,我們將從logzero導(dǎo)入記錄器,并嘗試4種不同的記錄級(jí)別示例。
#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.
如您所見,每個(gè)級(jí)別都有其自己的顏色。 這意味著您可以通過檢查顏色輕松識(shí)別等級(jí)。
將日志寫入文件 (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.
大多數(shù)時(shí)候,Python用戶傾向于在文件中寫入日志。 當(dāng)系統(tǒng)運(yùn)行時(shí),您可以將日志保存在文件中,并檢查它們以進(jìn)行錯(cuò)誤檢查和維護(hù)。 您還可以設(shè)置文件以將所有日志條目保存在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導(dǎo)入記錄器和日志文件。 logfile方法將幫助我們配置日志文件以保存日志條目。
Now your log entries will be logged into the file named my_logfile.log.
現(xiàn)在,您的日志條目將被記錄到名為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中的輸出包含日志記錄級(jí)別標(biāo)簽(對(duì)于信息級(jí)別標(biāo)記為“ I”),日期,時(shí)間,python文件名,行號(hào)和消息本身。
[I 200409 23:49:59 demo:8] This log message saved in the log file旋轉(zhuǎn)日志文件 (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.
您無需保存所有日志條目的單個(gè)日志文件。 這將導(dǎo)致大量的日志文件,這對(duì)于系統(tǒng)打開和關(guān)閉而言非常耗時(shí)。
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.
您可以使用maxBytes和backupCount參數(shù)來允許文件以預(yù)定大小滾動(dòng)。 當(dāng)將要超過該大小時(shí),將關(guān)閉文件,并以靜默方式打開一個(gè)新文件以進(jìn)行輸出。 只要當(dāng)前日志文件的長(zhǎng)度接近maxBytes,就會(huì)發(fā)生翻轉(zhuǎn)。 如果maxBytes或backupCount為零,則永遠(yuǎn)不會(huì)發(fā)生過渡。
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設(shè)置為1000000字節(jié)(1 MB)。 這意味著,當(dāng)大小超過1MB時(shí),將關(guān)閉文件并打開一個(gè)新文件以保存日志條目。 要保留的備份數(shù)設(shè)置為3。
# Set a rotating logfile logzero.logfile("my_logfile.log", maxBytes=1000000, backupCount=3)設(shè)置最低日志記錄級(jí)別 (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.
日志記錄級(jí)別是指設(shè)置給定日志消息的重要性級(jí)別。 您還可以通過使用logfile方法中的loglevel參數(shù)為文件處理程序設(shè)置不同的日志級(jí)別 。
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設(shè)置為warning 。 這意味著低于警告級(jí)別的所有日志條目都不會(huì)保存到日志文件中。
#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")設(shè)置自定義格式器 (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.
您希望如何格式化日志記錄取決于您自己。 您可以使用多種方式來格式化日志記錄。 您可以采用格式包括日期,時(shí)間和日志記錄級(jí)別,以便您知道何時(shí)發(fā)送日志以及處于什么級(jí)別。
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.
在上面的示例中,我們通過包括文件名,日期,時(shí)間,日志記錄級(jí)別名稱和消息來配置日志格式。
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自定義記錄器實(shí)例 (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.
除了使用默認(rèn)記錄器之外,您還可以使用logzero.setup_logger(..)設(shè)置特定的記錄器實(shí)例。 您可以使用不同的參數(shù)(例如名稱,日志文件名稱,格式化程序,maxBytes,backupCount和日志記錄級(jí)別)配置并返回完全配置的記錄器實(shí)例。
This is a working example of how to setup logging with a custom logger instance:
這是一個(gè)如何使用自定義日志記錄器實(shí)例設(shè)置日志記錄的有效示例:
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.
在上面的示例中,我們使用不同的配置參數(shù)值設(shè)置了一個(gè)名為custom_logger的自定義記錄器實(shí)例。
結(jié)語(yǔ) (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.
在本文中,您學(xué)習(xí)了如何使用Logezero Python軟件包的基礎(chǔ)知識(shí)和一些示例。 您可以在文檔中了解更多有關(guān)可用功能的信息 。 現(xiàn)在,您可以在下一個(gè)python項(xiàng)目中開始實(shí)現(xiàn)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
如果您學(xué)習(xí)了新知識(shí)或喜歡閱讀本文,請(qǐng)與他人分享,以便其他人可以看到。 在那之前,在下一篇文章中見! 也可以通過Twitter @Davis_McDavid與我聯(lián)系
翻譯自: https://www.freecodecamp.org/news/good-logging-practice-in-python-with-logzero/
python練習(xí)
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python练习_如何使用Logzero在Python中练习记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到挑选被子怎么回事
- 下一篇: Python的用途是什么? Python