Skip to content

Commit

Permalink
Create a new logging & setup functions for standardization
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Apr 28, 2024
1 parent b4cf1b0 commit a4a9940
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
3 changes: 2 additions & 1 deletion comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ def main():


def init():
# TODO(yoland): after this
# TODO(yoland): after this
metadata_manager = MetadataManager()
start_time = time.time()
metadata_manager.scan_dir()
end_time = time.time()
logging.setup_logging()

print(f"scan_dir took {end_time - start_time:.2f} seconds to run")

Expand Down
32 changes: 30 additions & 2 deletions comfy_cli/command/models/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typer
from typing_extensions import Annotated

from comfy_cli import tracking
from comfy_cli import tracking, logging

app = typer.Typer()

Expand Down Expand Up @@ -31,7 +31,7 @@ def download_model(url: str, path: str):
local_filepath = pathlib.Path(path, local_filename)
local_filepath.parent.mkdir(parents=True, exist_ok=True)

print(f"downloading {url} ...")
logging.debug(f"downloading {url} ...")
with httpx.stream("GET", url, follow_redirects=True) as stream:
total = int(stream.headers["Content-Length"])
with open(local_filepath, "wb") as f, tqdm(
Expand All @@ -44,3 +44,31 @@ def download_model(url: str, path: str):
stream.num_bytes_downloaded - num_bytes_downloaded
)
num_bytes_downloaded = stream.num_bytes_downloaded

# def download_model(url: str, path: str):
# # Set up logging to file
# logging.basicConfig(level=logging.INFO, filename='download.log', filemode='w',
# format='%(asctime)s - %(levelname)s - %(message)s')
#
# local_filename = url.split("/")[-1]
# local_filepath = pathlib.Path(path, local_filename)
# local_filepath.parent.mkdir(parents=True, exist_ok=True)
#
# # Log the URL being downloaded
# logging.info(f"Downloading {url} ...")
#
# with httpx.stream("GET", url, follow_redirects=True) as stream:
# total = int(stream.headers["Content-Length"])
# with open(local_filepath, "wb") as f, tqdm(
# total=total, unit_scale=True, unit_divisor=1024, unit="B"
# ) as progress:
# num_bytes_downloaded = stream.num_bytes_downloaded
# for data in stream.iter_bytes():
# f.write(data)
# progress.update(
# stream.num_bytes_downloaded - num_bytes_downloaded
# )
# num_bytes_downloaded = stream.num_bytes_downloaded
#
# # Log the completion of the download
# logging.info(f"Download completed. File saved to {local_filepath}")
35 changes: 35 additions & 0 deletions comfy_cli/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import logging
import os

"""
This module provides logging utilities for the CLI.
Note: we could potentially change the logging library or the way we log messages in the future.
Therefore, it's a good idea to encapsulate logging-related code in a separate module.
"""


def setup_logging():
# TODO: consider supporting different ways of outputting logs
# Note: by default, the log level is set to INFO
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(level=log_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')


def debug(message):
logging.debug(message)


def info(message):
logging.info(message)


def warning(message):
logging.warning(message)


def error(message):
logging.error(message)
# TODO: consider tracking errors to Mixpanel as well.

0 comments on commit a4a9940

Please sign in to comment.