Python logging module print log to specified file

Source: Internet
Author: User

Maybe we often use print to output information to the window, but when we have a lot of py files to run, the project is more than

The larger the time, print is simply too low point. Then we can use the powerful logging module to

Output to a file in a path to the specified local PC.

I. Framework of logging
1, loggers: The interface that can be called directly by the program, the app logs the log by invoking the provided API
2. Handlers: Decide to assign log records to the correct destination
3, Filters: Filtering log information to provide more granular log output of the judgment
4, formatters: Make the final record printing format layout

Second, log level
The system has a default of 6 levels, priority:

CRITICAL    50ERROR      40WARNING   30INFO        20DEBUG      10NOTSET     0

When setting the log to be printed, you only need to set the priority, such as setting the print info, then the warning/error/critical that is higher than the info priority will be printed.

Third, the normal printing

The error and warning information is only output here because the default output level of logging is warning.

Iv. output to the specified file
Let's take a look at the code written like this:

import loggingimport unittestclass lgtest(unittest.TestCase):    logging.basicConfig(filename=‘../LOG/‘+__name__+‘.log‘,format=‘[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]‘, level = logging.DEBUG,filemode=‘a‘,datefmt=‘%Y-%m-%d %I:%M:%S %p‘)    def test(self):        logging.error("这是一条error信息的打印")        logging.info("这是一条info信息的打印")        logging.warning("这是一条warn信息的打印")        logging.debug("这是一条debug信息的打印")if __name__==‘__main__‘:    unittest.main()

Use Logging.basicconfig to specify the file to output and the output form of log, including the time format, log level, and so on.

FileName: The file that specifies the path. The +-name-+ is used here to name the log as the current py.
Format: Formats the display of the log (that is, the format you see in the document). Time + current filename +log output level + output information, respectively
Levels: The log level of the output will not be saved to the log document if the priority is lower than the set level
Filemode:log Open Mode
A: The log continues to be written on behalf of each running program. The previously saved log information is not overwritten.
W: The log is re-written every time the program is run. That is, overwrite the previously saved log information

V. Final log Document

The file name is the same as the Py file name, because Basicconfig uses name to get it.

Let's take a look at the content, which is the contents of the Py file that ran two times:

[2018-02-10 02:29:57 PM-Logout.py-ERROR:这是一条error信息的打印][2018-02-10 02:29:57 PM-Logout.py-INFO:这是一条info信息的打印][2018-02-10 02:29:57 PM-Logout.py-WARNING:这是一条warn信息的打印][2018-02-10 02:29:57 PM-Logout.py-DEBUG:这是一条debug信息的打印][2018-02-10 02:39:32 PM-Logout.py-ERROR:这是一条error信息的打印][2018-02-10 02:39:32 PM-Logout.py-INFO:这是一条info信息的打印][2018-02-10 02:39:32 PM-Logout.py-WARNING:这是一条warn信息的打印][2018-02-10 02:39:32 PM-Logout.py-DEBUG:这是一条debug信息的打印]
运行一次会有4个信息打印出来,因为filemode设置成了a,所以再次运行时不会覆盖

Before the log information, there are 8 messages.

Ok,log output to a document is as simple as this. Of course, if you get acquainted, you can write your own recorder, filter and so on.

Python logging module print log to 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.