From efaf5fd2c1595fddc09029fa8483a8b7d709c56a Mon Sep 17 00:00:00 2001 From: Thomas Feldmann Date: Sun, 7 Apr 2024 10:57:58 +0200 Subject: [PATCH] add logging --- organize/cli.py | 3 +++ organize/filter.py | 7 ++++--- organize/filters/exif.py | 4 ++-- organize/filters/filecontent.py | 4 ++-- organize/logger.py | 22 ++++++++++++++++++++++ organize/rule.py | 6 ++++-- 6 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 organize/logger.py diff --git a/organize/cli.py b/organize/cli.py index fd23001c..903494d2 100644 --- a/organize/cli.py +++ b/organize/cli.py @@ -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 @@ -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, diff --git a/organize/filter.py b/organize/filter.py index 744c1dea..13198a6e 100644 --- a/organize/filter.py +++ b/organize/filter.py @@ -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 @@ -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 @@ -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 diff --git a/organize/filters/exif.py b/organize/filters/exif.py index 6c8f6bfd..f6ee9873 100644 --- a/organize/filters/exif.py +++ b/organize/filters/exif.py @@ -1,7 +1,6 @@ import collections import fnmatch import json -import logging import os import subprocess from datetime import date, datetime, timedelta @@ -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 @@ -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 diff --git a/organize/filters/filecontent.py b/organize/filters/filecontent.py index 0316caad..165c9282 100644 --- a/organize/filters/filecontent.py +++ b/organize/filters/filecontent.py @@ -1,4 +1,3 @@ -import logging import re import subprocess from functools import lru_cache @@ -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 @@ -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 diff --git a/organize/logger.py b/organize/logger.py new file mode 100644 index 00000000..9c5d366d --- /dev/null +++ b/organize/logger.py @@ -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, + ) + ], + ) diff --git a/organize/rule.py b/organize/rule.py index 12f769e0..519c37d9 100644 --- a/organize/rule.py +++ b/organize/rule.py @@ -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 @@ -268,7 +270,7 @@ def execute( level="error", sender=action, ) - # logging.exception(e) + logger.exception(e) return ReportSummary(errors=1) # normal mode @@ -301,6 +303,6 @@ def execute( level="error", sender=action, ) - # logging.exception(e) + logger.exception(e) summary.errors += 1 return summary