1, log Level 5:
Warning warning General Information info Debug Debug Error fatal Critical
2. Disable the Logging method
Logging.disable (logging. DEBUG)
3. Write log to File
Logging.basicconfig (filename= ' Log.txt ', level=logging. CRITICAL, format= '% (asctime) s-% (levelname) s-% (message) s ')
4. Formatted output log information
Precautions:
1, log output file, the log configuration involves writing to the log file should be placed in the first line of the log configuration, or it will be affected by the previous log configuration interference.
#!/usr/bin/env python# coding=utf-8import loggingimport oslogging.basicconfig (filename= ' Log.txt ', level=logging. CRITICAL, format= '% (asctime) s-% (levelname) s-% (message) s ') Logging.basicconfig (level=logging. Debug, format= '% (asctime) s-% (levelname) s-% (message) s ') # disables debug information for the Log module # logging.disable (logging. DEBUG) Logging.debug (' Start of Program ') # to determine if the log file exists if os.path.exists (' Log.txt '): print "has found the Log.txt " logging.critical (" good ") Else: logging.critical ("/home/log.txt is not existed!!! ") def factorial (n): logging.debug (' Start of factorial (%s%%) '% (n)) total = 1 for I in range (1, n + 1): tot Al *= I logging.debug (' I am ' + str (i) + ', total was ' + str (total)) Logging.debug (' End of factorial (%s%%) '% (n ) return Totalprint (factorial (5)) Logging.debug (' End of Program ')
The log module in Python logging