Skip to content

Commit

Permalink
we use threading.local to store the recursion protection flag (for Py…
Browse files Browse the repository at this point in the history
…thon 3.13+)

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 committed Sep 5, 2024
1 parent 30cff16 commit 05cf4aa
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions nc_py_api/ex_app/logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Transparent logging support to store logs in the nextcloud.log."""

import logging
import threading

from ..nextcloud import NextcloudApp
from .defs import LogLvl
Expand All @@ -18,30 +19,29 @@ def _python_loglvl_to_nextcloud(levelno: int) -> LogLvl:
return LogLvl.FATAL


class _NextcloudStorageHandler(logging.Handler):
class _NextcloudLogsHandler(logging.Handler):
def __init__(self):
super().__init__()
self.lock_flag = False

def emit(self, record):
if self.lock_flag:
if threading.local().__dict__.get("nc_py_api.loghandler", False):
return

try:
self.lock_flag = True
threading.local().__dict__["nc_py_api.loghandler"] = True
log_entry = self.format(record)
log_level = record.levelno
NextcloudApp().log(_python_loglvl_to_nextcloud(log_level), log_entry, fast_send=True)
except Exception: # noqa pylint: disable=broad-exception-caught
self.handleError(record)
finally:
self.lock_flag = False
threading.local().__dict__["nc_py_api.loghandler"] = False


def setup_nextcloud_logging(logger_name: str | None = None, logging_level: int = logging.DEBUG):
"""Function to easily send all or selected log entries to Nextcloud."""
logger = logging.getLogger(logger_name)
nextcloud_handler = _NextcloudStorageHandler()
nextcloud_handler = _NextcloudLogsHandler()
nextcloud_handler.setLevel(logging_level)
logger.addHandler(nextcloud_handler)
return nextcloud_handler

0 comments on commit 05cf4aa

Please sign in to comment.