Mercurial > libervia-backend
comparison libervia/backend/core/log_config.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 15055a00162c |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
29 force_colors = False | 29 force_colors = False |
30 | 30 |
31 def __init__(self, *args, **kwargs): | 31 def __init__(self, *args, **kwargs): |
32 super().__init__(*args, **kwargs) | 32 super().__init__(*args, **kwargs) |
33 from twisted.logger import Logger | 33 from twisted.logger import Logger |
34 | |
34 self.twisted_log = Logger() | 35 self.twisted_log = Logger() |
35 | 36 |
36 def out(self, message, level=None, **kwargs): | 37 def out(self, message, level=None, **kwargs): |
37 """Actually log the message | 38 """Actually log the message |
38 | 39 |
57 try: | 58 try: |
58 isatty = sys.stdout.isatty() | 59 isatty = sys.stdout.isatty() |
59 except AttributeError: | 60 except AttributeError: |
60 isatty = False | 61 isatty = False |
61 # FIXME: isatty should be tested on each handler, not globaly | 62 # FIXME: isatty should be tested on each handler, not globaly |
62 if (force_colors or isatty): | 63 if force_colors or isatty: |
63 # we need colors | 64 # we need colors |
64 log.Logger.post_treat = lambda logger, level, message: self.ansi_colors( | 65 log.Logger.post_treat = lambda logger, level, message: self.ansi_colors( |
65 level, message | 66 level, message |
66 ) | 67 ) |
67 elif force_colors: | 68 elif force_colors: |
107 class ConfigureTwisted(ConfigureBasic): | 108 class ConfigureTwisted(ConfigureBasic): |
108 LOGGER_CLASS = TwistedLogger | 109 LOGGER_CLASS = TwistedLogger |
109 | 110 |
110 def pre_treatment(self): | 111 def pre_treatment(self): |
111 from twisted import logger | 112 from twisted import logger |
113 | |
112 global logger | 114 global logger |
113 self.level_map = { | 115 self.level_map = { |
114 C.LOG_LVL_DEBUG: logger.LogLevel.debug, | 116 C.LOG_LVL_DEBUG: logger.LogLevel.debug, |
115 C.LOG_LVL_INFO: logger.LogLevel.info, | 117 C.LOG_LVL_INFO: logger.LogLevel.info, |
116 C.LOG_LVL_WARNING: logger.LogLevel.warn, | 118 C.LOG_LVL_WARNING: logger.LogLevel.warn, |
123 self.level = self.level_map[level] | 125 self.level = self.level_map[level] |
124 | 126 |
125 def configure_output(self, output): | 127 def configure_output(self, output): |
126 import sys | 128 import sys |
127 from twisted.python import logfile | 129 from twisted.python import logfile |
130 | |
128 self.log_publisher = logger.LogPublisher() | 131 self.log_publisher = logger.LogPublisher() |
129 | 132 |
130 if output is None: | 133 if output is None: |
131 output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT | 134 output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT |
132 self.manage_outputs(output) | 135 self.manage_outputs(output) |
135 if self.backend_data is None: | 138 if self.backend_data is None: |
136 raise ValueError( | 139 raise ValueError( |
137 "You must pass options as backend_data with Twisted backend" | 140 "You must pass options as backend_data with Twisted backend" |
138 ) | 141 ) |
139 options = self.backend_data | 142 options = self.backend_data |
140 log_file = logfile.LogFile.fromFullPath(options['logfile']) | 143 log_file = logfile.LogFile.fromFullPath(options["logfile"]) |
141 self.log_publisher.addObserver( | 144 self.log_publisher.addObserver( |
142 logger.FileLogObserver(log_file, self.text_formatter)) | 145 logger.FileLogObserver(log_file, self.text_formatter) |
146 ) | |
143 # we also want output to stdout if we are in debug or nodaemon mode | 147 # we also want output to stdout if we are in debug or nodaemon mode |
144 if options.get("nodaemon", False) or options.get("debug", False): | 148 if options.get("nodaemon", False) or options.get("debug", False): |
145 self.log_publisher.addObserver( | 149 self.log_publisher.addObserver( |
146 logger.FileLogObserver(sys.stdout, self.text_formatter)) | 150 logger.FileLogObserver(sys.stdout, self.text_formatter) |
151 ) | |
147 | 152 |
148 if C.LOG_OPT_OUTPUT_FILE in log.handlers: | 153 if C.LOG_OPT_OUTPUT_FILE in log.handlers: |
149 | 154 |
150 for path in log.handlers[C.LOG_OPT_OUTPUT_FILE]: | 155 for path in log.handlers[C.LOG_OPT_OUTPUT_FILE]: |
151 log_file = ( | 156 log_file = ( |
152 sys.stdout if path == "-" else logfile.LogFile.fromFullPath(path) | 157 sys.stdout if path == "-" else logfile.LogFile.fromFullPath(path) |
153 ) | 158 ) |
154 self.log_publisher.addObserver( | 159 self.log_publisher.addObserver( |
155 logger.FileLogObserver(log_file, self.text_formatter)) | 160 logger.FileLogObserver(log_file, self.text_formatter) |
161 ) | |
156 | 162 |
157 if C.LOG_OPT_OUTPUT_MEMORY in log.handlers: | 163 if C.LOG_OPT_OUTPUT_MEMORY in log.handlers: |
158 raise NotImplementedError( | 164 raise NotImplementedError( |
159 "Memory observer is not implemented in Twisted backend" | 165 "Memory observer is not implemented in Twisted backend" |
160 ) | 166 ) |
170 | 176 |
171 def post_treatment(self): | 177 def post_treatment(self): |
172 """Install twistedObserver which manage non SàT logs""" | 178 """Install twistedObserver which manage non SàT logs""" |
173 # from twisted import logger | 179 # from twisted import logger |
174 import sys | 180 import sys |
181 | |
175 filtering_obs = logger.FilteringLogObserver( | 182 filtering_obs = logger.FilteringLogObserver( |
176 observer=self.log_publisher, | 183 observer=self.log_publisher, |
177 predicates=[ | 184 predicates=[ |
178 logger.LogLevelFilterPredicate(self.level), | 185 logger.LogLevelFilterPredicate(self.level), |
179 ] | 186 ], |
180 ) | 187 ) |
181 logger.globalLogBeginner.beginLoggingTo([filtering_obs]) | 188 logger.globalLogBeginner.beginLoggingTo([filtering_obs]) |
182 | 189 |
183 def text_formatter(self, event): | 190 def text_formatter(self, event): |
184 if event.get('sat_logged', False): | 191 if event.get("sat_logged", False): |
185 timestamp = ''.join([logger.formatTime(event.get("log_time", None)), " "]) | 192 timestamp = "".join([logger.formatTime(event.get("log_time", None)), " "]) |
186 return f"{timestamp}{event.get('log_format', '')}\n" | 193 return f"{timestamp}{event.get('log_format', '')}\n" |
187 else: | 194 else: |
188 eventText = logger.eventAsText( | 195 eventText = logger.eventAsText(event, includeSystem=True) |
189 event, includeSystem=True) | |
190 if not eventText: | 196 if not eventText: |
191 return None | 197 return None |
192 return eventText.replace("\n", "\n\t") + "\n" | 198 return eventText.replace("\n", "\n\t") + "\n" |
193 | 199 |
194 | 200 |
239 super(ConfigureStandard, self).configure_format(fmt) | 245 super(ConfigureStandard, self).configure_format(fmt) |
240 import logging | 246 import logging |
241 | 247 |
242 class SatFormatter(logging.Formatter): | 248 class SatFormatter(logging.Formatter): |
243 """Formatter which manage SàT specificities""" | 249 """Formatter which manage SàT specificities""" |
250 | |
244 _format = fmt | 251 _format = fmt |
245 _with_profile = "%(profile)s" in fmt | 252 _with_profile = "%(profile)s" in fmt |
246 | 253 |
247 def __init__(self, can_colors=False): | 254 def __init__(self, can_colors=False): |
248 super(SatFormatter, self).__init__(self._format) | 255 super(SatFormatter, self).__init__(self._format) |
308 class SatMemoryHandler(BufferingHandler): | 315 class SatMemoryHandler(BufferingHandler): |
309 def emit(self, record): | 316 def emit(self, record): |
310 super(SatMemoryHandler, self).emit(self.format(record)) | 317 super(SatMemoryHandler, self).emit(self.format(record)) |
311 | 318 |
312 hdlr = SatMemoryHandler(options) | 319 hdlr = SatMemoryHandler(options) |
313 log.handlers[ | 320 log.handlers[handler] = ( |
314 handler | 321 hdlr # we keep a reference to the handler to read the buffer later |
315 ] = ( | 322 ) |
316 hdlr | |
317 ) # we keep a reference to the handler to read the buffer later | |
318 self._add_handler(root_logger, hdlr, can_colors=False) | 323 self._add_handler(root_logger, hdlr, can_colors=False) |
319 elif handler == C.LOG_OPT_OUTPUT_FILE: | 324 elif handler == C.LOG_OPT_OUTPUT_FILE: |
320 import os.path | 325 import os.path |
321 | 326 |
322 for path in options: | 327 for path in options: |