##// END OF EJS Templates
add LevelFormatter for extra formatting on high level messages...
MinRK -
Show More
@@ -95,6 +95,26 b' def catch_config_error(method, app, *args, **kwargs):'
95 class ApplicationError(Exception):
95 class ApplicationError(Exception):
96 pass
96 pass
97
97
98 class LevelFormatter(logging.Formatter):
99 """Formatter with additional `highlevel` record
100
101 This field is empty if log level is less than highlevel_limit,
102 otherwise it is formatted with self.highlevel_format.
103
104 Useful for adding 'WARNING' to warning messages,
105 without adding 'INFO' to info, etc.
106 """
107 highlevel_limit = logging.WARN
108 highlevel_format = " - %(levelname)s - "
109
110 def format(self, record):
111 if record.levelno >= self.highlevel_limit:
112 record.highlevel = self.highlevel_format % record.__dict__
113 else:
114 record.highlevel = ""
115
116 return super(LevelFormatter, self).format(record)
117
98
118
99 class Application(SingletonConfigurable):
119 class Application(SingletonConfigurable):
100 """A singleton application with full configuration support."""
120 """A singleton application with full configuration support."""
@@ -133,13 +153,19 b' class Application(SingletonConfigurable):'
133 self.log_level = new
153 self.log_level = new
134 self.log.setLevel(new)
154 self.log.setLevel(new)
135
155
136 log_format = Unicode("[%(name)s] %(message)s", config=True,
156 log_datefmt = Unicode("%Y-%m-%d %H:%M:%S", config=True,
157 help="The date format used by logging formatters for %(asctime)s"
158 )
159 def _log_datefmt_changed(self, name, old, new):
160 self._log_format_changed()
161
162 log_format = Unicode("[%(name)s]%(highlevel)s %(message)s", config=True,
137 help="The Logging format template",
163 help="The Logging format template",
138 )
164 )
139 def _log_format_changed(self, name, old, new):
165 def _log_format_changed(self, name, old, new):
140 """Change the log formatter when log_format is set."""
166 """Change the log formatter when log_format is set."""
141 _log_handler = self.log.handlers[0]
167 _log_handler = self.log.handlers[0]
142 _log_formatter = logging.Formatter(new)
168 _log_formatter = LevelFormatter(new, datefmt=self._log_datefmt)
143 _log_handler.setFormatter(_log_formatter)
169 _log_handler.setFormatter(_log_formatter)
144
170
145 log = Instance(logging.Logger)
171 log = Instance(logging.Logger)
@@ -167,7 +193,7 b' class Application(SingletonConfigurable):'
167 _log_handler = logging.StreamHandler(open(os.devnull, 'w'))
193 _log_handler = logging.StreamHandler(open(os.devnull, 'w'))
168 else:
194 else:
169 _log_handler = logging.StreamHandler()
195 _log_handler = logging.StreamHandler()
170 _log_formatter = logging.Formatter(self.log_format)
196 _log_formatter = LevelFormatter(self.log_format, datefmt=self.log_datefmt)
171 _log_handler.setFormatter(_log_formatter)
197 _log_handler.setFormatter(_log_formatter)
172 log.addHandler(_log_handler)
198 log.addHandler(_log_handler)
173 return log
199 return log
General Comments 0
You need to be logged in to leave comments. Login now