Python's logging module is used to write logs and is a standard Python module.Structure of the logging
View logging python source code, there are four main classes to realize the function;
Loggers: Provides interfaces directly used by the application,
First introduced how to find it, the online project log is through the logging module to the Syslog, ran a period of time after the discovery of the syslog UDP connection over 8W, yes is 8 W. The main thing is that the logging module is used incorrectly.
We had such a requirement, that is, for each connection log output the current connection information, so each connection creates a log instance, and assi
This article mainly introduces the usage examples of the logging module in Python, and introduces the usage of the logging module in the form of instances, which has some practical value, for more information about how to use the logging module, see the examples in this article. The specific method is as follows:
Tags: style blog class code java cFirst, PDB useThe PDB is a Python-brought package that provides an interactive source code debugging feature for Python programs, including setting breakpoints, stepping through debugging, entering function debugging, viewing current code, viewing stack fragments, dynamically changing the values of variables, and more.Insert a program in the middle of the program (import PD
Python Logging Duplicate Write log issuesWhen logging with Python's logging module, I encountered the problem of duplicate logging, the first record was written once, the second record was written two times, and the third record was written three times. It's a headache, but
Many programmers have the need to log, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python logging module provides a standard log interface, you can store a variety of logs through it, you can store various forms of log , the logging log can be divided into five levels: Debug (),
"example.py", line 6, in
open ( '/path/to/does/not/exist ', ' RB ') IOError: [Errno 2] No such file or directory: '/path/to/does/not/exist '
Python uses the logging module to log logs involving four main classes, which are best used in the official documentation:
Logger provides an interface that the application can use directly;
Handler sends the log record (created by logger) to the
:
Logging. Streamhandler # Use this handler to output information (output to the screen) to any file object similar to Sys.stdout or Sys.stderr
Logging. Filehandler, like Streamhandler, is used to output log information to a file, but Filehandler will open the file for you (output to the file)
There are two other uses: one is truncated according to the size of the file, and the other is tru
of some problem in the "near" (e.g. ' disk space low ') . The software is still working as expected.
ERROR
Due to a more serious problem, the software have not been able to perform some function.
CRITICAL
A serious error, indicating the program itself is unable to continue running.
With the Basicconfig method, you can configure the log records.In [1]: Import loggingIn [2]: Logging.basicconfig (filename= '/tmp/example.log ', level=
I think it's pretty good. I 've talked about all the basic skills.
The following describes how to use it.CodeIt is easy to write:
1 def initlog ():2 Import Logging34 logger = logging. getlogger ()5 hdlr = logging. filehandler (logfile)6 formatter = logging. formatter ('% (asctime) S % (levelname) S % (Message) s ')
Click on the link to join QQ Group 522720170 (free public classes, videos have everything):https://jq.qq.com/?_wv=1027k=5C08ATePhenomenon
The students of the test class of the full stack of jack-up Python ask that there will be repeated write logs, as followsReason
When you call log for the second time, you get the same logger based on the name of GetLogger, and the logger already has the first
Click on the link to join QQ Group 522720170 (free public lessons, video everything): Https://jq.qq.com/?_wv=1027k=5C08ATePhenomenonThe students of the test class of the full stack of jack-up Python ask that there will be repeated write logs, as followsReasonWhen you call log for the second time, you get the same logger based on the name of GetLogger, and the logger already has the first handler you added,
Logging module for writing log filesThere are 5 levels, debug (), info (), warning (), error (), and Critical (), the highest level is critical ()Debug () debug mode, info () is the normal information, warning () is warning, error () is wrong, critical () is a serious issueNormal printing#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.debug (' the Debug ') Logging.info (' the info ') Logging.warning (' Request outtime ') logging.e
as follows:
$ python logging_level_example.py Debug
DEBUG:root:This is a debug message
INFO:root:This is a info message
WARNING:root:This is a WARNING message
ERROR:root:This is a error message
CRITICAL:root:This is a CRITICAL error message
$ python logging_level_example.py InfoINFO:root:This is a info messageWARNING:root:This is a WARNING messageERROR:root:This is a error messageC
, if the parent logger level is not set, continue to find the parent's parent, finally found root, if the root set to use it, if root is not set, the default value of root is warning3. Generate some level of information on a logger, first and logger levels, if the message level is lower than Logger's EFFECTIVELEVL, the message is discarded and the message is no longer passed to the parent logger. If the pass (greater than equals) is checked, the message is given to logger all handler processing,
Log module logging Logging module can be based on custom log information, when the program is running, the log is printed in the terminal and logging to the file. Here's a look at the logs supported by logging five levelsDebug () debugging level, typically used to log details of program runsInfo () event level, typical
directly in this module--production environments are encapsulated into function callsMylogger.info(' Foorbar ')Mylogger.debug (' Just a test ')$ python demo.py2015-10-30 15:44:01,722-mylogger-test1.py-info-foorbar2015-10-30 15:44:01,723-mylogger-test1.py-debug-just a testhttp://www.php101.cn/2015/03/05/Python%E4%B8%AD%E7%9A%84Logging%E7%AE%80%E4%BB%8B/Wonderful examplesThree: API for
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.