1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-03-11 00:58:39 +03:00

Refactor logging in Module and Service classes

This commit is contained in:
Adolfo Gómez García 2024-07-05 17:09:49 +02:00
parent 6e59402747
commit 29179a31df
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
7 changed files with 45 additions and 27 deletions

View File

@ -39,7 +39,7 @@ import typing
from django.utils.translation import gettext as _
from uds.core.ui.user_interface import UserInterface
from uds.core.util import utils
from uds.core.util import utils, log
from .environment import Environment, Environmentable
from .serializable import Serializable
@ -214,11 +214,33 @@ class Module(UserInterface, Environmentable, Serializable, abc.ABC):
Nothing
"""
pass
@abc.abstractmethod
def db_obj(self) -> 'UUIDModel':
"""
Returns the database object associated with this module.
Can return "null()" if no database object is associated with this module.
"""
...
def do_log(
self,
level: 'types.log.LogLevel',
message: str,
source: types.log.LogSource = types.log.LogSource.MODULE,
) -> None:
"""
Logs a message at the level specified.
Args:
level: Level of the message
message: Message to log
"""
dbobj = self.db_obj()
if dbobj.is_null():
logger.error('Trying to log a message for a null object')
return
log.log(dbobj, level, message, source)
@classmethod
def mod_name(cls: type['Module']) -> str:
@ -310,4 +332,3 @@ class Module(UserInterface, Environmentable, Serializable, abc.ABC):
def __str__(self) -> str:
return "Base Module"

View File

@ -314,7 +314,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
return self.service().sanitized_name(f'UDS_{name}') # Default implementation
# overridable, to allow receiving notifications from, for example, usersevice
# overridable, to allow receiving notifications from, for example, services
def notify(self, message: str, data: typing.Any = None) -> None:
pass

View File

@ -34,7 +34,6 @@ import logging
import typing
from uds.core import module, environment, consts
from uds.core.util import log
from uds.core.ui import gui
# Not imported at runtime, just for type checking
@ -226,14 +225,13 @@ class ServiceProvider(module.Module):
return ret_val
def do_log(self, level: 'types.log.LogLevel', message: str) -> None:
"""
Logs a message with requested level associated with this service
"""
from uds.models import Provider as DBProvider # pylint: disable=import-outside-toplevel
if self.get_uuid():
log.log(DBProvider.objects.get(uuid=self.get_uuid()), level, message, types.log.LogSource.SERVICE)
def do_log(
self,
level: 'types.log.LogLevel',
message: str,
source: 'types.log.LogSource' = types.log.LogSource.SERVICE,
) -> None:
return super().do_log(level, message, source)
def __str__(self) -> str:
"""

View File

@ -38,7 +38,6 @@ import logging
from django.utils.translation import gettext_noop as _
from uds.core.module import Module
from uds.core.ui.user_interface import gui
from uds.core.util import log
from uds.core import types, consts
@ -262,7 +261,7 @@ class Service(Module):
Ideally, availability should be cached for a while, so that we don't have to check it every time.
"""
return True
def allows_errored_userservice_cleanup(self) -> bool:
"""
Returns if this service can clean errored services. This is used to check if a service can be cleaned
@ -463,14 +462,13 @@ class Service(Module):
"""
return False
def do_log(self, level: types.log.LogLevel, message: str) -> None:
"""
Logs a message with requested level associated with this service
"""
from uds.models import Service as DBService # pylint: disable=import-outside-toplevel
if self.get_uuid():
log.log(DBService.objects.get(uuid=self.get_uuid()), level, message, types.log.LogSource.SERVICE)
def do_log(
self,
level: 'types.log.LogLevel',
message: str,
source: 'types.log.LogSource' = types.log.LogSource.SERVICE,
) -> None:
return super().do_log(level, message, source)
@classmethod
def can_assign(cls) -> bool:

View File

@ -85,6 +85,7 @@ class LogSource(enum.StrEnum):
SERVER = 'server'
REST = 'rest'
LOGS = 'logs'
MODULE = 'module'
# Note: Once assigned a value, do not change it, as it will break the log

View File

@ -157,8 +157,8 @@ class UDSLogHandler(logging.handlers.RotatingFileHandler):
msg = LOGLEVEL_PATTERN.sub('', msg)
return msg
def notify(msg: str, identificator: str, logLevel: LogLevel) -> None:
NotificationsManager.manager().notify('log', identificator, logLevel, msg)
def notify(msg: str, identificator: str, loglevel: LogLevel) -> None:
NotificationsManager.manager().notify('log', identificator, loglevel, msg)
if apps.ready and record.levelno >= logging.INFO and not UDSLogHandler.emiting:
try:

View File

@ -47,7 +47,7 @@ from .publication import ProxmoxPublication
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from proxmox import types as prox_types
from .proxmox import types as prox_types
from .provider import ProxmoxProvider
from uds.core.services.generics.dynamic.publication import DynamicPublication
from uds.core.services.generics.dynamic.service import DynamicService