Use of the logging module in apache + python

Source: Internet
Author: User
Problem description: Use Logging:
#logging.conf[loggers]keys=root,example[handlers]keys=exampleHandler[formatters]keys=exampleFormatter[logger_example]level=DEBUGhandlers=exampleHandlerqualname=examplepropagate=0[logger_root]level=DEBUGhandlers=exampleHandlerqualname=rootpropagate=0[handler_exampleHandler]class=handlers.RotatingFileHandlerlevel=DEBUGformatter=exampleFormatterargs=('/tmp/example.log', 'a', 10*1024*1024, 5)[formatter_exampleFormatter]format=[%(asctime)s](%(levelname)s)%(name)s : %(message)sdatefmt=%a, %d %b %Y %H:%M:%S

There are two scripts: 1. test_log1.py

import loggingimport logging.configfrom mod_python import apachelogging.config.fileConfig("logger.conf");logger = logging.getLogger("root");def handler(req):logger.debug("this is a log in log_test1 file");return apache.OK;

2. test_log2.py

import loggingimport logging.configfrom mod_python import apachelogging.config.fileConfig("logger.conf");logger = logging.getLogger("root");def handler(req):logger.debug("this is a log in log_test2 file");return apache.OK;

Execution in the order of discovery: curl test_log1.py can write the log curl test_log1.py can write the log curl test_log2.py cannot write the log curl test_log1.py cannot write the log and restart apache service: curl test_log2.py can write the log curl test_log2.py can write the log curl test_log1.py cannot write the log curl test_log2.py cannot write the log solution logging has special processing for the root logger, it is best not to use it in the program, the preceding configuration file example is used in Python/logging/_ init __. PY has the following code:

1081     def handle(self, record):1082         """1083         Call the handlers for the specified record.1084 1085         This method is used for unpickled records received from a socket, as1086         well as those created locally. Logger-level filtering is applied.1087         """1088         if (not self.disabled) and self.filter(record):1089             self.callHandlers(record)

Note: This function needs to be called when writing logs. It determines the variable self. Disabled! When this variable is 1, logs cannot be written. The following code is available in Python/logging/config. py:

55 def fileConfig (fname, defaults = None ):...... 96 try: 97 try :...... 140 # at last, the loggers... first the root... 141 llist = cp. get ("loggers", "keys") 142 llist = string. split (llist, ",") 143 llist. remove ("root") # Pay attention to this line ...... 167 existing = root. manager. loggerDict. keys () 168 # now set up the new ones... 169 for log in llist: 170 sectname = "logger _ % s" % log171 qn = cp. get (sectname, "qualname") 172 opts = cp. options (sectname) 173 if "propagate" in opts: 174 propagate = cp. getint (sectname, "propagate") 175 else: 176 propagate = 1177 logger = logging. getLogger (qn) 178 if qn in existing: 179 existing. remove (qn) # Pay attention to this line ...... 186 logger. disabled = 0 # Pay attention to this line ...... 195 for log in existing: 196 root. manager. loggerDict [log]. disabled = 1 # Pay attention to this line

When the fileconfig function is executed, the root is deleted from lList. If root existing (loggerdict) exists, the above Code sets its root disabled to 1; however, for a common logger (such as the preceding example), otherwise, example will be removed from existing in row 179, therefore, the disabled of these common logger will not be set in Python/logging/_ init __. PY has the following code:

825 class Manager :...... 830 def _ init _ (self, rootnode ):...... 834 self. root = rootnode 835 self. disable = 0 836 self. emittedNoHandlerWarning = 0 837 self. loggerDict = {}# pay attention to this line 838 839 def getLogger (self, name ):...... 852 try: 853 if self. loggerDict. has_key (name ):...... 862 else: 863 rv = _ loggerClass (name) 864 rv. manager = self 865 self. loggerDict [name] = rv # Pay attention to this line

As you can see, for the first getLogger ('root') call, the root will be written to loggerDict. Therefore, the first getLogger ('root') Call can normally write logs, because the first loggerDict is {}, (row 837) is empty, so according to the above code analysis, the root disabled is not set to 1, can write logs but the second time, because the root has been put into loggerDict, the root disabled will be set to 1, and logs cannot be written. However, for the sake of python parsing acceleration, a log will be generated after the python file is parsed for the first time. pyc file, so that no second Parsing is performed (you can delete this file and try again, but when we request another file (test_log2.py), call the function again, root's disabled is set to 1, so there is a problem and logs cannot be written. Similarly, when test_log1.py is re-called, the log writing fails because the root disabled has been set to 1. This problem does not exist when example is used! Analysis is the same as above!

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.