Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script to collect logs from containers #592

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tools/src/zip_dev_env_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import docker
import logging
import os
import zipfile
import tempfile


def setup_logging():
"""Initialize logging with level INFO."""
logging.basicConfig(level=logging.INFO)


def save_container_logs(output_directory):
client = docker.from_env()

containers = client.containers.list()

if not os.path.exists(output_directory):
os.makedirs(output_directory)

with tempfile.TemporaryDirectory() as temp_output_directory:
for container in containers:
container_name = container.name
logging.info(f'Saving logs from container: {container_name}')

log_content = container.logs().decode('utf-8')
log_filename = f'{container_name}_logs.txt'
log_file_path = os.path.join(temp_output_directory, log_filename)

with open(log_file_path, 'w', encoding='utf-8') as log_file:
log_file.write(log_content)

logging.info(f'Logs from container {container_name} saved to file: {log_file_path}')

zip_filename = os.path.join(output_directory, 'containers_logs.zip')
with zipfile.ZipFile(zip_filename, 'w') as zip_file:
for folder_name, subfolders, filenames in os.walk(temp_output_directory):
for filename in filenames:
file_path = os.path.join(folder_name, filename)
zip_file.write(file_path, os.path.basename(file_path))

logging.info(f'Containers logs saved to zip archive: {zip_filename}')


if __name__ == "__main__":
setup_logging()
output_directory = os.getcwd()
save_container_logs(output_directory)
Loading