Skip to content

Commit

Permalink
refactor: minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zluuba committed May 21, 2024
1 parent 49dcaa5 commit 2991c4f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 1 addition & 5 deletions zluuba_art/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
from zluuba_art.app import app # noqa: F401

__all__ = (
app,
)
from zluuba_art.app import app # noqa: F401
3 changes: 2 additions & 1 deletion zluuba_art/logger/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
DEBUG, INFO, WARNING, ERROR, CRITICAL,
)
from os import path
from pathlib import Path, PosixPath
from typing import Union


Expand All @@ -21,7 +22,7 @@


def get_logger(filename: str,
basedir: Union[str, path],
basedir: Union[Path, PosixPath],
name: str = DEFAULT_NAME,
log_level: str = DEFAULT_LEVEL) -> Logger:
"""
Expand Down
17 changes: 9 additions & 8 deletions zluuba_art/logger/logger.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask import Request
from logging import Logger
from pathlib import Path
import requests
from typing import Union
from requests import get, RequestException
from typing import Union, Optional, Any

from .configuration import get_logger
from zluuba_art.logger.configuration import get_logger


BASE_DIR = Path(__file__).parents[1]
Expand Down Expand Up @@ -34,15 +34,15 @@ def get_users_statistic_log_msg(request: Request) -> str:
user_agent = request.headers.get('User-Agent')

try:
response = requests.get(f'https://ipapi.co/{ip_address}/json/')
response = get(f'https://ipapi.co/{ip_address}/json/')
response.raise_for_status()
geo_data = response.json()

country = geo_data.get('country_name', 'Unknown')
city = geo_data.get('city', 'Unknown')
region = geo_data.get('region', 'Unknown')

except requests.RequestException:
except RequestException:
country = city = region = 'Unknown'

return (f'Visit from {ip_address} - Country: {country}, '
Expand All @@ -63,18 +63,19 @@ def get_server_error_log_msg(error: Exception) -> str:

def log(log_type: str,
data: Union[Request, Exception],
logger: Logger = None,
logger: Optional[Logger] = None,
log_level: str = 'info'):
"""
Logs a message based on the log type and data provided.
:param log_type: Type of log ('users_statistic' or 'server_error').
:param data: Data to log (Request for 'users_statistic' or Exception for 'server_error').
:param data: Data to log (Request for 'users_statistic' or
Exception for 'server_error').
:param logger: Logger instance to use.
:param log_level: Log level as a string.
"""

log_msg_funcs = {
log_msg_funcs: dict[str, Any] = {
'users_statistic': {
'log_msg_func': get_users_statistic_log_msg,
'logger': users_logger,
Expand Down

0 comments on commit 2991c4f

Please sign in to comment.