Skip to content

Commit

Permalink
Merge pull request #41 from djotaku/1.3.0
Browse files Browse the repository at this point in the history
1.3.0 - Logging Improvements
  • Loading branch information
djotaku authored May 28, 2022
2 parents f7022cc + dd10eb1 commit 2722692
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
19 changes: 12 additions & 7 deletions snapintime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = "Eric Mesa"
__version__ = "1.2.2"
__version__ = "1.3.0"
__license__ = "GNU GPL v3.0"
__copyright__: str = "(c) 2014 - 2022 Eric Mesa"
__email__: str = "ericsbinaryworld at gmail dot com"
Expand All @@ -9,9 +9,14 @@

from rich.logging import RichHandler

FORMAT = "%(message)s"
logging.basicConfig(
level="INFO", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)

log = logging.getLogger("rich")
log = logging.getLogger("snapintime")
log.setLevel(logging.INFO)
RICH_FORMAT = logging.Formatter("%(message)s", "[%X]")
LOG_FILE_FORMAT = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler = logging.FileHandler('snap_in_time.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(LOG_FILE_FORMAT)
console_handler = RichHandler()
console_handler.setFormatter(RICH_FORMAT)
log.addHandler(file_handler)
log.addHandler(console_handler)
5 changes: 3 additions & 2 deletions snapintime/create_local_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
from datetime import datetime

from snapintime import log
from snapintime.utils import config as config # type: ignore


Expand Down Expand Up @@ -57,8 +58,8 @@ def main(): # pragma: no cover
our_config = config.import_config()
results = iterate_configs(date_time_for_backup, our_config)
for result in results:
print(f"\nRan {result['Command']} with a return code of {result['Return Code']}")
print(f"Result was: {str(result['Output'])}\n")
log.info(f"Ran: {result['Command']}. Command had a return code of {result['Return Code']}")
log.info(f"Result was: {str(result['Output'])}")


if __name__ == "__main__": # pragma: no cover
Expand Down
6 changes: 3 additions & 3 deletions snapintime/culling.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def cull_last_quarter(config: dict, remote: bool = False) -> list:
for week in last_quarter:
reg_ex_string = ""
for day in week:
reg_ex_string = reg_ex_string + f'({day.strftime("%Y-%m-%d")})|'
reg_ex_string = f"{reg_ex_string}({day.strftime('%Y-%m-%d')})|"
reg_ex_string_minus_final_or = reg_ex_string[:-1]
weekly_reg_ex = re.compile(reg_ex_string_minus_final_or)
subvols_this_week = get_subvols_by_date(subvol.get(location), weekly_reg_ex,
Expand Down Expand Up @@ -233,7 +233,7 @@ def cull_last_year(config: dict, remote: bool = False) -> list:
for quarter in last_year:
reg_ex_string = ""
for day in quarter:
reg_ex_string = reg_ex_string + f'({day.strftime("%Y-%m-%d")})|'
reg_ex_string = f"{reg_ex_string}({day.strftime('%Y-%m-%d')})|"
reg_ex_string_minus_final_or = reg_ex_string[:-1]
quarterly_reg_ex = re.compile(reg_ex_string_minus_final_or)
subvols_this_quarter = get_subvols_by_date(subvol.get("backuplocation"), quarterly_reg_ex)
Expand All @@ -249,7 +249,7 @@ def cull_last_year(config: dict, remote: bool = False) -> list:
def print_output(list_of_lists: list): # pragma: no cover
for directory in list_of_lists:
for result in directory:
print(result)
log.info(result)


def main(): # pragma: no cover
Expand Down
6 changes: 4 additions & 2 deletions snapintime/remote_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from snapintime.utils import config as config # type: ignore

from . import log


def get_remote_subvols(remote_location: str, remote_subvol_dir: str) -> list:
"""Retrieve the remote subvolumes.
Expand Down Expand Up @@ -96,8 +98,8 @@ def main(): # pragma: no cover
our_config = config.import_config()
results = iterate_configs(our_config)
for result in results:
print(f"\nRan {result['Command']} with a return code of {result['Return Code']}")
print(f"Result was: {str(result['Output'])}\n")
log.info(f"\nRan {result['Command']} with a return code of {result['Return Code']}")
log.info(f"Result was: {str(result['Output'])}\n")


if __name__ == "__main__": # pragma: no cover
Expand Down

0 comments on commit 2722692

Please sign in to comment.