|
19 | 19 | import string |
20 | 20 | import typing as ty |
21 | 21 | from contextlib import contextmanager |
22 | | -from datetime import datetime |
23 | 22 | from logging import FileHandler |
24 | 23 | from pathlib import Path |
25 | 24 |
|
@@ -74,40 +73,44 @@ def logfile_path(name_prefix: str) -> Path: |
74 | 73 | folder.mkdir(parents=True, exist_ok=True) |
75 | 74 | # Generate a random suffix so multiple instances of dvr-scan don't try to write to the same |
76 | 75 | # log file. |
77 | | - random_suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8)) |
78 | | - return folder / Path(f"{name_prefix}-{datetime.now():%Y%m%d-%H%M%S}-{random_suffix}.log") |
| 76 | + random_suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=12)) |
| 77 | + return folder / Path(f"{name_prefix}.{random_suffix}.log") |
79 | 78 |
|
80 | 79 |
|
81 | | -def prune_log_files(log_folder: Path, max_files: int, name_prefix: str): |
| 80 | +def prune_log_files(log_folder: Path, max_log_files: int, name_prefix: str): |
82 | 81 | """Prune log files, keeping the latest `max_files` number of logs.""" |
83 | 82 | # Prune oldest log files if we have too many. |
84 | | - if max_files > 0: |
| 83 | + if max_log_files > 0: |
85 | 84 | # We find all DVR-Scan log files by globbing, then remove the oldest ones. |
86 | | - log_file_pattern = str(log_folder / f"{name_prefix}-*.log") |
| 85 | + log_file_pattern = str(log_folder / f"{name_prefix}.*.log") |
87 | 86 | log_files = list(glob.glob(log_file_pattern)) |
88 | | - if len(log_files) > max_files: |
| 87 | + if len(log_files) > max_log_files: |
| 88 | + logger.debug( |
| 89 | + "pruning oldest logs:" |
| 90 | + f" max-log-files = {max_log_files}, len(log_files) = {len(log_files)}" |
| 91 | + ) |
89 | 92 | log_files.sort(key=os.path.getmtime) |
90 | | - for i in range(len(log_files) - max_files): |
91 | | - logger.debug("Removing old log file: %s", log_files[i]) |
| 93 | + for i in range(len(log_files) - max_log_files): |
92 | 94 | try: |
93 | 95 | os.remove(log_files[i]) |
| 96 | + logger.debug("removed log: %s", log_files[i]) |
94 | 97 | except PermissionError: |
95 | 98 | logger.warning( |
96 | | - "Failed to remove old log file: %s. It might be in use by another " |
97 | | - "DVR-Scan process.", |
| 99 | + "Failed to remove log file: %s. It might be in use by another process. " |
| 100 | + "Try raising `max-log-files` if this is the case.", |
98 | 101 | log_files[i], |
99 | 102 | ) |
100 | 103 |
|
101 | 104 |
|
102 | | -def setup_logger(logfile_path: Path, max_files: int, name_prefix: str): |
| 105 | +def setup_logger(logfile_path: Path, max_log_files: int, name_prefix: str): |
103 | 106 | """Initialize rolling debug logger.""" |
104 | | - prune_log_files(logfile_path.parent, max_files, name_prefix) |
| 107 | + prune_log_files(logfile_path.parent, max_log_files, name_prefix) |
| 108 | + logger.debug(f"writing logs to {logfile_path}") |
105 | 109 | handler = FileHandler(str(logfile_path)) |
106 | 110 | handler.setLevel(logging.DEBUG) |
107 | 111 | handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT_ROLLING_LOGS)) |
108 | 112 | # *WARNING*: This log message must come before we attach the handler otherwise it will get |
109 | 113 | # written to the log file each time. |
110 | | - logger.debug(f"writing logs to {logfile_path} (max_files: {max_files})") |
111 | 114 | attach_log_handler(handler) |
112 | 115 |
|
113 | 116 |
|
|
0 commit comments