-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrack_logs.py
More file actions
577 lines (468 loc) · 19.4 KB
/
track_logs.py
File metadata and controls
577 lines (468 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
"""
Track Logs
A log crawler designed to monitor multiple directories for specific file patterns.
It tracks file read positions and modification timestamps to ensure that only
new or modified content is processed across script restarts.
The script utilizes a JSON-based cache to store byte offsets and modification
times. It handles complex edge cases such as log rotation, file truncation
(shrinking), character swaps (same size, different content), and Windows-specific
file permission locks.
How to use the script:
1. Configure the Settings dataclasses at tht top as necessary.
2. Run the script.
3. Within a 'while True' loop in the main function:
- Iterate over 'get_updated_files(config.script.log_dirs, files_cache)'.
- Nested within that, iterate over 'get_new_lines(log_file, files_cache)'.
- Perform your logic (e.g., logging, parsing) on each yielded line.
- Save the 'files_cache' back to the JSON file if 'update_cache' is True.
"""
import json
import logging
import logging.handlers
import os
import platform
import re
import socket
import sys
import tempfile
import time
import typing
from dataclasses import dataclass, field, fields, asdict
from datetime import datetime
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path
logger = logging.getLogger(__name__)
__version__ = "0.0.0" # Major.Minor.Patch
logger = logging.getLogger(__name__)
log_buffer = logging.handlers.MemoryHandler(
capacity=0,
flushLevel=logging.CRITICAL,
target=None,
)
logger.addHandler(log_buffer)
logger.setLevel(logging.DEBUG)
@dataclass
class ScriptSettings:
files_cache_path = Path(r"logs_cache.json")
log_dirs = {
r"Logs 2": r".*log$"
}
polling_interval = 1 # Second(s)
@dataclass
class LogSettings:
mode: typing.Literal["per_run", "latest", "per_day", "single_file", "ConsoleOnly"] = "per_day"
folder: Path = Path(r"logs")
console_level: int = logging.DEBUG
file_level: int = logging.DEBUG
date_format: str = "%Y-%m-%d %H:%M:%S"
message_format: str = "%(asctime)s.%(msecs)03d %(levelname)s [%(funcName)s] - %(message)s"
max_files: int | None = 10
open_log_after_run: bool = False
@dataclass
class InternalSettings:
use_config_file: bool = False
@dataclass
class RuntimeSettings:
pause_on_error: bool = True
always_pause: bool = False
@dataclass
class Config:
script: ScriptSettings = field(default_factory=ScriptSettings)
logs: LogSettings = field(default_factory=LogSettings)
runtime: RuntimeSettings = field(default_factory=RuntimeSettings)
def get_updated_files(target_dirs, cache):
"""
Identifies files that have changed since the last successful cache update.
"""
for directory, pattern in target_dirs.items():
try:
regex = re.compile(pattern, re.IGNORECASE)
path_dir = Path(directory)
if not path_dir.is_dir():
continue
for file_path in path_dir.iterdir():
if file_path.is_file() and regex.search(file_path.name):
key = str(file_path.resolve())
stat = file_path.stat()
entry = cache.get(key, {})
last_offset = entry.get("offset", 0) # Bytes offset from beginning of file
last_mtime = entry.get("mtime", 0) # Unix epoch time
# Detection logic for Appends, Swaps, and Shrinks
if stat.st_mtime > last_mtime or stat.st_size != last_offset:
yield file_path
except (re.error, OSError):
continue
def get_new_lines(file_path, cache):
"""
Yields lines from the file. Updates cache only after successful reading
to prevent synchronization errors during debugging or crashes.
"""
key = str(file_path.resolve())
try:
stat = file_path.stat()
current_size = stat.st_size
current_mtime = stat.st_mtime
entry = cache.get(key, {})
last_offset = entry.get("offset", 0) # Bytes offset from beginning of file
last_mtime = entry.get("mtime", 0) # Unix epoch time
# Determine if we need a full re-read
should_re_read = (
current_size < last_offset or
(current_mtime > last_mtime and current_size == last_offset) or
key not in cache
)
read_offset = 0 if should_re_read else last_offset
new_lines = []
final_offset = last_offset
# 1. Read the file content into a temporary list
# This keeps the 'with open' block as short as possible
with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
f.seek(read_offset)
for line in f:
clean_line = line.strip()
if clean_line:
new_lines.append(clean_line)
# Capture the final position while the file is definitely open
final_offset = f.tell()
# 2. Yield the lines to main()
yield from new_lines
# 3. ONLY update the cache once all lines have been successfully yielded
# This ensures main()'s 'update_cache' logic remains accurate.
cache[key] = {
"offset": final_offset, # Bytes offset from beginning of file
"mtime": current_mtime, # Unix epoch time
}
except (PermissionError, FileNotFoundError, OSError):
# On error, we return without updating the cache, triggering a retry.
pass
def main():
config = Config()
if config.script.files_cache_path.exists():
try:
files_cache = load_cache(config.script.files_cache_path)
if not isinstance(files_cache, dict):
raise TypeError("File cache should be structured as a dictionary")
except Exception as e:
logger.warning("Could not read %s: %s", json.dumps(str(config.script.files_cache_path.as_posix())), e)
files_cache = {}
else:
files_cache = {}
try:
while True:
update_cache = False
for log_file in get_updated_files(config.script.log_dirs, files_cache):
for line in get_new_lines(log_file, files_cache):
update_cache = True
"""Line handling logic here"""
logger.debug("%s: %s", json.dumps(str(log_file.name)), line)
if update_cache:
save_cache(config.script.files_cache_path, files_cache)
time.sleep(config.script.polling_interval)
except KeyboardInterrupt:
logger.debug("Process interrupted by user.")
def read_json_file(file_path: Path) -> dict | list | None:
"""
Safely reads and parses a JSON file.
"""
if not file_path.exists():
logger.warning("File not found: %s", json.dumps(str(file_path)))
raise FileNotFoundError("File not found")
try:
data = json.loads(file_path.read_text(encoding='utf-8'))
logger.info("Successfully read data from %s", json.dumps(str(file_path)))
return data
except json.JSONDecodeError as e:
logger.error("Invalid JSON format in %s: %s", json.dumps(str(file_path)), e)
return None
except Exception as e:
logger.error("Unexpected error reading %s: %s", json.dumps(str(file_path)), e)
return None
def write_json_file(file_path: Path, data: dict | list) -> bool:
"""
Writes data to a JSON file atomically.
"""
file_path = Path(file_path).absolute()
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug("Created %s", json.dumps(str(file_path.parent.as_posix())))
temp_file_path: Path | None = None
try:
with tempfile.NamedTemporaryFile(mode='w', dir=str(file_path.parent), encoding='utf-8', suffix=".tmp", delete=False) as tf:
# Get file path from tempfile object
temp_file_path = Path(tf.name)
json.dump(data, tf, indent=4)
tf.flush()
os.fsync(tf.fileno())
# Atomic swap
temp_file_path.replace(file_path)
logger.info("Successfully saved to %s", json.dumps(str(file_path)))
return True
except (KeyboardInterrupt, SystemExit):
logger.error("Write interrupted for %s. Cleaning up.", json.dumps(str(file_path)))
if temp_file_path and temp_file_path.exists():
temp_file_path.unlink()
raise
except Exception as e:
logger.error("Failed to write to %s: %s", json.dumps(str(file_path)), e)
if temp_file_path and temp_file_path.exists():
temp_file_path.unlink()
return False
def load_config(file_path: Path) -> Config:
config = Config()
needs_sync = False
try:
external_config = read_json_file(file_path)
if not isinstance(external_config, dict):
external_config = {}
needs_sync = True
except FileNotFoundError:
external_config = {}
needs_sync = True
except Exception:
raise
# Merge logic
for section in fields(config):
section_name = section.name
if section_name not in external_config:
needs_sync = True
continue
section_instance = getattr(config, section_name)
json_values = external_config[section_name]
for f in fields(section_instance):
if f.name in json_values:
val = json_values[f.name]
if f.type is Path and isinstance(val, str):
val = Path(val)
setattr(section_instance, f.name, val)
else:
needs_sync = True
# Check for keys in external config that aren't in internal config
internal_field_names = {f.name for f in fields(config)}
if any(k for k in external_config if k not in internal_field_names):
needs_sync = True
if needs_sync:
def path_serializer(obj):
if isinstance(obj, Path):
return str(obj)
raise TypeError(f"Type {type(obj)} not serializable")
# We re-serialize the internal_config (which now has merged data)
# This naturally prunes extra keys because they weren't in the dataclass!
synced_config = json.loads(json.dumps(asdict(config), default=path_serializer))
write_json_file(file_path, synced_config)
return config
def save_config(file_path: Path, config_data: dict | list) -> bool:
"""Alias for write_json_file, specifically for configuration files."""
return write_json_file(file_path, config_data)
def load_cache(file_path: Path) -> dict | list | None:
"""Alias for read_json_file, specifically for cache files."""
return read_json_file(file_path)
def save_cache(file_path: Path, cache_data: dict | list) -> bool:
"""Alias for write_json_file, specifically for cache files."""
return write_json_file(file_path, cache_data)
def read_text_file(file_path: Path, as_list: bool = False) -> str | list[str] | None:
"""
Reads a text file as a single string or a list of lines.
Args:
file_path: Path to the file.
as_list: If True, returns a list of strings (lines). If False, one string.
"""
if not file_path.exists():
logger.warning("File not found: %s", file_path)
return None
try:
if as_list:
# .read_text().splitlines() is cleaner than .readlines()
# as it handles different OS line endings automatically
data = file_path.read_text(encoding='utf-8').splitlines()
else:
data = file_path.read_text(encoding='utf-8')
logger.info("Successfully read text from %s", file_path)
return data
except Exception as e:
logger.error("Unexpected error reading %s: %s", file_path, e)
return None
def write_text_file(file_path: Path, data: str | list[str]) -> bool:
"""
Writes a string or a list of strings to a text file atomically.
"""
file_path = Path(file_path).absolute()
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug("Created %s", json.dumps(str(file_path.parent.as_posix())))
temp_file_path: Path | None = None
try:
with tempfile.NamedTemporaryFile(mode='w', dir=str(file_path.parent), encoding='utf-8', suffix=".tmp", delete=False) as tf:
temp_file_path = Path(tf.name)
if isinstance(data, list):
# Add newlines if they aren't already there to ensure
# list items don't all end up on one line
tf.writelines(line if line.endswith('\n') else f"{line}\n" for line in data)
else:
tf.write(data)
tf.flush()
os.fsync(tf.fileno())
temp_file_path.replace(file_path)
logger.info("Successfully saved text to %s", file_path)
return True
except (KeyboardInterrupt, SystemExit):
logger.error("Write interrupted for %s. Cleaning up.", file_path)
if temp_file_path and temp_file_path.exists():
temp_file_path.unlink()
raise
except Exception as e:
logger.error("Failed to write text to %s: %s", file_path, e)
if temp_file_path and temp_file_path.exists():
temp_file_path.unlink()
return False
def enforce_max_log_count(dir_path: Path, max_count: int, script_name: str) -> None:
"""
Enforce a maximum number of log files for this script.
Deletes the oldest logs based on filename ordering.
Rules:
- Only affects files ending with `.log`
- Only affects logs that contain the script_name
- Sorting is done by filename (lexicographically)
"""
if max_count <= 0:
return
if not dir_path.exists():
return
log_files = [
f for f in dir_path.glob("*.log")
if script_name in f.name
]
if len(log_files) <= max_count:
return
log_files.sort(key=lambda p: p.name)
to_delete = log_files[:-max_count]
for file in to_delete:
try:
file.unlink()
logger.debug("Removed %s", json.dumps(file.absolute().as_posix()))
except Exception:
# Avoid raising during bootstrap
pass
def setup_logging(logger_obj: logging.Logger, log_settings: LogSettings) -> Path | None:
"""Set up file and console logging with flexible modes and rotation."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
day_stamp = datetime.now().strftime("%Y%m%d")
script_name = Path(__file__).stem
pc_name = socket.gethostname()
log_path: Path | None = None
if log_settings.mode != "ConsoleOnly":
log_dir = (log_settings.folder if isinstance(log_settings.folder, Path) else Path(log_settings.folder))
log_dir = log_dir.expanduser().resolve()
if not log_dir.exists():
log_dir.mkdir(parents=True, exist_ok=True)
logger.debug("Created log folder: %s", log_dir.as_posix())
match log_settings.mode:
case "per_run":
log_path = log_dir / f"{timestamp}__{script_name}__{pc_name}.log"
case "latest":
log_path = log_dir / f"latest_{script_name}__{pc_name}.log"
case "per_day":
log_path = log_dir / f"{day_stamp}__{script_name}__{pc_name}.log"
case "single_file":
log_path = log_dir / f"{script_name}__{pc_name}.log"
case _:
log_path = log_dir / f"{timestamp}__{script_name}__{pc_name}.log"
logger_obj.handlers.clear()
logger_obj.setLevel(logging.DEBUG)
# Formatter
formatter = logging.Formatter(
log_settings.message_format,
datefmt=log_settings.date_format,
)
# File handler
file_handler: logging.Handler | None = None
if log_path:
match log_settings.mode:
case "per_day":
file_handler = TimedRotatingFileHandler(
filename=log_path,
when="midnight",
interval=1,
backupCount=log_settings.max_files or 0,
encoding="utf-8",
)
case "single_file" | "latest" | "per_run":
file_mode = "a" if log_settings.mode == "single_file" else "w"
file_handler = logging.FileHandler(
log_path,
mode=file_mode,
encoding="utf-8",
)
if file_handler:
file_handler.setLevel(log_settings.file_level)
file_handler.setFormatter(formatter)
logger_obj.addHandler(file_handler)
# Console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(log_settings.console_level)
console_handler.setFormatter(formatter)
logger_obj.addHandler(console_handler)
# Flush logs buffer from prior to logging initialization
if "log_buffer" in globals():
class _ForwardToLogger(logging.Handler):
def emit(self, record):
logger_obj.handle(record)
forward_handler = _ForwardToLogger()
log_buffer.setTarget(forward_handler)
log_buffer.flush()
log_buffer.close()
# Enforce max log count (except per_day which rotates automatically)
if log_settings.max_files and log_path and log_settings.mode not in ("per_day", "ConsoleOnly"):
try:
enforce_max_log_count(
dir_path=log_path.parent,
max_count=log_settings.max_files,
script_name=script_name,
)
except Exception as e:
logger_obj.debug("Log pruning skipped: %s", e)
return log_path
def bootstrap():
exit_code = 0
log_path = None
script_path = Path(__file__)
logger.info("=" * 80)
config = Config()
config_path = script_path.with_name(f"{script_path.stem}_config.json")
global_settings = InternalSettings()
if global_settings.use_config_file:
config = load_config(config_path)
try:
log_path = setup_logging(logger_obj=logger, log_settings=config.logs)
logger.info("%-10s %s", "Version:", __version__)
logger.info("%-10s %s on %s", "User/Host:", os.getlogin(), socket.gethostname())
logger.info("%-10s %s %s (v%s)", "Platform:", platform.system(), platform.release(), platform.version())
logger.info("%-10s Python %s", "Runtime:", sys.version.split()[0])
logger.info("%-10s %s", "Directory:", Path.cwd().as_posix())
logger.info("%-10s %s", "AppConfig:", config)
main()
except KeyboardInterrupt:
logger.warning("Operation interrupted by user.")
exit_code = 130
except Exception as e:
logger.exception("A fatal error has occurred: %s", e)
exit_code = 1
finally:
for handler in logger.handlers[:]:
handler.close()
logger.removeHandler(handler)
if config.logs.open_log_after_run and log_path and log_path.exists():
try:
match sys.platform:
case plat if plat.startswith("win"): # Windows
os.startfile(log_path)
case "darwin": # macOS
os.system(f'open "{log_path}"')
case _: # Linux / others
os.system(f'xdg-open "{log_path}"')
except Exception as e:
logger.warning("Failed to open log file: %s", e)
if config.runtime.always_pause or (config.runtime.pause_on_error and exit_code != 0):
input("Press Enter to exit...")
return exit_code
if __name__ == "__main__":
sys.exit(bootstrap())