In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how python uses the nb_log module to capture logs. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Research log module
I want to write a log module, the first is the python built-in logging module, and then look for external third-party modules, at a glance to see our Chinese-written module nb_log. It is estimated to be a "awesome log", which has to be said to be quite "rigorous". It is also to my taste. The address of pypi is as follows:
Nb-log PyPIvery sharp color display,monkey patch bulitin print and high-performance multiprocess safe roating file handler,other handlers includeing dintalk, email,kafka,elastic and so on https://pypi.org/project/nb-log/
It's hard to say? The full text of the module introduction is quite talkative, looks quite amateur and boasts a lot, but it is very much to my taste! Birds of a feather flock together. If you don't believe me, look at the official description (just the tip of the iceberg):
There are many comparisons in the article, such as other modules or some blog articles directly pulled out and shot, looks extremely arrogant, which will not be shot, anyway, it is very in line with my taste! Use it according to the "official documentation" now!
Second, the use of nb_log module 1, installation mode
Pip install nb_log
As shown in the following figure, there are some hints of unsuccessful installation during the installation process, but everything is fine in the end:
2. Automatic jump function
As shown in the figure, there is also a jump to print (), because the author likes logs rather than print ().
Import nb_log print ("I'm nb_log!!")
3. Screen flow log effect print ("before importing nb_log, my print is very ordinary") from nb_log import get_logger print ("after importing nb_log, my print is good!") Logger = get_logger ('lalala') logger.debug ("my name is debug") logger.info ("my name is info") logger.warning ("my name is warning") logger.error ("my name is error") logger.critical ("my name is critical")
4. Write log to file
The error is not avoided, and the modification is as follows:
From multiprocessing import Processfrom nb_log import LogManager, get_logger # specifies that log_filename is automatically written to the file without None, and the default is filehandler, which is a multi-process safe cutting method. # console logs are added by default. If you do not want console logs, set is_add_stream_handler=False# to keep the number of method entries small. The specific cutting size and the number of backup files have default values. # if you need to modify the cutting size and the number of files, specify them in the nb_log_config.py file automatically generated by the root directory of the current python project. # logger = LogManager ('ha'). Get_logger_and_add_handlers (is_add_stream_handler=True,# log_filename='ha.log') # get_logger this is the same as the above sentence. But get_logger_and_add_handlers is not the only public method for LogManager. Logger = get_logger (name= "all_log", is_add_stream_handler=True, log_filename='ha.log') def f (): for i in range (10): logger.debug ('test file write performance at 1. Multi-process running 2. Under the premise that the backup is automatically cut by size, backup 3 does not make an error instantly'', verify whether this is the highest file writing speed in the history of python (python logging handler') if _ _ name__ = ='_ main__': [Process (target=f). Start () for _ in range (10)]
After running, it is not clear where the file went. I tried to find the current project through the file system, but I couldn't find it. From the point of view of the use of the whole module, I think it may be very powerful, but both the documentation and the program itself are very irregular!
Look up the whole world and find that the original log is written here:
I guess this function should have a path input, so I typed path to define it, as follows:
Logger = get_logger (name= "all_log", is_add_stream_handler=True, log_path= ". /", log_filename='ha.log')
This is the end of the article on "how python uses the nb_log module to capture logs". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.