Skip to content

Commit

Permalink
feat: improve logging (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Oct 27, 2024
1 parent b3ec960 commit a4647d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 5 additions & 1 deletion memium/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import sys
import time
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Annotated, Optional
Expand Down Expand Up @@ -53,6 +54,7 @@ def cli(
bool, typer.Option(help="Skip all syncing, useful for smoketesting of the interface")
] = False,
):
start_time = datetime.now()
config_dir = input_dir / ".memium"
config_dir.mkdir(exist_ok=True)

Expand Down Expand Up @@ -81,7 +83,9 @@ def cli(
main_fn()

if watch_seconds:
log.info(f"Sync complete, sleeping for {watch_seconds} seconds")
log.info(
f"Sync complete in {(datetime.now() - start_time).total_seconds()} seconds, sleeping for {watch_seconds} seconds"
)
time.sleep(watch_seconds)
main_fn()

Expand Down
18 changes: 14 additions & 4 deletions memium/source/document_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class FileNotRetrievedError(Exception):
path: Path
error: Exception

def __repr__(self) -> str:
return f"{self.path}: Could not retrieve due to '{self.error}'"


class BaseDocumentSource(Protocol):
def get_documents(self) -> Sequence[Document]: ...
Expand Down Expand Up @@ -59,10 +62,17 @@ def _sanitize_to_valid_markdown(self, input_str: str) -> str:

def _get_document_from_file(self, file_path: Path) -> Document | FileNotRetrievedError:
try:
with file_path.open("r", encoding="utf8") as f:
return Document(
content=self._sanitize_to_valid_markdown(f.read()), source_path=file_path
)
try:
contents = file_path.read_text(encoding="utf8")
except Exception as e:
raise Exception(f"Could not read file {file_path}") from e

try:
sanitized = self._sanitize_to_valid_markdown(contents)
except Exception as e:
raise Exception(f"Could not sanitize file {file_path}") from e

return Document(content=sanitized, source_path=file_path)
except Exception as e:
log.warning(f"Could not retrieve {file_path}: {e}")
return FileNotRetrievedError(file_path, e)
Expand Down

0 comments on commit a4647d3

Please sign in to comment.