Using Python's logging module to log logs

Source: Internet
Author: User

Python has a logging module to facilitate the recording of program action logs, this article is a simple example of the use of the logging module:

First introduce logging:

Import xlrd
Import MySQLdb
Import OS
Import logging

First initialize the configuration in the main program, such as:

if __name__ = = "__main__":
Logging.basicconfig (filename= './log.txt ', level=logging. DEBUG)
Path = './excel '
Main (PATH)

Specifies the file that holds the log, which is the Log.txt file log in the current directory. Where you need to record the operation:

Try
C.executemany (
' "'" INSERT into ' order ' VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%. %s,%s,%s) "" ",
Data
Db.commit ()
Except Exception as E:
Logging.info (e)
Db.close ()

Or

def main (path):
"""
Open Directory traverse Excel file and store to MySQL
"""
Files = Os.listdir (path)
For file in Files:
Save_file (path + '/' + file)
Print (file)
Logging.info (file)

Look at the highlighted section of the code so that the log is logged to the specified file.

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.