From 87bfad9866037ad53785d0ca7a2a27d206fd68ba Mon Sep 17 00:00:00 2001 From: Pedro Baptista <32106544+pmpbaptista@users.noreply.github.com> Date: Fri, 21 Jun 2024 16:44:32 +0000 Subject: [PATCH] logger: setup logging lib and use it in main script This commit adds a logging library to the project and uses it in the main script. The logging library is configured to log to the console and to a file. The log level is set to INFO. Issue: none --- .gitignore | 1 - docs/getting-started.md | 25 +++++++++++++++++++ folder_replicator/__main__.py | 6 +++++ folder_replicator/lib/__init__.py | 0 folder_replicator/lib/logger.py | 40 +++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 folder_replicator/lib/__init__.py create mode 100644 folder_replicator/lib/logger.py diff --git a/.gitignore b/.gitignore index 8969cd8..159df18 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ diff --git a/docs/getting-started.md b/docs/getting-started.md index 79c30d1..e3b19f9 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -6,3 +6,28 @@ This is a guide to help you get started with the project. It will walk you throu Before you begin, ensure you have met the following requirements: +## Configuration + +To set up the project, follow these steps: + +1. Step 1 +2. Step 2 +3. Step 3 + +### Environment Variables + +The project uses environment variables to configure the application. You can set these variables in a `.env` file in the root of the project. + +```sh +# .env +FR_VERBOSE=true +``` + + +## Running the project + +To run the project, follow these steps: + +1. Step 1 +2. Step 2 +3. Step 3 \ No newline at end of file diff --git a/folder_replicator/__main__.py b/folder_replicator/__main__.py index f4bf32c..624e597 100644 --- a/folder_replicator/__main__.py +++ b/folder_replicator/__main__.py @@ -1,9 +1,13 @@ """ Main module for the folder-replicator package. """ +from folder_replicator.lib import logger as fr_logger from folder_replicator.SyncContext import SyncContext from folder_replicator.SyncStrategyLocal import SyncStrategyLocal +# Get the logger +logger = fr_logger.get_logger() + def main(): """ Main function for the folder-replicator package. @@ -15,6 +19,8 @@ def main(): print("Hello, world!") + logger.info("Hello, world!") + # The client code picks a concrete strategy and passes it to the context. # The client should be aware of the differences between strategies in order # to make the right choice. diff --git a/folder_replicator/lib/__init__.py b/folder_replicator/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/folder_replicator/lib/logger.py b/folder_replicator/lib/logger.py new file mode 100644 index 0000000..0bd20d9 --- /dev/null +++ b/folder_replicator/lib/logger.py @@ -0,0 +1,40 @@ +import logging +import os +import sys + + +DEFAULT_LOG_FILE = "folder_replicator.log" + +def get_logger(name: str=__name__, verbose: bool=False, log_file: str=None) -> logging.Logger: + """ + Set up a logger instance and return it. + args: + name: str - name of the logger + verbose: bool - whether to log debug messages + log_file: str - path to the log file + returns: + logger: logging.Logger - the logger instance + """ + if "FR_VERBOSE" in os.environ: + verbose = True + + logger = logging.getLogger(name) + logger.setLevel(logging.DEBUG if verbose else logging.INFO) + + # logger itself is ensured to be a singleton + # but handlers should only be added once + if len(logger.handlers) == 0: + console_handler = logging.StreamHandler(sys.stderr) + formatter = logging.Formatter( + "%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S %z" + ) + console_handler.setFormatter(formatter) + logger.addHandler(console_handler) + + if log_file := DEFAULT_LOG_FILE: + file_handler = logging.FileHandler(log_file) + + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + + return logger