Skip to content

Commit

Permalink
use a counter and an ordered list to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Jul 14, 2017
1 parent 19ad768 commit a314f90
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/pyramid_debugtoolbar/panels/__init__.py
Expand Up @@ -61,7 +61,7 @@ class DebugPanel(object):
nav_subtitle = ''

#: CSS class used to give the subtitle a background color.
nav_subtitle_bg_color = ''
nav_subtitle_style = ''

#: Title showing in panel. Must be overridden.
title = NotImplemented
Expand Down
55 changes: 21 additions & 34 deletions src/pyramid_debugtoolbar/panels/logger.py
@@ -1,3 +1,4 @@
from collections import Counter
import datetime
import logging
try:
Expand Down Expand Up @@ -68,45 +69,35 @@ def process_response(self, response):

@property
def has_content(self):
if self.data.get('records'):
if self.data['records']:
return True
else:
return False

@property
def log_level_summary(self):
def get_log_level_summary(self):
"""
returns number of times a logging level is present. Used to allow end
user to quickly see what types of log records are present.
"""
summary = dict([('CRITICAL',0),
('ERROR',0),
('WARNING',0),
('INFO',0),
('DEBUG',0),
('NOTSET',0)])
for r in self.data.get('records'):
if 'level' in r.keys() and r['level'] in summary.keys():
#ToDo: Use numeric level to catch custom logging levels.
summary[r['level']] +=1
summary = Counter()
for r in self.data['records']:
summary[r['level']] += 1
return summary

def get_highest_log_level(self):
if self.log_level_summary['CRITICAL'] > 0:
# showing total counts of critical and error since they are colored the same.
return ('CRITICAL', self.log_level_summary['CRITICAL'] + self.log_level_summary['ERROR'])
elif self.log_level_summary['ERROR'] > 0:
return ('ERROR', self.log_level_summary['ERROR'])
elif self.log_level_summary['WARNING'] > 0:
return ('WARNING', self.log_level_summary['WARNING'])
elif self.log_level_summary['INFO'] > 0:
return ('INFO', self.log_level_summary['INFO'])
elif self.log_level_summary['DEBUG'] > 0:
return ('DEBUG', self.log_level_summary['DEBUG'])
elif self.log_level_summary['NOTSET'] > 0:
return ('NOTSET', self.log_level_summary['NOTSET'])
else:
return (None, 0)
ORDER = ['ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET']
summary = self.get_log_level_summary()
if summary['CRITICAL'] > 0:
# showing total counts of critical and error since they are
# colored the same and both are important
return ('CRITICAL', summary['CRITICAL'] + summary['ERROR'])

# find the first matching level and return it
for level in ORDER:
count = summary[level]
if count > 0:
return (level, count)
return (None, 0)

def get_and_delete(self):
records = handler.get_records()
Expand All @@ -115,21 +106,17 @@ def get_and_delete(self):

@property
def nav_subtitle(self):
if self.data:
return '%d' % self.get_highest_log_level()[1]
return '%d' % self.get_highest_log_level()[1]

@property
def nav_subtitle_bg_color(self):
def nav_subtitle_style(self):
log_level = self.get_highest_log_level()[0]
if log_level in ('CRITICAL', 'ERROR'):
return 'progress-bar-danger'
elif log_level == 'WARNING':
return 'progress-bar-warning'
elif log_level == 'INFO':
return 'progress-bar-info'
else:
return ''



def includeme(config):
Expand Down
2 changes: 1 addition & 1 deletion src/pyramid_debugtoolbar/templates/history_tab.dbtmako
Expand Up @@ -32,7 +32,7 @@
% endif

% if panel.nav_subtitle and panel.has_content:
<span class="badge pull-right ${panel.nav_subtitle_bg_color}">${panel.nav_subtitle}</span>
<span class="badge pull-right ${panel.nav_subtitle_style}">${panel.nav_subtitle}</span>
% endif

${panel.nav_title}
Expand Down

0 comments on commit a314f90

Please sign in to comment.