Skip to content

Instantly share code, notes, and snippets.

@costa86
Created October 22, 2021 18:21
Show Gist options
  • Save costa86/01706fc763baa9c50ee6c3f2c59710d3 to your computer and use it in GitHub Desktop.
Save costa86/01706fc763baa9c50ee6c3f2c59710d3 to your computer and use it in GitHub Desktop.
Get a new log file based on size
MAX_LOG_FILE_SIZE_IN_MB = 1.00
def get_current_log_file() -> str:
logs_path = os.path.join(os.getcwd(), "logs")
log_files = os.listdir(logs_path)
log_files_numbers = [int(i.split(".")[0].split("-")[-1])
for i in log_files]
greater_log_file_number = max(log_files_numbers)
current_log_file = os.path.join(
os.getcwd(), "logs", f"log-{greater_log_file_number}.json")
current_log_file_size_in_mb = round(
(os.path.getsize(current_log_file) / 1024 / 1024), 2)
if current_log_file_size_in_mb > MAX_LOG_FILE_SIZE_IN_MB:
current_log_file = os.path.join(
os.getcwd(), "logs", f"log-{greater_log_file_number + 1}.json")
return current_log_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment