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

Logging #414

Merged
merged 2 commits into from
Dec 18, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.9.35] - 2023-12-18
### Added
- logging callback

## [1.9.34] - 2023-12-11
### Added
- Profiles: extra_backtesting_time_frames
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OctoBot-Commons [1.9.34](https://github.com/Drakkar-Software/OctoBot-Commons/blob/master/CHANGELOG.md)
# OctoBot-Commons [1.9.35](https://github.com/Drakkar-Software/OctoBot-Commons/blob/master/CHANGELOG.md)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b31f3ab3511744a5a5ca6b9bb48e77bb)](https://app.codacy.com/gh/Drakkar-Software/OctoBot-Commons?utm_source=github.com&utm_medium=referral&utm_content=Drakkar-Software/OctoBot-Commons&utm_campaign=Badge_Grade_Dashboard)
[![PyPI](https://img.shields.io/pypi/v/OctoBot-Commons.svg)](https://pypi.python.org/pypi/OctoBot-Commons/)
[![Coverage Status](https://coveralls.io/repos/github/Drakkar-Software/OctoBot-Commons/badge.svg?branch=master)](https://coveralls.io/github/Drakkar-Software/OctoBot-Commons?branch=master)
Expand Down
2 changes: 1 addition & 1 deletion octobot_commons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# License along with this library.

PROJECT_NAME = "OctoBot-Commons"
VERSION = "1.9.34" # major.minor.revision
VERSION = "1.9.35" # major.minor.revision

MARKET_SEPARATOR = "/"
SETTLEMENT_ASSET_SEPARATOR = ":"
Expand Down
2 changes: 2 additions & 0 deletions octobot_commons/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get_errors_count,
reset_errors_count,
register_error_notifier,
register_log_callback,
set_enable_web_interface_logs,
)

Expand All @@ -60,5 +61,6 @@
"get_errors_count",
"reset_errors_count",
"register_error_notifier",
"register_log_callback",
"set_enable_web_interface_logs",
]
49 changes: 38 additions & 11 deletions octobot_commons/logging/logging_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# License along with this library.
import contextlib
import logging
import typing

import octobot_commons.constants as constants
import octobot_commons.timestamp_util as timestamp_util
Expand Down Expand Up @@ -46,6 +47,7 @@ def _default_callback(*_, **__):


_ERROR_CALLBACK = _default_callback
_LOG_CALLBACK: typing.Union[None, typing.Callable[[str], str]] = None


def set_global_logger_level(level, handler_levels=None) -> None:
Expand Down Expand Up @@ -171,47 +173,51 @@ def __init__(self, logger_name):
self.logger_name = logger_name
self.logger = logging.getLogger(logger_name)

def debug(self, message, *args, **kwargs) -> None:
def debug(self, message: str, *args, **kwargs) -> None:
"""
Called for a debug log
:param message: the log message
"""
message = self._process_log_callback(message)
self.logger.debug(message, *args, **kwargs)
self._publish_log_if_necessary(message, logging.DEBUG)

def info(self, message, *args, **kwargs) -> None:
def info(self, message: str, *args, **kwargs) -> None:
"""
Called for an info log
:param message: the log message
"""
message = self._process_log_callback(message)
self.logger.info(message, *args, **kwargs)
self._publish_log_if_necessary(message, logging.INFO)

def warning(self, message, *args, **kwargs) -> None:
def warning(self, message: str, *args, **kwargs) -> None:
"""
Called for a warning log
:param message: the log message
"""
message = self._process_log_callback(message)
self.logger.warning(message, *args, **kwargs)
self._publish_log_if_necessary(message, logging.WARNING)

def error(self, message, *args, skip_post_callback=False, **kwargs) -> None:
def error(self, message: str, *args, skip_post_callback=False, **kwargs) -> None:
"""
Called for an error log
:param message: the log message
:param skip_post_callback: when True, the error callback wont be called
"""
message = self._process_log_callback(message)
self.logger.error(message, *args, **kwargs)
self._publish_log_if_necessary(message, logging.ERROR)
self._post_callback_if_necessary(None, message, skip_post_callback)

def exception(
self,
exception,
publish_error_if_necessary=True,
error_message=None,
include_exception_name=True,
skip_post_callback=False,
exception: Exception,
publish_error_if_necessary: bool = True,
error_message: str = None,
include_exception_name: bool = True,
skip_post_callback: bool = False,
**kwargs,
) -> None:
"""
Expand All @@ -223,6 +229,11 @@ def exception(
:param skip_post_callback: when True, the error callback wont be called
"""
extra = kwargs.get("extra", {})
error_message = (
self._process_log_callback(error_message)
if error_message
else error_message
)
extra[constants.EXCEPTION_DESC] = error_message
self.logger.exception(exception, extra=extra, **kwargs)
if publish_error_if_necessary:
Expand All @@ -240,19 +251,21 @@ def exception(
)
self._post_callback_if_necessary(exception, error_message, skip_post_callback)

def critical(self, message, *args, **kwargs) -> None:
def critical(self, message: str, *args, **kwargs) -> None:
"""
Called for a critical log
:param message: the log message
"""
message = self._process_log_callback(message)
self.logger.critical(message, *args, **kwargs)
self._publish_log_if_necessary(message, logging.CRITICAL)

def fatal(self, message, *args, **kwargs) -> None:
def fatal(self, message: str, *args, **kwargs) -> None:
"""
Called for a fatal log
:param message: the log message
"""
message = self._process_log_callback(message)
self.logger.fatal(message, *args, **kwargs)
self._publish_log_if_necessary(message, logging.FATAL)

Expand All @@ -263,6 +276,11 @@ def disable(self, disabled):
"""
self.logger.disabled = disabled

def _process_log_callback(self, message: str) -> str:
if _LOG_CALLBACK is None:
return message
return _LOG_CALLBACK(message)

def _publish_log_if_necessary(self, message, level) -> None:
"""
Publish the log message if necessary
Expand Down Expand Up @@ -307,6 +325,15 @@ def _post_callback_if_necessary(exception, error_message, skip_post_callback):
_ERROR_CALLBACK(exception, error_message)


def register_log_callback(callback: typing.Union[None, typing.Callable[[str], str]]):
"""
:param callback: the callback to be called upon any log of any level
Register callback as the _LOG_CALLBACK
"""
global _LOG_CALLBACK
_LOG_CALLBACK = callback


def get_backtesting_errors_count() -> int:
"""
Get backtesting errors count
Expand Down