Skip to content

Commit

Permalink
feat: auto default logs for different use cases: testing, local dev, …
Browse files Browse the repository at this point in the history
…and to /tmp on permission failure
  • Loading branch information
danellecline committed Jul 19, 2024
1 parent cfcb581 commit 0c57d45
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions sdcat/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sdcat.detect.commands import run_detect


create_logger_file(log_path=Path.home() / 'sdcat' / 'logs')
create_logger_file("sdcat")
default_data_path = Path(__file__).parent / 'testdata'
default_model = 'MBARI/megabenthic'

Expand All @@ -35,7 +35,7 @@ def cli():
@cli.group(name="cluster")
def cli_cluster():
"""
Commands related to converting data
Commands related to clustering images
"""
pass

Expand Down
2 changes: 1 addition & 1 deletion sdcat/detect/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def run_detect(show: bool, image_dir: str, save_dir: str, model: str,
if len(allowable_classes) > 0:
allowable_classes = allowable_classes.split(',')

create_logger_file(Path.cwd(), 'detect')
create_logger_file('detect')

if not skip_sahi:
if model == 'yolov8s':
Expand Down
50 changes: 39 additions & 11 deletions sdcat/logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,45 @@
# Description: Logs to both a file and the console. The file is named with the current date.

import logging
import os
from pathlib import Path
from datetime import datetime as dt
from datetime import datetime as dt, timezone

LOGGER_NAME = "sdcat"
DEBUG = True


class _Singleton(type):
""" A metaclass that creates a Singleton base class when called. """
_instances = {}
"""A metaclass that creates a Singleton base class when called."""

_instances = {} # type: ignore

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class Singleton(_Singleton('SingletonMeta', (object,), {})): pass
class Singleton(_Singleton("SingletonMeta", (object,), {})):
pass


class CustomLogger(Singleton):
logger = None
summary_df = None
output_path = Path.cwd()
output_path = Path.home() / "sdcat" / "logs"

def __init__(self, output_path: Path = Path.cwd(), output_prefix: str = "uav"):
def __init__(self, output_path: Path = Path.cwd(), output_prefix: str = LOGGER_NAME):
"""
Initialize the logger
"""
self.logger = logging.getLogger(LOGGER_NAME)
self.logger.setLevel(logging.INFO)
self.logger.setLevel(logging.DEBUG)
self.output_path = output_path
self.output_path.mkdir(parents=True, exist_ok=True)
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")

# default log file date to today
now = dt.utcnow()
now = dt.now(timezone.utc)

# log to file
self.log_filename = output_path / f"{output_prefix}_{now:%Y%m%d}.log"
Expand All @@ -47,19 +50,44 @@ def __init__(self, output_path: Path = Path.cwd(), output_prefix: str = "uav"):
handler.setLevel(logging.INFO)
self.logger.addHandler(handler)

# also log to console
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
self.logger.addHandler(console)

self.logger.info(f"Logging to {self.log_filename}")

def loggers(self) -> logging.Logger:
return self.logger

def create_logger_file(log_path: Path, prefix: str=""):

def create_logger_file(prefix: str = "sdcat"):
"""
Create a logger file
:param log_path: Path to the log file
"""
ENVIRONMENT = str(os.getenv("ENVIRONMENT"))
if ENVIRONMENT and ENVIRONMENT.upper() == "TESTING":
log_path = Path("logs")
else:
log_path = Path.home() / "sdcat" / "logs"
# Check if can write to the log path, and if not revert to system temp
try:
log_path.mkdir(parents=True, exist_ok=True)
test_file = log_path / "test.txt"
with open(test_file, "w") as f:
f.write("test")
test_file.unlink()
except PermissionError:
import tempfile

temp_dir = tempfile.gettempdir()
log_path = Path(temp_dir) / "sdcat" / "logs"

# create the log directory if it doesn't exist
log_path.mkdir(parents=True, exist_ok=True)
return CustomLogger(log_path, f'{LOGGER_NAME}{prefix}')
return CustomLogger(log_path, prefix)


def custom_logger() -> logging.Logger:
Expand Down

0 comments on commit 0c57d45

Please sign in to comment.