Import Logging ''' Log Level: critical> error> warning> info> Debug. The higher the notset level, the fewer logs are printed, and vice versa. That is, debug: prints all logs (notset is equivalent to debug) info: print info, warning, error, and critical-level logs warning: Print warning, error, and critical-level log error: print error, critical-level log critical: print the critical-level log ''' Handler = Logging. filehandler ( " // Tmp/TNLOG-error.log " ) Def Log (level): Logger =Logging. getlogger () # Handler cannot be created repeatedly. Otherwise, the same record will be written repeatedly? Logger. addhandler (handler) Logger. setlevel (level) Logger. debug ( " Debug " ) Logger.info ( " Info " ) Logger. Warning ( " Warning " ) Logger. Error ( " Error " ) Logger. Critical ( " Critical \ n " ) If _ Name __ = " _ Main __ " : Log (logging. notset) log (logging. Debug) log (logging. info) log (logging. Warning) log (logging. Error) log (logging. Critical)
InProgramIn the development phase, we obviously need a large number of logs, so the log level should be Debug. When the system is gradually stable, we need to reduce the number of logs to be recorded, in this way, the program execution efficiency can be improved (I don't think we should delete the debug-level log statements because they will be required again during maintenance? AlthoughCodeI think a good system should have such a statement.) At this time, we are more concerned with the user's actions in the system and what the user has done, if our program is a website that can buy things, we need to record all the information related to money, that is, users' modifications to core data must be recorded, at this time, we can select logs of info or above and the most important information. Obviously, we need to select the critical level. How to divide the logs depends on the system design, I don't have much experience to talk about it, but I suggest using the error level for the exception part (try-try t). Even if the system runs normally, unexpected errors cannot be guaranteed, once low-level logs are selected, the cause of important system exceptions may be missed.