Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .coverage
Binary file not shown.
8 changes: 0 additions & 8 deletions src/__main__.py

This file was deleted.

22 changes: 22 additions & 0 deletions src/change_me/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Sample doc string."""

import argparse

from loguru import logger

from change_me.definitions import DEFAULT_LOG_LEVEL
from change_me.utils import setup_logger


def main(log_level: str) -> None:
"""Run the pipeline."""
setup_logger(filename="log_file", log_dir=None, log_level=log_level)
logger.info("Hello world!")


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--log-level", default=DEFAULT_LOG_LEVEL, type=str)
args = parser.parse_args()

main(log_level=args.log_level)
35 changes: 34 additions & 1 deletion src/change_me/definitions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
"""Sample doc string."""
"""Common definitions for this module."""

from dataclasses import dataclass
from pathlib import Path

import numpy as np

np.set_printoptions(precision=3, floatmode="fixed", suppress=True)


# --- Directories ---
ROOT_DIR: Path = Path("src").parent
DATA_DIR: Path = ROOT_DIR / "data"
RECORDINGS_DIR: Path = DATA_DIR / "recordings"
LOG_DIR: Path = DATA_DIR / "logs"

# Default encoding
ENCODING: str = "utf-8"

DATE_FORMAT = "%Y-%m-%d_%H-%M-%S"

DUMMY_VARIABLE = "dummy_variable"


@dataclass
class LogLevel:
"""Log levels for loguru."""

debug: str = "DEBUG"
info: str = "INFO"
warning: str = "WARNING"
error: str = "ERROR"
critical: str = "CRITICAL"


DEFAULT_LOG_LEVEL = LogLevel.info
52 changes: 52 additions & 0 deletions src/change_me/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Configure the logger."""

import sys
from datetime import datetime
from pathlib import Path

from loguru import logger

from change_me.definitions import DATE_FORMAT, DEFAULT_LOG_LEVEL, ENCODING, LOG_DIR


def create_timestamped_filepath(suffix: str, output_dir: Path, prefix: str) -> Path:
"""Generate a timestamped filename.

:param suffix: Suffix to append to the timestamped filename.
:param output_dir: Output directory.
:param prefix: Prefix to append to the timestamped filename.
:return: Path to the timestamped filename.
"""
timestamp = datetime.now().strftime(DATE_FORMAT)
filepath = output_dir / f"{prefix}_{timestamp}.{suffix}"
filepath.parent.mkdir(parents=True, exist_ok=True) # create dirs if missing
filepath.touch(exist_ok=True) # create empty file (don't overwrite)
return filepath


def setup_logger(
filename: str,
stderr_level: str = DEFAULT_LOG_LEVEL,
log_level: str = DEFAULT_LOG_LEVEL,
log_dir: Path | None = None,
) -> Path:
"""Configure the logger.

:param filename: Name of the file to create.
:param stderr_level: Logging level to use.
:param log_level: Logging level to use.
:param log_dir: Logging directory to use.
:return: Path to the created logfile.
"""
logger.remove()

if log_dir is None:
log_filepath = LOG_DIR
else:
log_filepath = log_dir
filepath_with_time = create_timestamped_filepath(
output_dir=log_filepath, prefix=filename, suffix="log"
)
logger.add(sys.stderr, level=stderr_level)
logger.add(filepath_with_time, level=log_level, encoding=ENCODING, enqueue=True)
return filepath_with_time
15 changes: 15 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Test the utils module."""

from pathlib import Path
from tempfile import TemporaryDirectory

from change_me.utils import setup_logger


def test_logger_init():
"""Test logger initialization."""
with TemporaryDirectory() as log_dir:
log_dir_path = Path(log_dir)
log_filepath = setup_logger(filename="log_file", log_dir=log_dir_path)
assert Path(log_filepath).exists()
assert not Path(log_filepath).exists()
Loading