Skip to content

Commit

Permalink
Refactor logging setup
Browse files Browse the repository at this point in the history
Centralized logging configuration in `__init__.py`
  • Loading branch information
andreztz committed Jun 26, 2024
1 parent b7d573e commit 41290b5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
33 changes: 33 additions & 0 deletions xradios/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
import os
import logging

from appdirs import user_log_dir
from appdirs import user_data_dir
from appdirs import user_config_dir

from pathlib import Path

__version__ = "0.0.1.dev6"

app_name = "xradios"

xradios_config_dir = Path(user_config_dir(appname=app_name))
xradios_config_dir.mkdir(parents=True, exist_ok=True)

xradios_data_dir = Path(user_data_dir(appname=app_name))
xradios_data_dir.mkdir(parents=True, exist_ok=True)

xradios_log_dir = Path(user_log_dir(appname=app_name))
xradios_log_dir.mkdir(parents=True, exist_ok=True)

log_level = getattr(logging, os.environ.get("XRADIOS_LOG_LEVEL", "INFO"))

if log_level == logging.DEBUG:
log_format = "%(levelname)s - %(name)s - %(module)s - %(funcName)s - %(message)s"
else:
log_format = "%(levelname)s - %(name)s - %(message)s"

log_file = "xradios.log"

logging.basicConfig(
filename=xradios_log_dir / log_file, level=log_level, format=log_format
)
33 changes: 3 additions & 30 deletions xradios/core/server.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,24 @@
import logging
import os
import sys
import signal

from pathlib import Path

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

from appdirs import user_data_dir
from appdirs import user_log_dir
from appdirs import user_config_dir

from pyradios import RadioBrowser

from tinydb import Query
from tinydb import TinyDB

from xradios.core.metadata import metadata_manager
from xradios.core.player import player
from xradios import xradios_data_dir


app_name = 'xradios'
server_name = 'xradiosd'

xradios_config_dir = Path(user_config_dir(appname=app_name))
xradios_config_dir.mkdir(parents=True, exist_ok=True)

xradios_data_dir = Path(user_data_dir(appname=app_name))
xradios_data_dir.mkdir(parents=True, exist_ok=True)

xradios_log_dir = Path(user_log_dir(appname=app_name))
xradios_log_dir.mkdir(parents=True, exist_ok=True)

log_level = getattr(logging, os.environ.get('XRADIOS_LOG_LEVEL', 'INFO'))
log_format = '%(levelname)s - %(name)s - %(message)s'
log_file = 'xradios.log'

logging.basicConfig(
filename=xradios_log_dir / log_file,
level=log_level,
format=log_format
)

log = logging.getLogger('xradios')
log = logging.getLogger("xradiosd")
effective_log_level = logging.getLevelName(log.getEffectiveLevel())
log.info(f'Log level {effective_log_level=}')
log.info(f"Log level {effective_log_level=}")


rb = RadioBrowser()
Expand Down

0 comments on commit 41290b5

Please sign in to comment.