Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tfeldmann committed Apr 7, 2024
1 parent 20368c0 commit efaf5fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions organize/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
find_config,
list_configs,
)
from organize.logger import enable_logfile
from organize.output import JSONL, Default, Output
from organize.utils import escape

Expand Down Expand Up @@ -248,6 +249,8 @@ def either_stdin_or_config(self):


def cli(argv: Union[list[str], str, None] = None) -> None:
enable_logfile()
assert __doc__ is not None
parsed_args = docopt(
__doc__,
argv=argv,
Expand Down
7 changes: 4 additions & 3 deletions organize/filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, NamedTuple, Protocol, runtime_checkable

from organize.logger import logger

if TYPE_CHECKING:
from .output import Output
from .resource import Resource
Expand Down Expand Up @@ -54,7 +55,7 @@ def pipeline(self, res: Resource, output: Output) -> bool:
return False
except Exception as e:
output.msg(res=res, level="error", msg=str(e), sender=filter)
logging.exception(e)
logger.exception(e)
return False
return True

Expand All @@ -72,5 +73,5 @@ def pipeline(self, res: Resource, output: Output) -> bool:
result = True
except Exception as e:
output.msg(res=res, level="error", msg=str(e), sender=filter)
logging.exception(e)
logger.exception(e)
return result
4 changes: 2 additions & 2 deletions organize/filters/exif.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import collections
import fnmatch
import json
import logging
import os
import subprocess
from datetime import date, datetime, timedelta
Expand All @@ -14,6 +13,7 @@
from rich import print

from organize.filter import FilterConfig
from organize.logger import logger
from organize.output import Output
from organize.resource import Resource

Expand All @@ -40,7 +40,7 @@ def exiftool_available() -> bool:
)
return True
except subprocess.CalledProcessError:
logging.warning("exiftool not available. Falling back to exifread library.")
logger.warning("exiftool not available. Falling back to exifread library.")
return False


Expand Down
4 changes: 2 additions & 2 deletions organize/filters/filecontent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import re
import subprocess
from functools import lru_cache
Expand All @@ -9,6 +8,7 @@
from pydantic.dataclasses import dataclass

from organize.filter import FilterConfig
from organize.logger import logger
from organize.output import Output
from organize.resource import Resource

Expand Down Expand Up @@ -48,7 +48,7 @@ def _pdftotext_available() -> bool:
)
return True
except subprocess.CalledProcessError:
logging.warning("pdftotext not available. Falling back to pdfminer library.")
logger.warning("pdftotext not available. Falling back to pdfminer library.")
return False


Expand Down
22 changes: 22 additions & 0 deletions organize/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path

from platformdirs import user_log_dir

logger = logging.getLogger(name="organize")


def enable_logfile():
logdir = Path(user_log_dir(appname="organize", ensure_exists=True))
logging.basicConfig(
level=logging.WARNING,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
RotatingFileHandler(
filename=logdir / "organize-errors.log",
backupCount=5,
maxBytes=5_000_000,
)
],
)
6 changes: 4 additions & 2 deletions organize/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator

from organize.logger import logger

from .action import Action
from .filter import All, Any, Filter, HasFilterPipeline, Not
from .location import Location
Expand Down Expand Up @@ -268,7 +270,7 @@ def execute(
level="error",
sender=action,
)
# logging.exception(e)
logger.exception(e)
return ReportSummary(errors=1)

# normal mode
Expand Down Expand Up @@ -301,6 +303,6 @@ def execute(
level="error",
sender=action,
)
# logging.exception(e)
logger.exception(e)
summary.errors += 1
return summary

0 comments on commit efaf5fd

Please sign in to comment.