Skip to content

Commit

Permalink
Add Ruff logging rules (#78)
Browse files Browse the repository at this point in the history
* Add ruff logging rules

* Add LOG ruff rule
  • Loading branch information
pederhan authored Mar 21, 2024
1 parent d85d8b4 commit e4c3bf1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ packages = ["zabbix_auto_config"]

[tool.ruff.lint]
extend-select = [
"G", # flake8-logging-format
"I", # isort
"LOG", # flake8-logging
"PLE1205", # pylint (too many logging args)
"PLE1206", # pylint (too few logging args)
]

[tool.ruff.lint.isort]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_host_merge_invalid(full_hosts):
["notset", logging.NOTSET],
["debug", logging.DEBUG],
["info", logging.INFO],
["warn", logging.WARN],
["warn", logging.WARN], # noqa: LOG009 # we need to test the deprecated name
["warning", logging.WARNING],
["error", logging.ERROR],
["fatal", logging.FATAL],
Expand All @@ -149,7 +149,7 @@ def test_zacsettings_log_level_str(level: str, expect: int, upper: bool) -> None
[0, logging.NOTSET],
[10, logging.DEBUG],
[20, logging.INFO],
[30, logging.WARN],
[30, logging.WARN], # noqa: LOG009 # we need to test the deprecated name
[30, logging.WARNING],
[40, logging.ERROR],
[50, logging.FATAL],
Expand Down
14 changes: 7 additions & 7 deletions zabbix_auto_config/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ def get_current_source_hosts(
host = models.Host(**result[0])
except ValidationError as e:
# TODO: ensure this actually identifies the faulty host
logging.exception(f"Invalid host in source hosts table: {e}")
logging.exception("Invalid host in source hosts table: %s", e)
except Exception as e:
logging.exception(
f"Error when parsing host from source hosts table: {e}"
"Error when parsing host from source hosts table: %s", e
)
else:
hosts[host.hostname] = host
Expand Down Expand Up @@ -524,10 +524,10 @@ def get_source_hosts(self, cursor: "Cursor") -> Dict[str, List[models.Host]]:
host_model = models.Host(**host[0])
except ValidationError as e:
# TODO: ensure this actually identifies the faulty host
logging.exception(f"Invalid host in source hosts table: {e}")
logging.exception("Invalid host in source hosts table: %s", e)
except Exception as e:
logging.exception(
f"Error when parsing host from source hosts table: {e}"
"Error when parsing host from source hosts table: %s", e
)
else:
source_hosts[host_model.hostname].append(host_model)
Expand All @@ -541,9 +541,9 @@ def get_hosts(self, cursor: "Cursor") -> Dict[str, models.Host]:
host_model = models.Host(**host[0])
except ValidationError as e:
# TODO: ensure this log actually identifies the faulty host
logging.exception(f"Invalid host in hosts table: {e}")
logging.exception("Invalid host in hosts table: %s", e)
except Exception as e:
logging.exception(f"Error when parsing host from hosts table: {e}")
logging.exception("Error when parsing host from hosts table: %s", e)
else:
hosts[host_model.hostname] = host_model
return hosts
Expand Down Expand Up @@ -590,7 +590,7 @@ def merge_sources(self):
host = hosts.get(hostname)
if not source_hosts:
logging.warning(
f"Host '{hostname}' not found in source hosts table"
"Host '%s' not found in source hosts table", hostname
)
continue

Expand Down
15 changes: 6 additions & 9 deletions zabbix_auto_config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ def read_map_file(path: Union[str, Path]) -> Dict[str, List[str]]:
)
except ValueError:
logging.warning(
"Invalid format at line {lineno} in map file '{path}'. Expected 'key:value', got '{line}'.".format(
lineno=lineno, path=path, line=line
),
"Invalid format at line %d in map file '%s'. Expected 'key:value', got '%s'.",
lineno,
path,
line,
)
continue

if key in _map:
logging.warning(
"Duplicate key {key} at line {lineno} in map file '{path}'.".format(
key=key, lineno=lineno, path=path
)
"Duplicate key %s at line %d in map file '%s'.", key, lineno, path
)
_map[key].extend(values)
else:
Expand All @@ -90,9 +89,7 @@ def read_map_file(path: Union[str, Path]) -> Dict[str, List[str]]:
values_dedup = list(dict.fromkeys(values)) # dict.fromkeys() guarantees order
if len(values) != len(values_dedup):
logging.warning(
"Ignoring duplicate values for key '{key}' in map file '{path}'.".format(
key=key, path=path
)
"Ignoring duplicate values for key '%s' in map file '%s'.", key, path
)
_map[key] = values_dedup
return _map
Expand Down

0 comments on commit e4c3bf1

Please sign in to comment.