Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FEATURE: Central metric logging) #523

Draft
wants to merge 4 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/dataSources.local.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .idea/runConfigurations/Run_Unmanic.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions .idea/runConfigurations/Run_Unmanic_Dev.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/unmanic.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ psutil~=6.0.0
requests>=2.28.2
requests_toolbelt>=0.9.1
py-cpuinfo>=7.0.0
JSON-log-formatter~=0.5.2

# Optional requirements
watchdog>=2.1.1
Expand Down
6 changes: 0 additions & 6 deletions tests/support_/test_data/data_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@
"""
import queue

from unmanic.libs import unlogger

unmanic_logging = unlogger.UnmanicLogger.__call__(False)
unmanic_logging.get_logger()

data_queues = {
"scheduledtasks": queue.Queue(),
"inotifytasks": queue.Queue(),
"progress_reports": queue.Queue(),
"logging": unmanic_logging
}
75 changes: 0 additions & 75 deletions tests/unit/test_unlogger.py

This file was deleted.

39 changes: 11 additions & 28 deletions unmanic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@
import json

from unmanic import metadata
from unmanic.libs import unlogger
from unmanic.libs import common
from unmanic.libs.logs import UnmanicLogging
from unmanic.libs.singleton import SingletonType

try:
from json.decoder import JSONDecodeError
except ImportError:
JSONDecodeError = ValueError

logger = UnmanicLogging.get_logger(name="Config")


class Config(object, metaclass=SingletonType):
app_version = ''
Expand Down Expand Up @@ -117,23 +119,6 @@ def __init__(self, config_path=None, **kwargs):
# Apply settings to the unmanic logger
self.__setup_unmanic_logger()

def _log(self, message, message2='', level="info"):
"""
Generic logging method. Can be implemented on any unmanic class

:param message:
:param message2:
:param level:
:return:
"""
unmanic_logging = unlogger.UnmanicLogger.__call__()
logger = unmanic_logging.get_logger(__class__.__name__)
if logger:
message = common.format_message(message, message2)
getattr(logger, level)(message)
else:
print("Unmanic.{} - ERROR!!! Failed to find logger".format(self.__name__))

def get_config_as_dict(self):
"""
Return a dictionary of configuration fields and their current values
Expand All @@ -156,8 +141,7 @@ def __setup_unmanic_logger(self):

:return:
"""
unmanic_logging = unlogger.UnmanicLogger.__call__()
unmanic_logging.setup_logger(self)
UnmanicLogging.get_logger(settings=self)

def __import_settings_from_env(self):
"""
Expand Down Expand Up @@ -189,7 +173,7 @@ def __import_settings_from_file(self, config_path=None):
with open(settings_file) as infile:
data = json.load(infile)
except Exception as e:
self._log("Exception in reading saved settings from file:", message2=str(e), level="exception")
logger.exception("Exception in reading saved settings from file: %s", e)
# Set data to Config class
self.set_bulk_config_items(data, save_settings=False)

Expand All @@ -206,7 +190,7 @@ def __write_settings_to_file(self):
result = common.json_dump_to_file(data, settings_file)
if not result['success']:
for message in result['errors']:
self._log("Error:", message2=str(message), level="error")
logger.error(message)
raise Exception("Exception in writing settings to file")

def get_config_item(self, key):
Expand Down Expand Up @@ -239,7 +223,7 @@ def set_config_item(self, key, value, save_settings=True):
field_id = key.lower()
# Check if key is a valid setting
if field_id not in self.get_config_keys():
self._log("Attempting to save unknown key", message2=str(key), level="warning")
logger.warning("Attempting to save unknown key: %s", key)
# Do not proceed if this is any key other than the database
return

Expand All @@ -257,7 +241,7 @@ def set_config_item(self, key, value, save_settings=True):
try:
self.__write_settings_to_file()
except Exception as e:
self._log("Failed to write settings to file: ", message2=str(self.get_config_as_dict()), level="exception")
logger.exception("Failed to write settings to file: %s", str(self.get_config_as_dict()))

def set_bulk_config_items(self, items, save_settings=True):
"""
Expand Down Expand Up @@ -323,7 +307,7 @@ def set_cache_path(self, cache_path):
:return:
"""
if cache_path == "":
self._log("Cache path cannot be empty. Resetting it to default", level="warning")
logger.warning("Cache path cannot be empty. Resetting it to default.")
cache_path = common.get_default_cache_path()
self.cache_path = cache_path

Expand Down Expand Up @@ -351,11 +335,10 @@ def set_debugging(self, value):

:return:
"""
unmanic_logging = unlogger.UnmanicLogger.__call__()
if value:
unmanic_logging.enable_debugging()
UnmanicLogging.enable_debugging()
else:
unmanic_logging.disable_debugging()
UnmanicLogging.disable_debugging()
self.debugging = value

def get_first_run(self):
Expand Down
8 changes: 0 additions & 8 deletions unmanic/libs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,3 @@
OR OTHER DEALINGS IN THE SOFTWARE.

"""
import sys
import warnings

from .unlogger import UnmanicLogger

__all__ = (
'UnmanicLogger',
)
22 changes: 7 additions & 15 deletions unmanic/libs/db_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from peewee import Model, SqliteDatabase, Field
from peewee_migrate import Migrator, Router

from unmanic.libs import unlogger
from unmanic.libs.logs import UnmanicLogging
from unmanic.libs.unmodels.lib import BaseModel


Expand All @@ -48,12 +48,10 @@ class Migrations(object):
Handle all migrations during application start.
"""

logger = None
database = None

def __init__(self, config):
unmanic_logging = unlogger.UnmanicLogger.__call__()
self.logger = unmanic_logging.get_logger(__class__.__name__)
self.logger = UnmanicLogging.get_logger(name=__class__.__name__)

# Based on configuration, select database to connect to.
if config['TYPE'] == 'SQLITE':
Expand All @@ -70,12 +68,6 @@ def __init__(self, config):

self.migrator = Migrator(self.database)

def __log(self, message, level='info'):
if self.logger:
getattr(self.logger, level)(message)
else:
print(message)

def __run_all_migrations(self):
"""
Run all new migrations.
Expand All @@ -102,23 +94,23 @@ def update_schema(self):
all_models = [tup[1] for tup in discovered_models]

# Start by creating all models
self.__log("Initialising database tables")
self.logger.info("Initialising database tables")
try:
with self.database.transaction():
for model in all_models:
self.migrator.create_table(model)
self.migrator.run()
except Exception:
self.database.rollback()
self.__log("Initialising tables failed", level='exception')
self.logger.exception("Initialising tables failed")
raise

# Migrations will only be used for removing obsolete columns
self.__run_all_migrations()

# Newly added fields can be auto added with this function... no need for a migration script
# Ensure all files are also present for each of the model classes
self.__log("Updating database fields")
self.logger.info("Updating database fields")
for model in all_models:
if issubclass(model, BaseModel):
# Fetch all peewee fields for the model class
Expand All @@ -131,12 +123,12 @@ def update_schema(self):
if isinstance(field, Field):
if not any(f for f in self.database.get_columns(model._meta.name) if f.name == field.name):
# Field does not exist in DB table
self.__log("Adding missing column")
self.logger.info("Adding missing column")
try:
with self.database.transaction():
self.migrator.add_columns(model, **{field.name: field})
self.migrator.run()
except Exception:
self.database.rollback()
self.__log("Update failed", level='exception')
self.logger.exception("Update failed")
raise
Loading
Loading