One, log module: Logging
1 #简单应用2 Import Logging3Logging.debug ("Hello python")4Logging.info ("Hello World")5Logging.warning ("Hello Java") #日志级别默认WARNUNG only show warning, the above debug,info not print out6Logging.error ("Hello C")7Logging.critical ("Hello C + +")8 9 #打印结果:Ten #WARNING: Root:hello java One #ERROR: Root:hello C A#CRITICAL: Root:hello C + + -#
Log rank from bottom to top: critical-->error-->warning-->info-->debug
2, flexibility to set the log level, location, log format
1 Import Logging2 #日志等级设置成debug3Logging.basicconfig (level=logging. DEBUG, #日志等级设置成debug4format="% (asctime) s% (filename) s% (levelname) s% (message) s",5filename ="Test.txt", #设置文件名6FileMode ="W", #以w模式写入内容7 )8Logging.debug ("Debug Message")9Logging.info ("Info Message")TenLogging.warning ("warning Message") OneLogging.error ("error Message") ALogging.critical ("Critical Message")
2.1 The contents of the Test.txt file:
.-Geneva- - +: in: the,808logging module. PY Debug Debug Message .-Geneva- - +: in: the,825logging module. PY Info Info Message .-Geneva- - +: in: the,826logging module. PY WARNING WARNING Message .-Geneva- - +: in: the,826logging module. PY Error Error message .-Geneva- - +: in: the,826Logging module. PY CRITICAL CRITICAL Message
2.2 format string that may be used in format
1 " "2 3%(name) s Logger's name4%(Levelno) s log level in digital form5%(levelname) s log level in text form6%(pathname) s The full pathname of the module that invokes the log output function, possibly without7%(filename) s The file name of the module that invokes the log output function8%module Name of the log output function called by (module) s9%(funcName) s function name of the call log output functionTen%(Lineno) d The line of code where the statement that invokes the log output function One%(created) F current time, represented by the UNIX standard floating-point number representing the time A%(relativecreated) d when the log information is output, the number of milliseconds since logger was created -% (asctime) s The current time in string form. The default format is "2003- -- , -: the: $,896". The comma is followed by milliseconds -%(thread) d thread ID. Probably not . the%(threadname) s thread name. Probably not . -%(process) d ID. Probably not . -%(message) s user-output messages - " "
2.3 Logger Object method: Recommended usage
1 Import Logging2 #日志模块3 def get_logger ():4Logger =Logging.getlogger () #获得一个logger对象5 6FH = logging. Filehandler ("a.txt"# Create a file stream that needs to give a file parameter7 8SH =logging. Streamhandler () #创建一个屏幕流,9 Ten Logger.setlevel (logging. DEBUG) #设定最低等级debug One #写入文件的中的格式 AFM = logging. Formatter ("% (asctime) s-% (name) s-% (levelname) s-% (message) s") - - Logger.addhandler (FH) #把文件流添加进来, flow to file the Logger.addhandler (SH) #把屏幕流添加进来, flow to screen - - fh.setformatter (FM) #在文件流添加写入格式 - sh.setformatter (FM) #在屏幕流添加写入格式 + - returnLogger + ALogger =Get_logger () atLogger.debug ("Hello World") -Logger.info ("World python") -Logger.warning ("Hello Java") -Logger.error ("python C") -Logger.critical ("Hello C + +")
2.3 Content in A.txt files
1 #内容都是一样的, it's just that two schools are out there.2 3 #屏幕上的内容:4 .-Geneva- - +: $: -,336-Root-debug-Hello World5 .-Geneva- - +: $: -,337-Root-info-World python6 .-Geneva- - +: $: -,337-Root-warning-Hello Java7 .-Geneva- - +: $: -,337-Root-error-python C8 .-Geneva- - +: $: -,338-Root-critical-hello C + +9 Ten One #文件中的内容: A .-Geneva- - +: $: -,336-Root-debug-Hello World - .-Geneva- - +: $: -,337-Root-info-World python - .-Geneva- - +: $: -,337-Root-warning-Hello Java the .-Geneva- - +: $: -,337-Root-error-python C - .-Geneva- - +: $: -,338-Root-critical-hello C + +
Two: Serialization module: JSON important
Python basic------Module