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

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: Changing the right log handler. 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, no_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 no_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 no_time:
51 prefix = ''
52 else:
53 prefix = '%(asctime)s - '
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, no_time=False):
qyearsley 2017/06/21 16:42:26 It might be a bit clearer to rename this to avoid
mithro 2017/06/23 00:37:57 Done.
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.
qyearsley 2017/06/21 16:42:26 Remember to include the time argument here.
mithro 2017/06/23 00:37:57 Done.
83 """ 89 """
84 # If the stream does not define an "encoding" data attribute, the 90 # If the stream does not define an "encoding" data attribute, the
85 # logging module can throw an error like the following: 91 # logging module can throw an error like the following:
86 # 92 #
87 # Traceback (most recent call last): 93 # Traceback (most recent call last):
88 # File "/System/Library/Frameworks/Python.framework/Versions/2.6/... 94 # File "/System/Library/Frameworks/Python.framework/Versions/2.6/...
89 # lib/python2.6/logging/__init__.py", line 761, in emit 95 # lib/python2.6/logging/__init__.py", line 761, in emit
90 # self.stream.write(fs % msg.encode(self.stream.encoding)) 96 # self.stream.write(fs % msg.encode(self.stream.encoding))
91 # LookupError: unknown encoding: unknown 97 # LookupError: unknown encoding: unknown
92 if logging_level is None: 98 if logging_level is None:
93 logging_level = logging.INFO 99 logging_level = logging.INFO
94 if logger is None: 100 if logger is None:
95 logger = logging.getLogger() 101 logger = logging.getLogger()
96 if stream is None: 102 if stream is None:
97 stream = sys.stderr 103 stream = sys.stderr
98 if handlers is None: 104 if handlers is None:
99 handlers = _default_handlers(stream, logging_level) 105 handlers = _default_handlers(stream, logging_level, no_time)
100 106
101 logger.setLevel(logging_level) 107 logger.setLevel(logging_level)
102 108
103 for handler in handlers: 109 for handler in handlers:
104 logger.addHandler(handler) 110 logger.addHandler(handler)
105 111
106 _log.debug('Debug logging enabled.') 112 _log.debug('Debug logging enabled.')
107 113
108 return handlers 114 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