-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable file logging * Add changelog
- Loading branch information
Showing
5 changed files
with
63 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Enabled detailed automatic logging to file. For locally executed commands, logs will | ||
go to `~/.neuro/logs` directory. Remote executor detailed logs will go to `storage:.flow/logs/<bake_id>/`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
from datetime import datetime, timedelta, timezone | ||
from pathlib import Path | ||
from random import random | ||
from typing import Optional | ||
|
||
|
||
DATETIME_FORMAT = "%Y-%m-%d_%H-%M-%S" | ||
LOGS_DIR = Path("~/.neuro/logs").expanduser() | ||
FILE_FORMAT_PREFIX = "neuro-flow-run-" | ||
LOGS_ROTATION_DELAY = timedelta(days=3) | ||
|
||
|
||
def get_handler() -> logging.FileHandler: | ||
if random() < 0.1: # Only do cleanup for 10% of runs | ||
_do_rotation(LOGS_ROTATION_DELAY) | ||
return _get_handler() | ||
|
||
|
||
_file_path_cached: Optional[Path] = None | ||
|
||
|
||
def get_log_file_path() -> Path: | ||
global _file_path_cached | ||
if _file_path_cached is None: | ||
now = datetime.now(timezone.utc) | ||
time_str = now.strftime(DATETIME_FORMAT) | ||
_file_path_cached = LOGS_DIR / f"{FILE_FORMAT_PREFIX}{time_str}.txt" | ||
return _file_path_cached | ||
|
||
|
||
def _get_handler() -> logging.FileHandler: | ||
LOGS_DIR.mkdir(parents=True, exist_ok=True) | ||
return logging.FileHandler(get_log_file_path()) | ||
|
||
|
||
def _do_rotation(delay: timedelta) -> None: | ||
now = datetime.now(timezone.utc) | ||
if not LOGS_DIR.exists(): | ||
return | ||
for log_file in LOGS_DIR.iterdir(): | ||
if not log_file.is_file(): | ||
continue | ||
time_str = log_file.stem[len(FILE_FORMAT_PREFIX) :] | ||
try: | ||
log_time = datetime.strptime(time_str, DATETIME_FORMAT) | ||
except ValueError: | ||
continue | ||
log_time = log_time.replace(tzinfo=timezone.utc) | ||
if log_time + delay < now: | ||
log_file.unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters