-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NextcloudApp:
setup_nextcloud_logging
function for transparent logg…
…ing (#294) Usage: ```python3 import logging from pathlib import Path from os import environ import nc_py_api environ["APP_ID"] = "nc_py_api" environ["APP_VERSION"] = "1.0.0" environ["APP_SECRET"] = "12345" environ["NEXTCLOUD_URL"] = "http://nextcloud.local" if __name__ == "__main__": nc_app = nc_py_api.NextcloudApp() logging.basicConfig( level=logging.INFO, format="%(asctime)s: [%(funcName)s]:%(levelname)s: %(message)s", datefmt="%H:%M:%S", ) logging.getLogger("httpx").setLevel(logging.ERROR) # not needed, but better to hide spam to console nc_py_api.ex_app.setup_nextcloud_logging() # setup logging handler in one line of code logging.fatal("Fatal test") logging.error("Error test") logging.warning("Warning test") logging.info("Info test") logging.debug("Debug test") logging.fatal("Fatal test2") try: a = 0 b = z except Exception as e: logging.exception("Exception test") ``` ### setup_nextcloud_logging ```python3 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.setLevel(logging_level) logger.addHandler(nextcloud_handler) return nextcloud_handler ``` --------- Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
- Loading branch information
Showing
5 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Transparent logging support to store logs in the nextcloud.log.""" | ||
|
||
import logging | ||
import threading | ||
|
||
from ..nextcloud import NextcloudApp | ||
from .defs import LogLvl | ||
|
||
LOGLVL_MAP = { | ||
logging.NOTSET: LogLvl.DEBUG, | ||
logging.DEBUG: LogLvl.DEBUG, | ||
logging.INFO: LogLvl.INFO, | ||
logging.WARNING: LogLvl.WARNING, | ||
logging.ERROR: LogLvl.ERROR, | ||
logging.CRITICAL: LogLvl.FATAL, | ||
} | ||
|
||
THREAD_LOCAL = threading.local() | ||
|
||
|
||
class _NextcloudLogsHandler(logging.Handler): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def emit(self, record): | ||
if THREAD_LOCAL.__dict__.get("nc_py_api.loghandler", False): | ||
return | ||
|
||
try: | ||
THREAD_LOCAL.__dict__["nc_py_api.loghandler"] = True | ||
log_entry = self.format(record) | ||
log_level = record.levelno | ||
NextcloudApp().log(LOGLVL_MAP.get(log_level, LogLvl.FATAL), log_entry, fast_send=True) | ||
except Exception: # noqa pylint: disable=broad-exception-caught | ||
self.handleError(record) | ||
finally: | ||
THREAD_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 = _NextcloudLogsHandler() | ||
nextcloud_handler.setLevel(logging_level) | ||
logger.addHandler(nextcloud_handler) | ||
return nextcloud_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters