diff --git a/.gitignore b/.gitignore index cff5543..2bb5881 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env __pycache__/ *.pyc +logs/ diff --git a/run.py b/run.py index c6355e1..ab5d42b 100644 --- a/run.py +++ b/run.py @@ -1,5 +1,6 @@ import sys import io +import os sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", line_buffering=True) from airtable_client import AirtableClient @@ -11,6 +12,26 @@ import config from datetime import datetime +class Tee: + """Escribe simultáneamente en consola y en archivo de log.""" + def __init__(self, filepath): + os.makedirs(os.path.dirname(filepath), exist_ok=True) + self._file = open(filepath, "w", encoding="utf-8") + self._stdout = sys.stdout + + def write(self, data): + self._stdout.write(data) + self._file.write(data) + + def flush(self): + self._stdout.flush() + if not self._file.closed: + self._file.flush() + + def close(self): + self._file.close() + + ICONOS = { "PAUSAR": "⛔", "SPRINT": "🚀", @@ -115,4 +136,12 @@ def run(): if __name__ == "__main__": - run() + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + log_path = os.path.join("logs", f"{timestamp}.log") + tee = Tee(log_path) + sys.stdout = tee + try: + run() + finally: + tee.close() + print(f"\nLog guardado en: {log_path}", file=tee._stdout)