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