Python uses the logging module for Log writes

Source: Internet
Author: User

Python implements the ability to write logs to logging. The logging module is still very useful.

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 #@Time: 2018/4/25 17:055 #@Author: Zms6 #@Site:7 #@File: log.py8 #@Software: pycharm Community Edition9 Ten Import Time One ImportLogging A ImportOS -  fromLogging.handlersImportRotatingfilehandler -  theProject_dir = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) -Log_path = Project_dir + R"\log\\" -  -  + classLog (logging. Logger): -     def __init__(self, logname): +         #Super (MyLogger, self). __init__ (filename) Afilename = log_path + logname +'. Log' atLogging. Logger.__init__(self, filename) -  -         #set the log format -Fmthandler = logging. Formatter ('% (asctime) s [% (filename) s:% (Lineno) s][% (levelname) s]% (message) s') -  -         #Terminal log output stream settings in         Try: -Consolehd =logging. Streamhandler () to Consolehd.setlevel (logging. ERROR) + Consolehd.setformatter (Fmthandler) - Self.addhandler (CONSOLEHD) the         exceptException as Reason: *Self.error ("%s"%reason) $ Panax Notoginseng             #setting up the log file -         Try: the os.makedirs (os.path.dirname (filename)) +         exceptException as Reason: A             Pass the         Try: +             #set the rollback log with a maximum of 10M per log and up to 5 logs -Filehd =Logging.handlers.RotatingFileHandler ( $FileName, maxbytes=10 * 1024x768, backupcount=5) $             #Filehd = logging. Filehandler (filename) - filehd.setlevel (logging.info) - Filehd.setformatter (Fmthandler) the Self.addhandler (FILEHD) -         exceptException as Reason:WuyiSelf.error ("%s"%reason) the  -         return Wu  -  About  $ if __name__=='__main__': -Test1 = Log ('test1') -Test2 = Log ('test2') -      whileTrue: ATest1.error ("test1") +Test2.error ("test2")

Python uses the logging module for Log writes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.