Python Script log system

Source: Internet
Author: User
Tags python script

Python provides log functionality through the logging module, and there is already a lot of detailed information on the use of the logging module, and here is how to use the log function in the actual project.

Suppose you want to develop an automated scripting tool, the project structure is as follows, common this package is the implementation of the framework function, the scripts directory is the test case script we write (please ignore other unrelated directories).

Our requirements for the logging feature are as follows:

1 in order to facilitate the view of the log, each script corresponds to a log file, the log file is named after the name of the script

2nd log path and the journal capacity saved per script can be set, such as set to 5MB, then the oldest log is automatically overwritten

3rd log function to be easy to use, reduce the coupling with the framework business functions

  

Now, let's analyze each of these requirements individually.

1 to implement a log file for each script, you need to generate a log file in the log module, based on the name of the use case script, the key question here is how to get the name of the use case script in the log module.

The common ways to get filenames are: OS.GETCWD (), sys.argv[0], __file__, look at the various functions:

Write the following code in a file (assuming test.py):

  

Then in another file (assuming script1.py), import test, and then call the Func method:

  

Run the script1.py with the result:

  

Visible, OS.GETCWD () Gets the directory that executes the script, sys.argv[0] is the absolute path name of the execution script, and __file__ is the absolute pathname of the file where the code is executed.

Now it is clear that we should use sys.argv[0] to get the name of the execution script, because the obtained absolute path, need to do a bit of processing: sys.argv[0].split ('/') [-1].split ('. ') [0]

2nd log capacity problem, to achieve over capacity automatically overwrite the oldest log, using the Rotatingfilehandler class in logging, you can set the size of the log file, as well as the number of backups.

So where does the log path and capacity configuration fit? Let the user directly modify the parameters of the Rotatingfilehandler is not good, it is best not to let the user modify the framework file, the user simply call the interface to write their own script.

The scenario used here is to write the configuration information to a file that the XML file is more appropriate to use as a configuration file, the user modifies the XML file to make the configuration, and the log module reads the parameters from the XML file.

Here, for the convenience of placing the XML file under Common, it is named config:

<?XML version= "1.0" encoding= "Utf-8"?><Config>    <!--Log Save path -    <LogPath>E:\PythonLog</LogPath>    <!--log file size per script, in megabytes -    <LogSize>8</LogSize>    <!--number of log files saved per script -    <Lognum>3</Lognum></Config>

Reading the contents of the XML file, using the lxml library is very simple, and the code is given later.

  

3rd log function to be easy to use, reduce the coupling with the framework business functions, it is best to encapsulate the logging function, only provide logging interface.

Log interface in the form of a class method can meet the above requirements, the user only need to call through the class logging interface, anywhere call, easy to use, and no need to define class instances, and framework business is not coupled.

    

With the above analysis, we will implement the log module.

Since the log function is also part of the Framework Foundation, we also put the log module in the common package, under common new log.py file, the code is as follows:

1 #Coding:utf-82 3  fromlxmlImportetree4 Importlogging.handlers5 ImportLogging6 ImportOS7 ImportSYS8 9 #provides logging capabilitiesTen classLogger: One     #read the configuration data in the XML file first A     #because CONFIG. xml is placed in the same directory as the current file, it is __file__ to get the directory of the file and then stitch it into an absolute path -     #this uses the lxml library to parse the XML -Root = Etree.parse (Os.path.join (Os.path.dirname (__file__),'config .') . Getroot () the     #read log file save path -LogPath = Root.find ('LogPath'). Text -     #read log file capacity, convert to bytes -LogSize = 1024*1024*int (Root.find ('LogSize'). Text) +     #Read log file Save Count -lognum = Int (Root.find ('Lognum'). Text) +  A     #Log file name: The name of the use case script, combined with the log save path, to get the absolute path of the log files atLogName = Os.path.join (LogPath, Sys.argv[0].split ('/') [ -1].split ('.') [0]) -  -     #Initialize Logger -Log =Logging.getlogger () -     #log format, which can be set as needed -FMT = logging. Formatter ('[% (asctime) s] [% (filename) s][line:% (Lineno) d][% (levelname) s]% (message) s','%y-%m-%d%h:%m:%s') in  -     #log output to a file, here used to get the above log name, size, save number toHandle1 = Logging.handlers.RotatingFileHandler (logname, Maxbytes=logsize, backupcount=lognum) + Handle1.setformatter (FMT) -     #simultaneous output to the screen for easy observation theHandle2 = logging. Streamhandler (stream=sys.stdout) * Handle2.setformatter (FMT) $ Log.addhandler (handle1)Panax Notoginseng Log.addhandler (handle2) -  the     #set the log basic, here is set to info, indicating that only the info level and above will print + log.setlevel (logging.info) A  the     #log interface, the user only need to call the interface here, here only the info, WARNING, error three levels of log, can be defined as needed more interfaces + @classmethod -     definfo (CLS, msg): $ cls.log.info (msg) $         return -  - @classmethod the     defWarning (CLS, msg): - cls.log.warning (msg)Wuyi         return the  - @classmethod Wu     deferror (CLS, msg): - cls.log.error (msg) About         return

To test, write the following code separately in scripts script1 and SCRIPT2:

 from Import *logger.info ('this isinfo') logger.warning (' This was warning') logger.error ('This iserror')

Run two scripts separately, console output is:

  

The resulting log file:

  

File contents:

  

Well, now, whether it's in other files in the framework, or in user scripts, it's easy to log through the log interface of the Logger class.

  

Python Script log system

Related Article

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.