Python: Managing logs with the logbook module

Source: Internet
Author: User

Log management, as a common part of a software project, is particularly important in both development and automated testing processes.

Originally intended to use Python's logging module to manage logs, it later looked at GitHub and others ' automated framework designs, made a comparison, or decided to use logbook.

This blog, describes the use of the logbook module in Python, for reference only ...

First, download the installation

1. File Installation

Download the logbook installation file from the official website and install it.

Website Link: https://pypi.org/project/Logbook/

2. PIP Command Installation

Enter the cmd command line, enter the pip install logbook command to install, and after successful installation, enter the pip show logbook command to view the relevant information.

Ii. Introduction of Logbook

Logbook is designed to replace the standard library log module for Python: Logging. Details can be obtained through the links below to view official documentation instructions:

Official Document: Http://logbook.readthedocs.io/en/stable/index.html

Iii. Description of Usage

The sample code is as follows:log.py

#Coding=utf-8ImportOSImportSYSImportLogbook fromLogbookImportLogger,streamhandler,filehandler,timedrotatingfilehandler fromLogbook.moreImportColorizedstderrhandlerdefLog_type (record,handler): Log="[{Date}] [{level }] [{filename}] [{func_name}] [{Lineno}] {msg}". Format (Date= Record.time,#Log TimeLevel = Record.level_name,#Log Levelfilename = os.path.split (record.filename) [-1],#file nameFunc_name = Record.func_name,#Name of functionLineno = Record.lineno,#Line numbermsg = Record.message#Log Content    )    returnLog#Log Storage PathLog_dir = Os.path.join ("Log")if  notos.path.exists (Log_dir): Os.makedirs (Log_dir)#log Print to screenLOG_STD = Colorizedstderrhandler (bubble=True) Log_std.formatter=Log_type#log Print to fileLog_file =Timedrotatingfilehandler (Os.path.join (Log_dir,'%s.log'%'Log'), date_format='%y-%m-%d', Bubble=true, encoding='Utf-8') Log_file.formatter=Log_type#Script LogRun_log = Logger ("Script_log")defInit_logger (): Logbook.set_datetime_format ("Local") Run_log.handlers=[] Run_log.handlers.append (Log_file) run_log.handlers.append (LOG_STD)#instantiation, default callLogger = Init_logger ()

Code parsing:

1, define the log file type, followed by time, log level, test file name, function method name, number of lines, specific information to display;

Log Level:

Level Describe
Critical A critical error can cause the program to exit
Error Error within controllable range
Warning Warning message
Notice Most of the cases want to see the record
Info Most cases do not want to see the records
Debug Verbose output records when debugging a program

2, the definition log storage path is the log folder;

3, logbook the log output of 2 kinds: Print to the screen (more suitable for debugging, the formal use can be commented out) and print output to the log file;

4, define the log output;

5, instantiation, convenient for other modules to call;

You can test the code to see if the log is printed to the corresponding path, and the test code is as follows:test_log.py

# Coding=utf-8 Import OS  from Import Run_log as Logger if __name__ ' __main__ ' :    logger.info (" Test log module, temporarily optimize to this step, further improvements ")

Test results:

As shown above, that is the basic use of logbook, the code is for reference only, the specific use please self-practice ...

Python: Managing logs with the logbook module

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.