Belle II Software development
LogViewer Class Reference
Inheritance diagram for LogViewer:
IPythonWidget

Public Member Functions

def __init__ (self, log_content)
 
def format_logmessage (self, buf, message, indent=4, base=0)
 
def create (self)
 

Public Attributes

 log_content
 The log content to show.
 
 log_levels
 The log levels of the framework.
 
 log_color_codes
 The color codes for the log messages.
 
 log_message
 A templated line in the log.
 
 toggle_button_line
 The toggle button.
 

Detailed Description

A widget to show the log of a calculation.

Definition at line 295 of file viewer.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  log_content 
)
Initialize the log viewer.

Definition at line 300 of file viewer.py.

300 def __init__(self, log_content):
301 """
302 Initialize the log viewer.
303 """
304
305
306 self.log_content = log_content
307
308
309 self.log_levels = ["DEBUG", "INFO", "RESULT", "WARNING", "ERROR", "FATAL", "DEFAULT"]
310
311
312 self.log_color_codes = {"DEBUG": "gray", "ERROR": "red", "FATAL": "red", "INFO": "black", "RESULT": "green",
313 "WARNING": "orange", "DEFAULT": "black"}
314
315
316 self.log_message = """<pre class="log-line-{type_lower}" title="{info}">[{level}] {message}{var_output}</pre>"""
317
318
319 self.toggle_button_line = """<a onclick="$('.log-line-{type_lower}').hide();
320 $('.log-line-{type_lower}-hide-button').hide();
321 $('.log-line-{type_lower}-show-button').show();" style="cursor: pointer; margin: 0px 10px;"
322 class="log-line-{type_lower}-hide-button">Hide {type_upper}</a>
323 <a onclick="$('.log-line-{type_lower}').show();
324 $('.log-line-{type_lower}-hide-button').show();
325 $('.log-line-{type_lower}-show-button').hide();"
326 style="cursor: pointer; margin: 0px 10px; display: none;"
327 class="log-line-{type_lower}-show-button">Show {type_upper}</a>"""
328
329

Member Function Documentation

◆ create()

def create (   self)
Create the log viewer.

Reimplemented from IPythonWidget.

Definition at line 345 of file viewer.py.

345 def create(self):
346 """
347 Create the log viewer.
348 """
349 from ipywidgets import HTML, HBox, VBox
350
351 output = StringIO()
352 output.write("<style scoped>\n")
353 for level, color in self.log_color_codes.items():
354 level = level.lower()
355 output.write(f".log-line-{level} {{margin:0; padding:0; line-height:normal; color: {color} !important;}}\n")
356
357 output.write("""</style><div style="max-height: 400px; overflow-y: auto; width: 100%";>""")
358
359 for line in self.log_content.split("\n"):
360 if line.startswith('{"level"'):
361 try:
362 message = json.loads(line)
363 # ok, message is parsed. Prepare some info string which
364 # contains the info about the message in a indented
365 # format but don't completely json or pprint because it
366 # takes to much time
367 buf = StringIO()
368 self.format_logmessage(buf, message)
369 info = escape(buf.getvalue())
370 # add variables if necessary
371 variables = message.get("variables", "")
372 if variables:
373 variables = "\n".join([""] + [f"\t{k} = {v}" for k, v in variables.items()])
374 # and write out
375 level = message["level"].lower()
376 output.write(self.log_message.format(info=info, type_lower=level, var_output=variables, **message))
377 continue
378 except json.JSONDecodeError:
379 # any error: treat as default output, not a log line
380 pass
381
382 output.write('<pre class="log-line-default">')
383 output.write(line)
384 output.write('</pre>')
385
386 output.write("</div>")
387
388 html = HTML()
389 html.value = output.getvalue()
390 html.width = "100%"
391 html.margin = "5px"
392
393 buttons = []
394 for type in self.log_levels:
395 buttons.append(HTML(self.toggle_button_line.format(type_lower=type.lower(), type_upper=type.upper())))
396
397 buttons_view = HBox(buttons)
398 buttons_view.margin = "10px 0px"
399 result_vbox = VBox((buttons_view, html))
400
401 return result_vbox

◆ format_logmessage()

def format_logmessage (   self,
  buf,
  message,
  indent = 4,
  base = 0 
)
Format the json object of a logmessage as key: value list with recursion and indentation
Funnily this is faster than json.dumps() and looks a bit better in the title attribute

Definition at line 330 of file viewer.py.

330 def format_logmessage(self, buf, message, indent=4, base=0):
331 """
332 Format the json object of a logmessage as key: value list with recursion and indentation
333 Funnily this is faster than json.dumps() and looks a bit better in the title attribute
334 """
335 for key, val in message.items():
336 if not val:
337 continue
338 if isinstance(val, dict):
339 buf.write(f"{key}:\n")
340 self.format_logmessage(buf, val, indent=indent, base=base + indent)
341 else:
342 buf.write(base * " ")
343 buf.write(f"{key}: {val!r}\n")
344

Member Data Documentation

◆ log_color_codes

log_color_codes

The color codes for the log messages.

Definition at line 312 of file viewer.py.

◆ log_content

log_content

The log content to show.

Definition at line 306 of file viewer.py.

◆ log_levels

log_levels

The log levels of the framework.

Definition at line 309 of file viewer.py.

◆ log_message

log_message

A templated line in the log.

Definition at line 316 of file viewer.py.

◆ toggle_button_line

toggle_button_line

The toggle button.

Definition at line 319 of file viewer.py.


The documentation for this class was generated from the following file: