Skip to content

Commit

Permalink
refactor: Refactored logging functions to consistently use lazy inter…
Browse files Browse the repository at this point in the history
…polation (#2071)

* Refactored logging functions to consistently use lazy interpolation

* Code formatting

---------

Co-authored-by: Daniel Vaz Gaspar <danielvazgaspar@gmail.com>
  • Loading branch information
xuganyu96 and dpgaspar authored Jul 10, 2023
1 parent 6640cce commit fbb2af7
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 231 deletions.
6 changes: 3 additions & 3 deletions examples/quickcharts/app/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def fill_data():
db.session.add(c)
db.session.commit()
except Exception as e:
log.error("Update ViewMenu error: {0}".format(str(e)))
log.error("Update ViewMenu error: %s", e)
db.session.rollback()
for political in politicals:
c = PoliticalType(name=political)
try:
db.session.add(c)
db.session.commit()
except Exception as e:
log.error("Update ViewMenu error: {0}".format(str(e)))
log.error("Update ViewMenu error: %s", e)
db.session.rollback()
try:
for x in range(1, 20):
Expand All @@ -51,5 +51,5 @@ def fill_data():
db.session.add(cs)
db.session.commit()
except Exception as e:
log.error("Update ViewMenu error: {0}".format(str(e)))
log.error("Update ViewMenu error: %s", e)
db.session.rollback()
6 changes: 3 additions & 3 deletions examples/quickcharts2/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def fill_data():
db.session.add(c)
db.session.commit()
except Exception as e:
log.error("Update ViewMenu error: {0}".format(str(e)))
log.error("Update ViewMenu error: %s", e)
db.session.rollback()
for political in politicals:
c = PoliticalType(name=political)
try:
db.session.add(c)
db.session.commit()
except Exception as e:
log.error("Update ViewMenu error: {0}".format(str(e)))
log.error("Update ViewMenu error: %s", e)
db.session.rollback()
try:
for x in range(1, 20):
Expand All @@ -61,7 +61,7 @@ def fill_data():
db.session.add(cs)
db.session.commit()
except Exception as e:
log.error("Update ViewMenu error: {0}".format(str(e)))
log.error("Update ViewMenu error: %s", e)
db.session.rollback()


Expand Down
4 changes: 2 additions & 2 deletions examples/user_registration/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ class ContactTimeChartView(GroupByChartView):
category="Contacts",
)

log.info("F.A.B. Version: {0}".format(appbuilder.version))
log.info("User extension class {0}".format(UserExtensionMixin.__subclasses__()[0]))
log.info("F.A.B. Version: %s", appbuilder.version)
log.info("User extension class %s", UserExtensionMixin.__subclasses__()[0])
9 changes: 6 additions & 3 deletions flask_appbuilder/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def add_api_spec(self, api_spec: APISpec) -> None:
):
continue
if attr_name in self.exclude_route_methods:
log.info(f"Not registering api spec for method {attr_name}")
log.info("Not registering api spec for method %s", attr_name)
continue
operations = {}
path = self.path_helper(path=url, operations=operations)
Expand Down Expand Up @@ -612,13 +612,16 @@ def _register_urls(self) -> None:
):
continue
if attr_name in self.exclude_route_methods:
log.info(f"Not registering route for method {attr_name}")
log.info("Not registering route for method %s", attr_name)
continue
attr = getattr(self, attr_name)
if hasattr(attr, "_urls"):
for url, methods in attr._urls:
log.info(
f"Registering route {self.blueprint.url_prefix}{url} {methods}"
"Registering route %s%s %s",
self.blueprint.url_prefix,
url,
methods,
)
route_handler = wrap_route_handler_with_hooks(
attr_name, attr, before_request_hooks
Expand Down
16 changes: 8 additions & 8 deletions flask_appbuilder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def dynamic_class_import(class_path: str) -> Optional[DynamicImportType]:
return reduce(getattr, tmp[1:], package)
except Exception as e:
log.exception(e)
log.error(LOGMSG_ERR_FAB_ADDON_IMPORT.format(class_path, e))
log.error(LOGMSG_ERR_FAB_ADDON_IMPORT, class_path, e)
return None


Expand Down Expand Up @@ -341,10 +341,10 @@ def _add_addon_views(self) -> None:
inst_addon_class.register_views()
inst_addon_class.post_process()
self.addon_managers[addon] = inst_addon_class
log.info(LOGMSG_INF_FAB_ADDON_ADDED.format(str(addon)))
log.info(LOGMSG_INF_FAB_ADDON_ADDED, addon)
except Exception as e:
log.exception(e)
log.error(LOGMSG_ERR_FAB_ADDON_PROCESS.format(addon, e))
log.error(LOGMSG_ERR_FAB_ADDON_PROCESS, addon, e)

def _check_and_init(
self, baseview: Union[Type["AbstractViewApi"], "AbstractViewApi"]
Expand Down Expand Up @@ -438,7 +438,7 @@ def add_view(
appbuilder.add_link("google", href="www.google.com", icon = "fa-google-plus")
"""
baseview = self._check_and_init(baseview)
log.info(LOGMSG_INF_FAB_ADD_VIEW.format(baseview.__class__.__name__, name))
log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, name)

if not self._view_exists(baseview):
baseview.appbuilder = self
Expand Down Expand Up @@ -555,7 +555,7 @@ def add_view_no_menu(
"""
baseview = self._check_and_init(baseview)
log.info(LOGMSG_INF_FAB_ADD_VIEW.format(baseview.__class__.__name__, ""))
log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, "")

if not self._view_exists(baseview):
baseview.appbuilder = self
Expand All @@ -568,7 +568,7 @@ def add_view_no_menu(
self._add_permission(baseview)
self.add_limits(baseview)
else:
log.warning(LOGMSG_WAR_FAB_VIEW_EXISTS.format(baseview.__class__.__name__))
log.warning(LOGMSG_WAR_FAB_VIEW_EXISTS, baseview.__class__.__name__)
return baseview

def add_api(self, baseview: Type["AbstractViewApi"]) -> "AbstractViewApi":
Expand Down Expand Up @@ -675,15 +675,15 @@ def _add_permission(
)
except Exception as e:
log.exception(e)
log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW.format(str(e)))
log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW, e)

def _add_permissions_menu(self, name: str, update_perms: bool = False) -> None:
if self.update_perms or update_perms:
try:
self.sm.add_permissions_menu(name)
except Exception as e:
log.exception(e)
log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_MENU.format(str(e)))
log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_MENU, e)

def _add_menu_permissions(self, update_perms: bool = False) -> None:
if self.menu is None:
Expand Down
12 changes: 8 additions & 4 deletions flask_appbuilder/baseviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,19 @@ def _register_urls(self):
continue
if attr_name in self.exclude_route_methods:
log.info(
f"Not registering route for method "
f"{self.__class__.__name__}.{attr_name}"
"Not registering route for method %s.%s",
self.__class__.__name__,
attr_name,
)
continue
attr = getattr(self, attr_name)
if hasattr(attr, "_urls"):
for url, methods in attr._urls:
log.info(
f"Registering route {self.blueprint.url_prefix}{url} {methods}"
"Registering route %s%s %s",
self.blueprint.url_prefix,
url,
methods,
)
route_handler = wrap_route_handler_with_hooks(
attr_name, attr, before_request_hooks
Expand Down Expand Up @@ -1010,7 +1014,7 @@ def _get_related_view_widget(
name = related_view.__name__
else:
name = related_view.__class__.__name__
log.error("Can't find relation on related view {0}".format(name))
log.error("Can't find relation on related view %s", name)
return None
return related_view._get_view_widget(
filters=filters,
Expand Down
94 changes: 46 additions & 48 deletions flask_appbuilder/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,111 +17,109 @@
"""

LOGMSG_ERR_SEC_ACCESS_DENIED = "Access is Denied for: {0} on: {1}"
LOGMSG_ERR_SEC_ACCESS_DENIED = "Access is Denied for: %s on: %s"
""" Access denied log message, format with user and view/resource """
LOGMSG_WAR_SEC_LOGIN_FAILED = "Login Failed for user: {0}"
LOGMSG_ERR_SEC_CREATE_DB = "DB Creation and initialization failed: {0}"
LOGMSG_WAR_SEC_LOGIN_FAILED = "Login Failed for user: %s"
LOGMSG_ERR_SEC_CREATE_DB = "DB Creation and initialization failed: %s"
""" security models creation fails, format with error message """
LOGMSG_ERR_SEC_ADD_ROLE = "Add Role: {0}"
LOGMSG_ERR_SEC_ADD_ROLE = "Add Role: %s"
""" Error adding role, format with err message """
LOGMSG_ERR_SEC_ADD_PERMISSION = "Add Permission: {0}"
LOGMSG_ERR_SEC_ADD_PERMISSION = "Add Permission: %s"
""" Error adding permission, format with err message """
LOGMSG_ERR_SEC_ADD_VIEWMENU = "Add View Menu Error: {0}"
LOGMSG_ERR_SEC_ADD_VIEWMENU = "Add View Menu Error: %s"
""" Error adding view menu, format with err message """
LOGMSG_ERR_SEC_DEL_PERMISSION = "Del Permission Error: {0}"
LOGMSG_ERR_SEC_DEL_PERMISSION = "Del Permission Error: %s"
""" Error deleting permission, format with err message """
LOGMSG_ERR_SEC_ADD_PERMVIEW = "Creation of Permission View Error: {0}"
LOGMSG_ERR_SEC_ADD_PERMVIEW = "Creation of Permission View Error: %s"
""" Error adding permission view, format with err message """
LOGMSG_ERR_SEC_DEL_PERMVIEW = "Remove Permission from View Error: {0}"
LOGMSG_ERR_SEC_DEL_PERMVIEW = "Remove Permission from View Error: %s"
""" Error deleting permission view, format with err message """
LOGMSG_WAR_SEC_DEL_PERMVIEW = (
"Refused to delete permission view, assoc with role exists {}.{} {}"
"Refused to delete permission view, assoc with role exists %s.%s %s"
)
LOGMSG_WAR_SEC_DEL_PERMISSION = "Refused to delete, permission {} does not exist"
LOGMSG_WAR_SEC_DEL_VIEWMENU = "Refused to delete, view menu {} does not exist"
LOGMSG_WAR_SEC_DEL_PERM_PVM = "Refused to delete permission {}, PVM exists {}"
LOGMSG_WAR_SEC_DEL_VIEWMENU_PVM = "Refused to delete view menu {}, PVM exists {}"
LOGMSG_ERR_SEC_ADD_PERMROLE = "Add Permission to Role Error: {0}"
LOGMSG_WAR_SEC_DEL_PERMISSION = "Refused to delete, permission %s does not exist"
LOGMSG_WAR_SEC_DEL_VIEWMENU = "Refused to delete, view menu %s does not exist"
LOGMSG_WAR_SEC_DEL_PERM_PVM = "Refused to delete permission %s, PVM exists %s"
LOGMSG_WAR_SEC_DEL_VIEWMENU_PVM = "Refused to delete view menu %s, PVM exists %s"
LOGMSG_ERR_SEC_ADD_PERMROLE = "Add Permission to Role Error: %s"
""" Error adding permission to role, format with err message """
LOGMSG_ERR_SEC_DEL_PERMROLE = "Remove Permission to Role Error: {0}"
LOGMSG_ERR_SEC_DEL_PERMROLE = "Remove Permission to Role Error: %s"
""" Error deleting permission to role, format with err message """
LOGMSG_ERR_SEC_ADD_REGISTER_USER = "Add Register User Error: {0}"
LOGMSG_ERR_SEC_ADD_REGISTER_USER = "Add Register User Error: %s"
""" Error adding registered user, format with err message """
LOGMSG_ERR_SEC_DEL_REGISTER_USER = "Remove Register User Error: {0}"
LOGMSG_ERR_SEC_DEL_REGISTER_USER = "Remove Register User Error: %s"
""" Error deleting registered user, format with err message """
LOGMSG_ERR_SEC_NO_REGISTER_HASH = "Attempt to activate user with false hash: {0}"
LOGMSG_ERR_SEC_NO_REGISTER_HASH = "Attempt to activate user with false hash: %s"
""" Attempt to activate user with not registered hash, format with hash """
LOGMSG_ERR_SEC_AUTH_LDAP = "LDAP Error {0}"
LOGMSG_ERR_SEC_AUTH_LDAP = "LDAP Error %s"
""" Generic LDAP error, format with err message """
LOGMSG_ERR_SEC_AUTH_LDAP_TLS = (
"LDAP Could not activate TLS on established connection with {0}"
"LDAP Could not activate TLS on established connection with %s"
)
""" LDAP Could not activate TLS on established connection with server """
LOGMSG_ERR_SEC_ADD_USER = "Error adding new user to database. {0}"
LOGMSG_ERR_SEC_ADD_USER = "Error adding new user to database. %s"
""" Error adding user, format with err message """
LOGMSG_ERR_SEC_UPD_USER = "Error updating user to database. {0} "
LOGMSG_ERR_SEC_UPD_USER = "Error updating user to database. %s "
""" Error updating user, format with err message """
LOGMSG_WAR_SEC_NO_USER = "No user yet created, use flask fab command to do it."
""" Warning when app starts if no user exists on db """
LOGMSG_WAR_SEC_NOLDAP_OBJ = "No LDAP object found for: {0}"
LOGMSG_WAR_SEC_NOLDAP_OBJ = "No LDAP object found for: %s"

LOGMSG_INF_SEC_ADD_PERMVIEW = "Created Permission View: {0}"
LOGMSG_INF_SEC_ADD_PERMVIEW = "Created Permission View: %s"
""" Info when adding permission view, format with permission view class string """
LOGMSG_INF_SEC_DEL_PERMVIEW = "Removed Permission View: {0} on {1}"
LOGMSG_INF_SEC_DEL_PERMVIEW = "Removed Permission View: %s on %s"
""" Info when deleting permission view, format with permission name and view name """
LOGMSG_INF_SEC_ADD_PERMROLE = "Added Permission {0} to role {1}"
LOGMSG_INF_SEC_ADD_PERMROLE = "Added Permission %s to role %s"
""" Info when adding permission to role,
format with permission view class string and role name """
LOGMSG_INF_SEC_DEL_PERMROLE = "Removed Permission {0} to role {1}"
LOGMSG_INF_SEC_DEL_PERMROLE = "Removed Permission %s to role %s"
""" Info when deleting permission to role,
format with permission view class string and role name """
LOGMSG_INF_SEC_ADD_ROLE = "Inserted Role: {0}"
LOGMSG_INF_SEC_ADD_ROLE = "Inserted Role: %s"
""" Info when added role, format with role name """
LOGMSG_INF_SEC_NO_DB = "Security DB not found Creating all Models from Base"
LOGMSG_INF_SEC_ADD_DB = "Security DB Created"
LOGMSG_INF_SEC_ADD_USER = "Added user {0}"
LOGMSG_INF_SEC_ADD_USER = "Added user %s"
""" User added, format with username """
LOGMSG_INF_SEC_UPD_USER = "Updated user {0}"
LOGMSG_INF_SEC_UPD_USER = "Updated user %s"
""" User updated, format with username """
LOGMSG_INF_SEC_UPD_ROLE = "Updated role {0}"
LOGMSG_INF_SEC_UPD_ROLE = "Updated role %s"
""" Role updated, format with role name """
LOGMSG_ERR_SEC_UPD_ROLE = "An error occurred updating role {0}"
LOGMSG_ERR_SEC_UPD_ROLE = "An error occurred updating role %s"
""" Role updated Error, format with role name """

LOGMSG_INF_FAB_ADDON_ADDED = "Registered AddOn: {0}"
LOGMSG_INF_FAB_ADDON_ADDED = "Registered AddOn: %s"
""" Addon imported and registered """
LOGMSG_ERR_FAB_ADDON_IMPORT = "An error occurred when importing declared addon {0}: {1}"
LOGMSG_ERR_FAB_ADDON_IMPORT = "An error occurred when importing declared addon %s: %s"
""" Error on addon import, format with addon class path and error message """
LOGMSG_ERR_FAB_ADDON_PROCESS = (
"An error occurred when processing declared addon {0}: {1}"
)
LOGMSG_ERR_FAB_ADDON_PROCESS = "An error occurred when processing declared addon %s: %s"
""" Error on addon processing (pre, register, post),
format with addon class path and error message """


LOGMSG_ERR_FAB_ADD_PERMISSION_MENU = "Add Permission on Menu Error: {0}"
LOGMSG_ERR_FAB_ADD_PERMISSION_MENU = "Add Permission on Menu Error: %s"
""" Error when adding a permission to a menu, format with err """
LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW = "Add Permission on View Error: {0}"
LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW = "Add Permission on View Error: %s"
""" Error when adding a permission to a menu, format with err """

LOGMSG_ERR_DBI_ADD_GENERIC = "Add record error: {0}"
LOGMSG_ERR_DBI_ADD_GENERIC = "Add record error: %s"
""" Database add generic error, format with err message """
LOGMSG_ERR_DBI_EDIT_GENERIC = "Edit record error: {0}"
LOGMSG_ERR_DBI_EDIT_GENERIC = "Edit record error: %s"
""" Database edit generic error, format with err message """
LOGMSG_ERR_DBI_DEL_GENERIC = "Delete record error: {0}"
LOGMSG_ERR_DBI_DEL_GENERIC = "Delete record error: %s"
""" Database delete generic error, format with err message """
LOGMSG_WAR_DBI_AVG_ZERODIV = "Zero division on aggregate_avg"

LOGMSG_WAR_FAB_VIEW_EXISTS = "View already exists {0} ignoring"
LOGMSG_WAR_FAB_VIEW_EXISTS = "View already exists %s ignoring"
""" Attempt to add an already added view, format with view name """
LOGMSG_WAR_DBI_ADD_INTEGRITY = "Add record integrity error: {0}"
LOGMSG_WAR_DBI_ADD_INTEGRITY = "Add record integrity error: %s"
""" Dabase integrity error, format with err message """
LOGMSG_WAR_DBI_EDIT_INTEGRITY = "Edit record integrity error: {0}"
LOGMSG_WAR_DBI_EDIT_INTEGRITY = "Edit record integrity error: %s"
""" Dabase integrity error, format with err message """
LOGMSG_WAR_DBI_DEL_INTEGRITY = "Delete record integrity error: {0}"
LOGMSG_WAR_DBI_DEL_INTEGRITY = "Delete record integrity error: %s"
""" Dabase integrity error, format with err message """

LOGMSG_INF_FAB_ADD_VIEW = "Registering class {0} on menu {1}"
LOGMSG_INF_FAB_ADD_VIEW = "Registering class %s on menu %s"
""" Inform that view class was added, format with class name, name"""


Expand Down
4 changes: 2 additions & 2 deletions flask_appbuilder/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def convert(self):
validators=self.validators,
default=self.default,
)
log.error("Column %s Type not supported" % self.colname)
log.error("Column %s Type not supported", self.colname)


class GeneralModelConverter(object):
Expand Down Expand Up @@ -257,7 +257,7 @@ def _convert_col(
form_props,
)
else:
log.warning("Relation {0} not supported".format(col_name))
log.warning("Relation %s not supported", col_name)
else:
return self._convert_simple(
col_name, label, description, lst_validators, form_props
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/models/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def convert(self, col_name):
for conversion in self.conversion_table:
if getattr(self.datamodel, conversion[0])(col_name):
return [item(col_name, self.datamodel) for item in conversion[1]]
log.warning("Filter type not supported for column: %s" % col_name)
log.warning("Filter type not supported for column: %s", col_name)


class Filters(object):
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def to_dict(self, data):
for group_col_data, i in zip(item[0], enumerate(item[0])):
row[self.group_bys_cols[i]] = str(group_col_data)
for col_data, i in zip(item[1:], enumerate(item[1:])):
log.debug("{0},{1}".format(col_data, i))
log.debug("%s,%s", col_data, i)
key = self.aggr_by_cols[i].__name__ + self.aggr_by_cols[i]
if isinstance(col_data, datetime.date):
row[key] = str(col_data)
Expand Down
Loading

0 comments on commit fbb2af7

Please sign in to comment.