From b637a63e7140573d1c988d32ee1aba23258478f1 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sat, 12 Dec 2020 08:11:00 +0100 Subject: [PATCH] Refactored exception logging --- cps/db.py | 2 +- cps/editbooks.py | 7 ++----- cps/gdrive.py | 5 +---- cps/gdriveutils.py | 5 +---- cps/helper.py | 7 ++----- cps/logger.py | 12 ++++++++++-- cps/oauth_bb.py | 20 ++++---------------- cps/services/worker.py | 4 +--- cps/tasks/mail.py | 6 +++--- cps/web.py | 10 ++-------- 10 files changed, 27 insertions(+), 51 deletions(-) diff --git a/cps/db.py b/cps/db.py index bc564638..2e428f72 100644 --- a/cps/db.py +++ b/cps/db.py @@ -765,5 +765,5 @@ def lcase(s): return unidecode.unidecode(s.lower()) except Exception as e: log = logger.create() - log.exception(e) + log.debug_or_exception(e) return s.lower() diff --git a/cps/editbooks.py b/cps/editbooks.py index b8f025df..e62986c8 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -259,10 +259,7 @@ def delete_book(book_id, book_format, jsonResponse): filter(db.Data.format == book_format).delete() calibre_db.session.commit() except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) calibre_db.session.rollback() else: # book not found @@ -719,7 +716,7 @@ def edit_book(book_id): flash(error, category="error") return render_edit_book(book_id) except Exception as e: - log.exception(e) + log.debug_or_exception(e) calibre_db.session.rollback() flash(_("Error editing book, please check logfile for details"), category="error") return redirect(url_for('web.show_book', book_id=book.id)) diff --git a/cps/gdrive.py b/cps/gdrive.py index b8dc1d80..9ba87260 100644 --- a/cps/gdrive.py +++ b/cps/gdrive.py @@ -156,8 +156,5 @@ def on_received_watch_confirmation(): move(os.path.join(tmp_dir, "tmp_metadata.db"), dbpath) calibre_db.reconnect_db(config, ub.app_DB_path) except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) return '' diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index e1731aab..6f78ab47 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -199,10 +199,7 @@ def getDrive(drive=None, gauth=None): except RefreshError as e: log.error("Google Drive error: %s", e) except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) else: # Initialize the saved creds gauth.Authorize() diff --git a/cps/helper.py b/cps/helper.py index aa59cadb..da5ea2b3 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -562,10 +562,7 @@ def get_book_cover_internal(book, use_generic_cover_on_failure): log.error('%s/cover.jpg not found on Google Drive', book.path) return get_cover_on_failure(use_generic_cover_on_failure) except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) return get_cover_on_failure(use_generic_cover_on_failure) else: cover_file_path = os.path.join(config.config_calibre_dir, book.path) @@ -697,7 +694,7 @@ def check_unrar(unrarLocation): log.debug("unrar version %s", version) break except (OSError, UnicodeDecodeError) as err: - log.exception(err) + log.debug_or_exception(err) return _('Error excecuting UnRar') diff --git a/cps/logger.py b/cps/logger.py index 7cc0f4d9..a9606cb1 100644 --- a/cps/logger.py +++ b/cps/logger.py @@ -41,10 +41,18 @@ logging.addLevelName(logging.WARNING, "WARN") logging.addLevelName(logging.CRITICAL, "CRIT") +class _Logger(logging.Logger): + + def debug_or_exception(self, message, *args, **kwargs): + if is_debug_enabled(): + self.exception(message, stacklevel=2, *args, **kwargs) + else: + self.error(message, stacklevel=2, *args, **kwargs) + + def get(name=None): return logging.getLogger(name) - def create(): parent_frame = inspect.stack(0)[1] if hasattr(parent_frame, 'frame'): @@ -54,7 +62,6 @@ def create(): parent_module = inspect.getmodule(parent_frame) return get(parent_module.__name__) - def is_debug_enabled(): return logging.root.level <= logging.DEBUG @@ -99,6 +106,7 @@ def setup(log_file, log_level=None): May be called multiple times. ''' log_level = log_level or DEFAULT_LOG_LEVEL + logging.setLoggerClass(_Logger) logging.getLogger(__package__).setLevel(log_level) r = logging.root diff --git a/cps/oauth_bb.py b/cps/oauth_bb.py index 97fe8f4c..ba09678d 100644 --- a/cps/oauth_bb.py +++ b/cps/oauth_bb.py @@ -88,10 +88,7 @@ def register_user_with_oauth(user=None): try: ub.session.commit() except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) ub.session.rollback() @@ -210,10 +207,7 @@ if ub.oauth_support: ub.session.add(oauth_entry) ub.session.commit() except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) ub.session.rollback() # Disable Flask-Dance's default behavior for saving the OAuth token @@ -245,10 +239,7 @@ if ub.oauth_support: flash(_(u"Link to %(oauth)s Succeeded", oauth=provider_name), category="success") return redirect(url_for('web.profile')) except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) ub.session.rollback() else: flash(_(u"Login failed, No User Linked With OAuth Account"), category="error") @@ -295,10 +286,7 @@ if ub.oauth_support: logout_oauth_user() flash(_(u"Unlink to %(oauth)s Succeeded", oauth=oauth_check[provider]), category="success") except Exception as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) ub.session.rollback() flash(_(u"Unlink to %(oauth)s Failed", oauth=oauth_check[provider]), category="error") except NoResultFound: diff --git a/cps/services/worker.py b/cps/services/worker.py index d3b7b14f..072674a0 100644 --- a/cps/services/worker.py +++ b/cps/services/worker.py @@ -161,8 +161,7 @@ class CalibreTask: self.run(*args) except Exception as e: self._handleError(str(e)) - if logger.is_debug_enabled(): - log.exception(e) + log.debug_or_exception(e) self.end_time = datetime.now() @@ -211,7 +210,6 @@ class CalibreTask: self._progress = x def _handleError(self, error_message): - log.error(error_message) self.stat = STAT_FAIL self.progress = 1 self.error = error_message diff --git a/cps/tasks/mail.py b/cps/tasks/mail.py index ac3ec424..8424f084 100644 --- a/cps/tasks/mail.py +++ b/cps/tasks/mail.py @@ -167,7 +167,7 @@ class TaskEmail(CalibreTask): smtplib.stderr = org_smtpstderr except (MemoryError) as e: - log.exception(e) + log.debug_or_exception(e) self._handleError(u'MemoryError sending email: ' + str(e)) # return None except (smtplib.SMTPException, smtplib.SMTPAuthenticationError) as e: @@ -178,7 +178,7 @@ class TaskEmail(CalibreTask): elif hasattr(e, "args"): text = '\n'.join(e.args) else: - log.exception(e) + log.debug_or_exception(e) text = '' self._handleError(u'Smtplib Error sending email: ' + text) # return None @@ -225,7 +225,7 @@ class TaskEmail(CalibreTask): data = file_.read() file_.close() except IOError as e: - log.exception(e) + log.debug_or_exception(e) log.error(u'The requested file could not be read. Maybe wrong permissions?') return None diff --git a/cps/web.py b/cps/web.py index 03d10556..9715a90e 100644 --- a/cps/web.py +++ b/cps/web.py @@ -316,10 +316,7 @@ def import_ldap_users(): try: new_users = services.ldap.get_group_members(config.config_ldap_group_name) except (services.ldap.LDAPException, TypeError, AttributeError, KeyError) as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) showtext['text'] = _(u'Error: %(ldaperror)s', ldaperror=e) return json.dumps(showtext) if not new_users: @@ -347,10 +344,7 @@ def import_ldap_users(): try: user_data = services.ldap.get_object_details(user=user_identifier, query_filter=query_filter) except AttributeError as e: - if config.config_log_level == logger.logging.DEBUG: - log.exception(e) - else: - log.error(e) + log.debug_or_exception(e) continue if user_data: user_login_field = extract_dynamic_field_from_filter(user, config.config_ldap_user_object)