| OLD | NEW |
| (Empty) |
| 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | |
| 2 # | |
| 3 # Redistribution and use in source and binary forms, with or without | |
| 4 # modification, are permitted provided that the following conditions | |
| 5 # are met: | |
| 6 # 1. Redistributions of source code must retain the above copyright | |
| 7 # notice, this list of conditions and the following disclaimer. | |
| 8 # 2. Redistributions in binary form must reproduce the above copyright | |
| 9 # notice, this list of conditions and the following disclaimer in the | |
| 10 # documentation and/or other materials provided with the distribution. | |
| 11 # | |
| 12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND | |
| 13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR | |
| 16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 22 | |
| 23 """Supports webkitpy logging.""" | |
| 24 | |
| 25 import logging | |
| 26 import sys | |
| 27 | |
| 28 _log = logging.getLogger(__name__) | |
| 29 | |
| 30 | |
| 31 def _default_handlers(stream, logging_level): | |
| 32 """Return a list of the default logging handlers to use. | |
| 33 | |
| 34 Args: | |
| 35 stream: See the configure_logging() docstring. | |
| 36 """ | |
| 37 # Create the filter. | |
| 38 def should_log(record): | |
| 39 """Return whether a logging.LogRecord should be logged.""" | |
| 40 if record.name.startswith("webkitpy.thirdparty"): | |
| 41 return False | |
| 42 return True | |
| 43 | |
| 44 logging_filter = logging.Filter() | |
| 45 logging_filter.filter = should_log | |
| 46 | |
| 47 # Create the handler. | |
| 48 handler = logging.StreamHandler(stream) | |
| 49 if logging_level == logging.DEBUG: | |
| 50 formatter = logging.Formatter("%(name)s: [%(levelname)s] %(message)s") | |
| 51 else: | |
| 52 formatter = logging.Formatter("%(message)s") | |
| 53 | |
| 54 handler.setFormatter(formatter) | |
| 55 handler.addFilter(logging_filter) | |
| 56 | |
| 57 return [handler] | |
| 58 | |
| 59 | |
| 60 def configure_logging(logging_level=None, logger=None, stream=None, | |
| 61 handlers=None): | |
| 62 """Configure logging for standard purposes. | |
| 63 | |
| 64 Returns: | |
| 65 A list of references to the logging handlers added to the root | |
| 66 logger. This allows the caller to later remove the handlers | |
| 67 using logger.removeHandler. This is useful primarily during unit | |
| 68 testing where the caller may want to configure logging temporarily | |
| 69 and then undo the configuring. | |
| 70 | |
| 71 Args: | |
| 72 logging_level: The minimum logging level to log. Defaults to | |
| 73 logging.INFO. | |
| 74 logger: A logging.logger instance to configure. This parameter | |
| 75 should be used only in unit tests. Defaults to the | |
| 76 root logger. | |
| 77 stream: A file-like object to which to log used in creating the default | |
| 78 handlers. The stream must define an "encoding" data attribute, | |
| 79 or else logging raises an error. Defaults to sys.stderr. | |
| 80 handlers: A list of logging.Handler instances to add to the logger | |
| 81 being configured. If this parameter is provided, then the | |
| 82 stream parameter is not used. | |
| 83 """ | |
| 84 # If the stream does not define an "encoding" data attribute, the | |
| 85 # logging module can throw an error like the following: | |
| 86 # | |
| 87 # Traceback (most recent call last): | |
| 88 # File "/System/Library/Frameworks/Python.framework/Versions/2.6/... | |
| 89 # lib/python2.6/logging/__init__.py", line 761, in emit | |
| 90 # self.stream.write(fs % msg.encode(self.stream.encoding)) | |
| 91 # LookupError: unknown encoding: unknown | |
| 92 if logging_level is None: | |
| 93 logging_level = logging.INFO | |
| 94 if logger is None: | |
| 95 logger = logging.getLogger() | |
| 96 if stream is None: | |
| 97 stream = sys.stderr | |
| 98 if handlers is None: | |
| 99 handlers = _default_handlers(stream, logging_level) | |
| 100 | |
| 101 logger.setLevel(logging_level) | |
| 102 | |
| 103 for handler in handlers: | |
| 104 logger.addHandler(handler) | |
| 105 | |
| 106 _log.debug("Debug logging enabled.") | |
| 107 | |
| 108 return handlers | |
| OLD | NEW |