In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "python how to solve the duplicate problem of console printing log output". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "python how to solve the problem of console printing log output repetition".
Let's first take a look at the code of the file log.py:
Code example:''function description: realize the function of recording both the console and the file at the same time. Writer: Chao Brother write date: step Analysis: 1-configure logger name 2-configure log level 3-configure log format (can be set separately Can also be set uniformly) 4-create and add handler- console 5-create and add handler- files 6-provide external access to logger''' import loggingimport sys def log (): # 1-configure logger name logger = logging.getLogger ('AutoTest') # 2-configure log level logger.setLevel (logging.DEBUG) # 3-configure log format (can be set separately You can also set it uniformly) format = logging.Formatter ('% (name) s asctime% (message) s') # 4-create and add handler-console sh = logging.StreamHandler () sh.setFormatter (format) logger.addHandler (sh) # 5-create and add handler-file fh = logging.FileHandler ('test.log') fh.setFormatter (format) logger.addHandler (fh) # 6-provide logger return logger if _ _ name__ = ='_ _ main__': logger = log () logger.info ('log method defined by function')
We create another file in the same directory:
After we import the written log.py file
From xx directory import log log (). Info ('xxx1') log (). Info (' xxx2') log (). Info ('xxx3')
After the first call, there are already two handler in handlers, namely console handle StreamHandler and file handle FileHandler. In the figure below, the second call add handle
After execution, you will find that there is an extra StreamHandler in the handlers.
How to solve this situation, there are two solutions, we list two kinds of code respectively:
The first is to use singleton mode and add a line to the log.py file: logger = log (). The purpose of this sentence is to instantiate the object in advance, and other modules are applicable to this object, so the import statement of other modules should be changed to: from xxx package. Log import logger, and then use logger.info ('xxxx') to output the log.
…… Omit the above code
# 6-provide a method to obtain logg return logger # add a line logger = log () if _ _ name__ ='_ _ main__': logger = log () logger.info ('log method defined by function')
Import:
From xx package import logger logger.info ('xxx1') logger.info (' xxx2') logger.info ('xxx3')
Output:
The second scheme: log.py determines whether handlers already exists each time.
…… # 4-create and add handler-console sh = logging.StreamHandler () sh.setFormatter (format) # 5-create and add handler-file fh = logging.FileHandler ('test.log') fh.setFormatter (format) # determine whether if not logger.handlers is empty when adding handler: logger.addHandler (sh) logger.addHandler (fh) # 6- Provide a method to obtain logg return logger if _ _ name__ = ='_ _ main__': logger = log () logger.info ('use the log method defined by the function')
The import file code remains the same:
From xx package import log log (). Info ('xxx1') log (). Info (' xxx2') log (). Info ('xxx3') Thank you for your reading. The above is the content of "how to solve the problem of log output repetition in console printing by python". After the study of this article, I believe you have a deeper understanding of how python solves the problem of log output duplication in console printing. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.