Logging
Module for easy logging and thread-safe
Importlogging logging.basicconfig (filename='Log.log', Format='% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s', Datefmt='%y-%m-%d%h:%m:%s%p', level=10) Logging.debug ('Debug') Logging.info ('Info') logging.warning ('Warning') Logging.error ('Error') logging.critical ('Critical') Logging.log (10,'Log')
Log level:
CRITICAL = = ==+ = == =0
Note: log files are logged only if the current write level is greater than the log level.
2. Multi-File Log
For the above logging functionality, only the log can be recorded in a single file, if you want to set up multiple log files, Logging.basicconfig will not complete, you need to customize the file and Log action objects.
#definition fileFile_1_1 = logging. Filehandler ('L1_1.log','a', encoding='Utf-8') FMT= Logging. Formatter (fmt="% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s") File_1_1.setformatter (FMT) file_1_2= Logging. Filehandler ('L1_2.log','a', encoding='Utf-8') FMT=logging. Formatter () file_1_2.setformatter (FMT)#Defining LogsLogger1 = logging. Logger ('S1', level=logging. ERROR) Logger1.addhandler (file_1_1) Logger1.addhandler (file_1_2)#Write LogLogger1.critical ('1111')Log One
# definition file file_2_1 = logging. Filehandler ('l2_1.log'a'= logging. Formatter () file_2_1.setformatter (FMT)# defines the log Logger2 = logging. Logger ('s2', level=logging.info) Logger2.addhandler (file_2_1)
log (ii)
such as the two log objects created above
- When the log is written using "Logger1", the corresponding content is written to the L1_1.log and L1_2.log files
- When the log is written using "Logger2", the corresponding content is written to the L2_1.log file
Python's logging module