mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 21:12:16 +00:00
Hotfix/logs on account (#5417)
* small adjustments to logs * logs on account commands * fixing tests * remove outdated test
This commit is contained in:
@@ -29,7 +29,6 @@ class PosthogHandler(logging.Handler):
|
||||
|
||||
log_startup = "log_startup"
|
||||
log_cmd = "log_cmd"
|
||||
log_sdk = "log_sdk"
|
||||
log_error = "log_error"
|
||||
log_warning = "log_warning"
|
||||
|
||||
@@ -60,10 +59,6 @@ class PosthogHandler(logging.Handler):
|
||||
for log in re.findall(log_regex, log_info):
|
||||
log_dict[log[0]] = json.loads(log[1])
|
||||
|
||||
sdk_regex = r"({\"INPUT\":.*})"
|
||||
if sdk_dict := re.findall(sdk_regex, log_info):
|
||||
log_dict["SDK"] = json.loads(sdk_dict[0])
|
||||
|
||||
return log_dict
|
||||
|
||||
def send(self, record: logging.LogRecord):
|
||||
|
||||
@@ -134,7 +134,7 @@ class LoggingService(metaclass=SingletonMeta):
|
||||
|
||||
return handlers_manager
|
||||
|
||||
def _log_startup(self) -> None:
|
||||
def _log_startup(self, route: Optional[str] = None) -> None:
|
||||
"""
|
||||
Log startup information.
|
||||
"""
|
||||
@@ -156,6 +156,7 @@ class LoggingService(metaclass=SingletonMeta):
|
||||
"STARTUP: %s ",
|
||||
json.dumps(
|
||||
{
|
||||
"route": route,
|
||||
"PREFERENCES": self._user_settings.preferences,
|
||||
"KEYS": check_credentials_defined(
|
||||
self._user_settings.credentials.dict()
|
||||
@@ -194,6 +195,8 @@ class LoggingService(metaclass=SingletonMeta):
|
||||
Callable representing the executed function.
|
||||
kwargs : Dict[str, Any]
|
||||
Keyword arguments passed to the function.
|
||||
exec_info : Optional[Tuple[Type[BaseException], BaseException, Optional[TracebackType]]], optional
|
||||
Exception information, by default None
|
||||
"""
|
||||
self._user_settings = user_settings
|
||||
self._system_settings = system_settings
|
||||
@@ -204,7 +207,7 @@ class LoggingService(metaclass=SingletonMeta):
|
||||
self._handlers_manager.update_handlers(self._logging_settings)
|
||||
|
||||
if "login" in route:
|
||||
self._log_startup()
|
||||
self._log_startup(route)
|
||||
else:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
# pylint: disable=W0212:protected-access
|
||||
import json
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
from sys import exc_info
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from openbb_core.app.logs.logging_service import LoggingService
|
||||
from openbb_core.app.model.abstract.error import OpenBBError
|
||||
from openbb_core.app.model.hub.hub_session import HubSession
|
||||
from openbb_core.app.model.user_settings import UserSettings
|
||||
@@ -29,6 +32,32 @@ class Account:
|
||||
def __repr__(self) -> str:
|
||||
return self.__doc__ or ""
|
||||
|
||||
def _log_account_command(func): # pylint: disable=E0213
|
||||
@wraps(func)
|
||||
def wrapped(self, *args, **kwargs):
|
||||
try:
|
||||
result = func(self, *args, **kwargs) # pylint: disable=E1102
|
||||
except Exception as e:
|
||||
raise OpenBBError(e) from e
|
||||
finally:
|
||||
user_settings = self._base_app._command_runner.user_settings
|
||||
system_settings = self._base_app._command_runner.system_settings
|
||||
ls = LoggingService(
|
||||
user_settings=user_settings, system_settings=system_settings
|
||||
)
|
||||
ls.log(
|
||||
user_settings=user_settings,
|
||||
system_settings=system_settings,
|
||||
route=f"/account/{func.__name__}", # pylint: disable=E1101
|
||||
func=func,
|
||||
kwargs={}, # don't want any credentials being logged by accident
|
||||
exec_info=exc_info(),
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
return wrapped
|
||||
|
||||
def _create_hub_service(
|
||||
self,
|
||||
email: Optional[str] = None,
|
||||
@@ -51,6 +80,7 @@ class Account:
|
||||
hs.connect(email, password, pat)
|
||||
return hs
|
||||
|
||||
@_log_account_command
|
||||
def login(
|
||||
self,
|
||||
email: Optional[str] = None,
|
||||
@@ -91,6 +121,7 @@ class Account:
|
||||
|
||||
return self._base_app._command_runner.user_settings
|
||||
|
||||
@_log_account_command
|
||||
def save(self) -> UserSettings:
|
||||
"""Save user settings.
|
||||
|
||||
@@ -109,6 +140,7 @@ class Account:
|
||||
hs.push(self._base_app._command_runner.user_settings)
|
||||
return self._base_app._command_runner.user_settings
|
||||
|
||||
@_log_account_command
|
||||
def refresh(self) -> UserSettings:
|
||||
"""Refresh user settings.
|
||||
|
||||
@@ -130,6 +162,7 @@ class Account:
|
||||
self._base_app._command_runner.user_settings = updated
|
||||
return self._base_app._command_runner.user_settings
|
||||
|
||||
@_log_account_command
|
||||
def logout(self) -> UserSettings:
|
||||
"""Logout from hub.
|
||||
|
||||
|
||||
@@ -162,12 +162,6 @@ def test_emit_calls_handleError_when_send_raises_exception_of_another_type(handl
|
||||
'STARTUP: {"status": "success"}',
|
||||
{"STARTUP": {"status": "success"}},
|
||||
),
|
||||
(
|
||||
'{"INPUT": {"something_something":"something_something"}}',
|
||||
{
|
||||
"SDK": {"INPUT": {"something_something": "something_something"}},
|
||||
},
|
||||
),
|
||||
(
|
||||
'CMD: {"path": "/stocks/", "known_cmd": "load", "other_args": "aapl", "input": "load aapl"}',
|
||||
{
|
||||
|
||||
@@ -86,9 +86,10 @@ def test_log_startup(logging_service):
|
||||
)
|
||||
logging_service._system_settings = "your_system_settings"
|
||||
|
||||
logging_service._log_startup()
|
||||
logging_service._log_startup(route="test_route")
|
||||
|
||||
expected_log_data = {
|
||||
"route": "test_route",
|
||||
"PREFERENCES": "your_preferences",
|
||||
"KEYS": {"username": "defined", "password": "defined"},
|
||||
"SYSTEM": "your_system_settings",
|
||||
|
||||
Reference in New Issue
Block a user