The logging module in the Python Standard library provides a standard set of APIs to handle the printing of log information.
ImportLogginglogging.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 logs printed in Myapp.log are 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] (log GING_DEMO.PY:11) Info-this is an INFO message2015-03-11 15:54:34 [Mainthread] (Logging_demo.py:12) Warning-this is a W Arning message
The parameter description of the Logging.basicconfig function:
Parameters |
Description |
FileName |
Log file name |
FileMode |
Open mode of log file, default to ' a ' |
Format |
Output format of the log |
Datefmt |
The output format of the time |
Level |
Log level, size relationship CRITICAL (0) > ERROR (+) > WARNING (+) > INFO > DEBUG (Ten) > NOTSET (+) |
Stream |
The output stream of the log print, which is ignored when specified with filename |
Formatting symbols in Logging:
Symbol |
Description |
% (Asctime) s |
Time |
% (filename) s |
Filename |
% (FuncName) s |
Name of function |
% (LevelName) s |
Log level values |
% (Levelno) s |
Log level |
% (Lineno) d |
Line number |
% (module) s |
Module |
% (message) s |
Log Messages |
% (name) s |
Journal name |
% (pathname) s |
Name of the Logger |
% (process) d |
Process ID |
% (ProcessName) s |
Process Name |
% (thread) d |
Thread ID |
% (ThreadName) s |
Thread Name |
A simple example of Python (2.7.6) Log processing