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