Skip to content

Commit

Permalink
Merge pull request #8 from pmpbaptista/pb/logging
Browse files Browse the repository at this point in the history
logger: setup logging lib and use it in main script
  • Loading branch information
pmpbaptista authored Jun 21, 2024
2 parents 41af4eb + 87bfad9 commit 251201a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down
25 changes: 25 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions folder_replicator/__main__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down
Empty file.
40 changes: 40 additions & 0 deletions folder_replicator/lib/logger.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 251201a

Please sign in to comment.