1 ImportLogging2 3 4 #Simple Print Log Example5Logging.basicconfig (level=logging. DEBUG)#set the log level, WARN6Logging.warning ('Watch out!')#Would print a message to the console7Logging.info ('I told')#Would not print anything8 9 Ten #Print the log to a file, note that you want to start a new file, or you cannot save the file One defLog_to_file (logs_dir="D:\\test_data\\logs\\log_debug.txt"): ALogging.basicconfig (Filename=logs_dir, level=logging. DEBUG) -Logging.debug ('This message should go to the log file') -Logging.info ('So should this') theLogging.warning ('and this, too') - - - Log_to_file () + - #Multi-parameter logging +Logging.warning ('%s before you%s',' Look','leap!') A at #print time in log -Logging.basicconfig (format='% (asctime) s% (message) s') -Logging.warning ('is while this event was logged.') - - #Specify a time format -Logging.basicconfig (format='% (asctime) s% (message) s', datefmt='%y-%m-%d%i:%m:%s%p') inLogging.warning ('is while this event was logged.')
Note: The logging parameter settings are only valid until the first run, and can only be set once, and subsequent settings are not valid.
Specify the time format to run the result:
1 # Specify a time format 2 is while this event was logged.
Python's Print log logging