A simple example of the Python (2.7.6) Standard Log Module, python2.7.6
The logging module in the Python Standard Library provides a set of standard APIs to process log information printing.
import logginglogging.basicConfig( level = logging.DEBUG, format = '%(asctime)s [%(threadName)s] (%(filename)s:%(lineno)d) %(levelname)s - %(message)s', datefmt = '%Y-%m-%d %H:%M:%S', filename = 'myapp.log',)logging.debug('This is a debug message')logging.info('This is an info message')logging.warning('This is a warning message')
The output log in myapp. log is as follows:
2015-03-11 15:54:34 [MainThread] (logging_demo.py:10) DEBUG - This is a debug message2015-03-11 15:54:34 [MainThread] (logging_demo.py:11) INFO - This is an info message2015-03-11 15:54:34 [MainThread] (logging_demo.py:12) WARNING - This is a warning message
Parameter descriptions of the logging. basicConfig function:
| Parameters |
Description |
| Filename |
File Name of the FileHandler instance created by the user |
| Filemode |
Log File opening mode. The default value is 'A' |
| Format |
Log output format |
| Datefmt |
Time Output Format |
| Level |
Log level. The size relationship is CRITICAL (50)> ERROR (40)> WARNING (30)> INFO (20)> DEBUG (10)> NOTSET (0) |
| Stream |
Used to initialize the stream of StreamHandler. this parameter is specified with filename and will be ignored. |
Format symbol in logging:
| Symbol |
Description |
| % (Asctime) s |
Time |
| % (Filename) s |
File Name |
| % (FuncName) s |
Function Name |
| % (Levelname) s |
Log Level Value |
| % (Levelno) s |
Log Level |
| % (Lineno) d |
Row number |
| % (Module) s |
Module |
| % (Message) s |
Log message |
| % (Name) s |
Log name |
| % (Pathname) s |
Logger name |
| % (Process) d |
Process ID |
| % (ProcessName) s |
Process name |
| % (Thread) d |
Thread ID |
| % (ThreadName) s |
Thread name |