|
Regardless of the log for the development or operation and maintenance is a very important thing, it can be used for debugging, troubleshooting and statistical analysis.
This article describes the use of python log library.
Log database: import logging
Use logs need to define the following things:
1. Obtain log name, such as
logging.getLogger (__ name__)
2. Define Handler, such as
logging.FileHandler ( '/ var / log / messages')
3. Set the level, such as
fh.setLevel (logging.DEBUG)
4. Define the format, such as
formatter = logging.Formatter ( '% (asctime) s -% (name) s -% (levelname) s -% (message) s')
5. Print logs, such as
LOG.error ( "python logging test !!")
Here are a script is used to output logs to / var / log / messages and terminals.
import logging
LOG = logging.getLogger (__ name__)
formatter = logging.Formatter ( '% (asctime) s -% (name) s -% (levelname) s -% (message) s')
fh = logging.FileHandler ( '/ var / log / messages')
fh.setLevel (logging.DEBUG)
fh.setFormatter (formatter)
LOG.addHandler (fh)
ch = logging.StreamHandler ()
ch.setLevel (logging.ERROR)
ch.setFormatter (formatter)
LOG.addHandler (ch)
LOG.error ( "python logging test !!")
Run, you'll find the / var / log / messages and the screen will have a similar "2015-06-28 07: 41: 41,527 - test - ERROR - python logging test !!".
This is the basic usage log, the future will introduce the use of more complex points. |
|
|
|