Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: third_party/WebKit/Tools/Scripts/webkitpy/common/system/log_utils.py

Issue 2948043002: webkitpy: Add time output to the start of logging messages. (Closed)
Patch Set: git cl try Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/common/system/log_utils_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
2 # 2 #
3 # Redistribution and use in source and binary forms, with or without 3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions 4 # modification, are permitted provided that the following conditions
5 # are met: 5 # are met:
6 # 1. Redistributions of source code must retain the above copyright 6 # 1. Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer. 7 # notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright 8 # 2. Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the 9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution. 10 # documentation and/or other materials provided with the distribution.
(...skipping 10 matching lines...) Expand all
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 22
23 """Supports webkitpy logging.""" 23 """Supports webkitpy logging."""
24 24
25 import logging 25 import logging
26 import sys 26 import sys
27 27
28 _log = logging.getLogger(__name__) 28 _log = logging.getLogger(__name__)
29 29
30 30
31 def _default_handlers(stream, logging_level): 31 def _default_handlers(stream, logging_level, include_time):
32 """Return a list of the default logging handlers to use. 32 """Return a list of the default logging handlers to use.
33 33
34 Args: 34 Args:
35 stream: See the configure_logging() docstring. 35 stream: See the configure_logging() docstring.
36 include_time: See the configure_logging() docstring.
36 """ 37 """
37 # Create the filter. 38 # Create the filter.
38 def should_log(record): 39 def should_log(record):
39 """Return whether a logging.LogRecord should be logged.""" 40 """Return whether a logging.LogRecord should be logged."""
40 if record.name.startswith('webkitpy.thirdparty'): 41 if record.name.startswith('webkitpy.thirdparty'):
41 return False 42 return False
42 return True 43 return True
43 44
44 logging_filter = logging.Filter() 45 logging_filter = logging.Filter()
45 logging_filter.filter = should_log 46 logging_filter.filter = should_log
46 47
47 # Create the handler. 48 # Create the handler.
48 handler = logging.StreamHandler(stream) 49 handler = logging.StreamHandler(stream)
50 if include_time:
51 prefix = '%(asctime)s - '
52 else:
53 prefix = ''
54
49 if logging_level == logging.DEBUG: 55 if logging_level == logging.DEBUG:
50 formatter = logging.Formatter('%(name)s: [%(levelname)s] %(message)s') 56 formatter = logging.Formatter(prefix + '%(name)s: [%(levelname)s] %(mess age)s')
51 else: 57 else:
52 formatter = logging.Formatter('%(message)s') 58 formatter = logging.Formatter(prefix + '%(message)s')
53 59
54 handler.setFormatter(formatter) 60 handler.setFormatter(formatter)
55 handler.addFilter(logging_filter) 61 handler.addFilter(logging_filter)
56 62
57 return [handler] 63 return [handler]
58 64
59 65
60 def configure_logging(logging_level=None, logger=None, stream=None, 66 def configure_logging(logging_level=None, logger=None, stream=None,
61 handlers=None): 67 handlers=None, include_time=True):
62 """Configure logging for standard purposes. 68 """Configure logging for standard purposes.
63 69
64 Returns: 70 Returns:
65 A list of references to the logging handlers added to the root 71 A list of references to the logging handlers added to the root
66 logger. This allows the caller to later remove the handlers 72 logger. This allows the caller to later remove the handlers
67 using logger.removeHandler. This is useful primarily during unit 73 using logger.removeHandler. This is useful primarily during unit
68 testing where the caller may want to configure logging temporarily 74 testing where the caller may want to configure logging temporarily
69 and then undo the configuring. 75 and then undo the configuring.
70 76
71 Args: 77 Args:
72 logging_level: The minimum logging level to log. Defaults to 78 logging_level: The minimum logging level to log. Defaults to
73 logging.INFO. 79 logging.INFO.
74 logger: A logging.logger instance to configure. This parameter 80 logger: A logging.logger instance to configure. This parameter
75 should be used only in unit tests. Defaults to the 81 should be used only in unit tests. Defaults to the
76 root logger. 82 root logger.
77 stream: A file-like object to which to log used in creating the default 83 stream: A file-like object to which to log used in creating the default
78 handlers. The stream must define an "encoding" data attribute, 84 handlers. The stream must define an "encoding" data attribute,
79 or else logging raises an error. Defaults to sys.stderr. 85 or else logging raises an error. Defaults to sys.stderr.
80 handlers: A list of logging.Handler instances to add to the logger 86 handlers: A list of logging.Handler instances to add to the logger
81 being configured. If this parameter is provided, then the 87 being configured. If this parameter is provided, then the
82 stream parameter is not used. 88 stream parameter is not used.
89 include_time: Include time information at the start of every log message.
90 Useful for understanding how much time has passed between
91 subsequent log messages.
83 """ 92 """
84 # If the stream does not define an "encoding" data attribute, the 93 # If the stream does not define an "encoding" data attribute, the
85 # logging module can throw an error like the following: 94 # logging module can throw an error like the following:
86 # 95 #
87 # Traceback (most recent call last): 96 # Traceback (most recent call last):
88 # File "/System/Library/Frameworks/Python.framework/Versions/2.6/... 97 # File "/System/Library/Frameworks/Python.framework/Versions/2.6/...
89 # lib/python2.6/logging/__init__.py", line 761, in emit 98 # lib/python2.6/logging/__init__.py", line 761, in emit
90 # self.stream.write(fs % msg.encode(self.stream.encoding)) 99 # self.stream.write(fs % msg.encode(self.stream.encoding))
91 # LookupError: unknown encoding: unknown 100 # LookupError: unknown encoding: unknown
92 if logging_level is None: 101 if logging_level is None:
93 logging_level = logging.INFO 102 logging_level = logging.INFO
94 if logger is None: 103 if logger is None:
95 logger = logging.getLogger() 104 logger = logging.getLogger()
96 if stream is None: 105 if stream is None:
97 stream = sys.stderr 106 stream = sys.stderr
98 if handlers is None: 107 if handlers is None:
99 handlers = _default_handlers(stream, logging_level) 108 handlers = _default_handlers(stream, logging_level, include_time)
100 109
101 logger.setLevel(logging_level) 110 logger.setLevel(logging_level)
102 111
103 for handler in handlers: 112 for handler in handlers:
104 logger.addHandler(handler) 113 logger.addHandler(handler)
105 114
106 _log.debug('Debug logging enabled.') 115 _log.debug('Debug logging enabled.')
107 116
108 return handlers 117 return handlers
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/common/system/log_utils_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698