In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly describes how to use logzero in Python for simple logging, the article is very detailed, has a certain reference value, interested friends must read it!
The logzero library makes logging as easy as printing statements, and is an excellent example of simplicity. I'm not sure if logzero's name fits with "zero boilerplate libraries" like pygame-zero, GPIO Zero, and guizero, but it definitely falls into that category. It is a Python library that makes logging simple and straightforward.
You can use it for basic logging to standard output, just as you can use print for information and debugging, and the learning curve for learning its more advanced logging (e.g. logging to files) is smooth.
First, install logzero using pip:
$ sudo pip3 install logzero
In a Python file, import logger and try one or all of the following log instances:
from logzero import logger logger.debug("hello")logger.info("info")logger.warning("warning")logger.error("error")
The output is automatically colored in an easy-to-read manner:
Python, Raspberry Pi: import logger
So instead of using print to see what's going on, use a logger with an associated log level.
Writing logs to files in Python
If you read this and make a few changes as you write code, that's enough for me. If you want to know more, keep reading!
Writing to standard output is great for testing new programs, but it's only useful if you log on to the computer running the script. In many cases, you need to execute code remotely and check for errors afterwards. In this case, it is helpful to document it. Let's try:
from logzero import logger, logfile logfile('/home/pi/test.log')
Your log entries will now be recorded in the file test.log. Remember to make sure the script has permission to write to the file and its directory structure.
You can also specify more options:
logfile('/home/pi/test.log', maxBytes=1e6, backupCount=3)
Now, when the data supplied to the test.log file reaches 1MB (106 bytes), it will be written alternately through the test.log.1, test.log.2, and so on. This behavior prevents the system from opening and closing so many I/O intensive log files that the system cannot open and close them. To be more professional, you might also want to log to/var/log. Assuming you are using Linux, create a directory and make the user the owner so that you can write to it:
$ sudo mkdir /var/log/test$ sudo chown pi /var/log/test
Then in your Python code, change the logfile path:
logfile('/var/log/test/test.log', maxBytes=1e6, backupCount=3)
When you want to catch an exception in a logfile, you can use logging.exception:
try: c = a / bexcept Exception as e: logger.exception(e)
This will output (in case b is zero):
[E 190422 23:41:59 test:9] division by zero Traceback (most recent call last): File "test.py", line 7, in c = a / b ZeroDivisionError: division by zero
You'll get the logs, complete with backtracking. Alternatively, you can use logging.error and hide backtracking:
try: c = a / bexcept Exception as e: logger.error(f"{e.__ class__.__ name__}: {e}")
Now, this will produce a simpler result:
[E 190423 00:04:16 test:9] ZeroDivisionError: division by zero
That's all for "How to use logzero for simple logging in Python." Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.