diff --git a/.github/workflows/ftp-setup.yaml b/.github/workflows/ftp-setup.yaml index 483ed17..5cb969a 100644 --- a/.github/workflows/ftp-setup.yaml +++ b/.github/workflows/ftp-setup.yaml @@ -5,10 +5,10 @@ name: FTP Setup on: workflow_call: workflow_dispatch: - workflow_run: - branches: [main] - types: [completed] - workflows: [CI] + # workflow_run: + # branches: [main] + # types: [completed] + # workflows: [CI] env: dir: tmp diff --git a/docs/temp.md b/docs/temp.md deleted file mode 100644 index 1b933b5..0000000 --- a/docs/temp.md +++ /dev/null @@ -1,4 +0,0 @@ -# PyPackage - -::: src.temp -::: tests.test_temp diff --git a/docs/utilities.md b/docs/utilities.md new file mode 100644 index 0000000..5817cb4 --- /dev/null +++ b/docs/utilities.md @@ -0,0 +1,3 @@ +# PyPackage + +::: src.utilities diff --git a/mkdocs.yaml b/mkdocs.yaml index 347a8b2..1a06ca0 100644 --- a/mkdocs.yaml +++ b/mkdocs.yaml @@ -8,9 +8,7 @@ copyright: MIT License - Copyright (c) 2023 Sébastien Menard nav: - Home: index.md - - Temp: temp.md - - Temp: temp.md - - Temp: temp.md + - Utilities: utilities.md plugins: - mkdocstrings # generate from docstrings diff --git a/src/utilities/__init__.py b/src/utilities/__init__.py new file mode 100644 index 0000000..0db3994 --- /dev/null +++ b/src/utilities/__init__.py @@ -0,0 +1 @@ +"""Package Utilities.""" diff --git a/src/utilities/logger.py b/src/utilities/logger.py new file mode 100644 index 0000000..ffc227b --- /dev/null +++ b/src/utilities/logger.py @@ -0,0 +1,120 @@ +"""Module Logger.""" + + +from __future__ import annotations + +import logging +import sys + + +class Logger: + """Interface representing logger utilities.""" + + def __init__( + self: Logger, + *, + debugging: bool = False, + tracing: bool = False, + newline: bool = True, + ) -> None: + """Initiate a logger. + + Parameters + ---------- + debugging : bool, optional + Set logging level to DEBUG otherwise INFO, by default False + tracing : bool, optional + Trace method calls when using _trace, by default False + newline : bool, optional + Add a new line between logs, by default True + """ + formatter = "%(name)s:%(levelname)s => %(msg)s" + "\n" if newline else "" + + handler = logging.StreamHandler() + handler.setFormatter(fmt=logging.Formatter(fmt=formatter)) + + logger = logging.getLogger(name="LOG") + logger.addHandler(hdlr=handler) + logger.setLevel(level=logging.DEBUG if debugging else logging.INFO) + + self.logger = logger + self.debugging = debugging + self.tracing = tracing + self.newline = newline + + def debug_(self: Logger, msg: str) -> None: + """Log a message with severity DEBUG. + + Parameters + ---------- + msg : str + The message to display. + """ + self.logger.debug(msg=msg) + + def warn_(self: Logger, msg: str) -> None: + """Log a message with severity WARN. + + Parameters + ---------- + msg : str + The message to display. + """ + self.logger.warning(msg=msg) + + def info_(self: Logger, msg: str) -> None: + """Log a message with severity INFO. + + Parameters + ---------- + msg : str + The message to display. + """ + self.logger.info(msg=msg) + + def error_(self: Logger, msg: str) -> None: + """Log a message with severity ERROR. + + Parameters + ---------- + msg : str + The message to display. + """ + self.logger.error(msg=msg) + + def critical_(self: Logger, msg: str) -> None: + """Log a message with severity CRITICAL. + + Parameters + ---------- + msg : str + The message to display. + """ + self.logger.critical(msg=msg) + + def trace_(self: Logger, msg: str | None = None) -> None: + """Log a message with severity 'DEBUG' to trace methods call. + + Parameters + ---------- + msg : str | None, optional + The message to display, by default None. + """ + if not self.tracing: + return + + frame = sys._getframe(1) # noqa: SLF001 + + message = "TRACE" + + # Trace class name if method is from a class + if "self" in frame.f_locals: + message += f" - CLASS {frame.f_locals['self'].__class__.__name__}" + + # Trace method name + message += f" - METHOD {frame.f_code.co_name}" + + if msg is not None: + message += f" - {msg}" + + self.info_(msg=f"{message}")