From ce31d116adac3023130d227da71ce17039b4ab12 Mon Sep 17 00:00:00 2001 From: Parsa Darbouy Date: Tue, 26 Aug 2025 17:09:02 -0600 Subject: [PATCH 1/2] feat: update logging configuration to use environment variable for log file paths --- .env.example | 5 ++++- backup_filebase_locally.py | 12 ++++++++---- filebase_pin_api.py | 4 ++-- get_filebase_cids.py | 6 +++++- kubo_rpc_api.py | 2 +- metadata_checker.py | 5 ++++- update_filebase_with_local.py | 12 +++++++++--- 7 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index 0831461..df8119c 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,7 @@ FILEBASE_TOKEN_REALITY_V2= FILEBASE_TOKEN_KLEROS_TOKEN_LIST= FILEBASE_TOKEN_V2_LOGS= FILEBASE_TOKEN_ATLAS_LOGS= -FILEBASE_TOKEN_KLEROS_SCOUT= \ No newline at end of file +FILEBASE_TOKEN_KLEROS_SCOUT= + +# Location of logs. By defult it uses /var/log/py-kleros-ipfs +LOG_FILEPATH= \ No newline at end of file diff --git a/backup_filebase_locally.py b/backup_filebase_locally.py index 0401d5b..f3ef622 100644 --- a/backup_filebase_locally.py +++ b/backup_filebase_locally.py @@ -17,13 +17,17 @@ from filebase_pin_api import FilebasePinAPI from logger import setup_logger -LOG_LOCATION: str = 'logs/backup_filebase_locally.log' -RPC = KuboRPC(log_filepath=LOG_LOCATION, +load_dotenv() +log_path: str = os.getenv('LOG_FILEPATH', '/var/log/py-kleros-ipfs') +log_filepath: str = os.path.join(log_path, 'backup_filebase_locally.log') + + +RPC = KuboRPC(log_filepath=log_filepath, log_level=logging.DEBUG) -FILEBASE_API = FilebasePinAPI(log_filepath=LOG_LOCATION, +FILEBASE_API = FilebasePinAPI(log_filepath=log_filepath, log_level=logging.DEBUG) logger: logging.Logger = setup_logger( - name="backup-filebase-locally", log_file=LOG_LOCATION, level=logging.DEBUG) + name="backup-filebase-locally", log_file=log_filepath, level=logging.DEBUG) BUCKETS: List[str] = [ 'kleros', 'kleros-v2', diff --git a/filebase_pin_api.py b/filebase_pin_api.py index f117eed..d61ca39 100644 --- a/filebase_pin_api.py +++ b/filebase_pin_api.py @@ -48,12 +48,12 @@ class FilebasePinAPI(): https://docs.filebase.com/api-documentation/ipfs-pinning-service-api """ - def __init__(self, log_filepath: str = "filebasePinAPI.log", log_level: int = logging.INFO) -> None: + def __init__(self, log_filepath: str = "/var/log/py-kleros-ipfs/filebasePinAPI.log", log_level: int = logging.INFO) -> None: """ Initialize the FilebasePinAPI class. Args: - log_filepath (str, optional): Path to the log file. Defaults to "filebasePinAPI.log". + log_filepath (str, optional): Path to the log file. Defaults to "/var/log/py-kleros-ipfs/filebasePinAPI.log". log_level (int, optional): Log level for the self.logger. Defaults to logging.INFO. """ self.logger: logging.Logger = setup_logger( diff --git a/get_filebase_cids.py b/get_filebase_cids.py index aa17e21..7f248d3 100644 --- a/get_filebase_cids.py +++ b/get_filebase_cids.py @@ -34,7 +34,11 @@ def main() -> None: Returns: None """ - filebase_api = FilebasePinAPI(log_filepath="logs/get_filebase_cids.log") + load_dotenv() + log_path: str = os.getenv('LOG_FILEPATH', '/var/log/py-kleros-ipfs') + log_filepath: str = os.path.join(log_path, 'get_filebase_cids.log') + + filebase_api = FilebasePinAPI(log_filepath=log_filepath) for bucket in BUCKETS: cids = filebase_api.get_all_cids(bucket) print(f"{bucket} has {len(cids)} CIDs") diff --git a/kubo_rpc_api.py b/kubo_rpc_api.py index b0c339e..6b268a1 100644 --- a/kubo_rpc_api.py +++ b/kubo_rpc_api.py @@ -22,7 +22,7 @@ class KuboRPC(): def __init__( self, api_url: str = "http://127.0.0.1:5001/api/v0", - log_filepath: str = "filebasePinAPI.log", + log_filepath: str = "/var/log/py-kleros-ipfs/filebasePinAPI.log", log_level: int = logging.INFO ) -> None: self.api_url: str = api_url diff --git a/metadata_checker.py b/metadata_checker.py index 7c55052..7afde22 100644 --- a/metadata_checker.py +++ b/metadata_checker.py @@ -6,9 +6,12 @@ from dotenv import load_dotenv from filebase_pin_api import FilebasePinAPI +import os load_dotenv() +log_path: str = os.getenv('LOG_FILEPATH', '/var/log/py-kleros-ipfs') +log_filepath: str = os.path.join(log_path, 'metadata_checker.log') -filebase_api = FilebasePinAPI('logs/metadata_checker.log') +filebase_api = FilebasePinAPI(log_filepath) print(filebase_api.get_file( 'kleros', 'QmVq2GstdkVQDNQFZMLGLFpVxwHR2abYyYgFZwh8GtkkWi')) diff --git a/update_filebase_with_local.py b/update_filebase_with_local.py index a55957a..abe0c1f 100644 --- a/update_filebase_with_local.py +++ b/update_filebase_with_local.py @@ -7,6 +7,8 @@ import random from typing import List +import os +from dotenv import load_dotenv from filebase_datatypes import GetPinsResponse from filebase_pin_api import PinStatus, FilebasePinAPI from kubo_rpc_api import KuboRPC @@ -43,9 +45,13 @@ def main(filepath, bucket_name="kleros"): The first 10 CIDs from the list. The number of CIDs in Kleros IPFS, in Filebase, and the number of missed CIDs in Filebase. """ - kubo_rpc = KuboRPC(log_filepath='logs/update_filebase_with_local.log') - filebase_api = FilebasePinAPI( - log_filepath='logs/update_filebase_with_local.log') + # Load log filepath from environment (.env) + load_dotenv() + log_path: str = os.getenv('LOG_FILEPATH', '/var/log/py-kleros-ipfs') + log_filepath: str = os.path.join(log_path, 'update_filebase_with_local.log') + + kubo_rpc = KuboRPC(log_filepath=log_filepath) + filebase_api = FilebasePinAPI(log_filepath=log_filepath) bucket_name = 'kleros' cids: List[str] = kubo_rpc.read_pin_ls_output(filepath) print(cids[0:10]) From 961aa032d1866c2330c2a5067bf88bb7453d43d7 Mon Sep 17 00:00:00 2001 From: Parsa Darbouy Date: Thu, 28 Aug 2025 14:04:11 -0600 Subject: [PATCH 2/2] fix: add dotenv support for environment variable management --- backup_filebase_locally.py | 1 + get_filebase_cids.py | 1 + 2 files changed, 2 insertions(+) diff --git a/backup_filebase_locally.py b/backup_filebase_locally.py index f3ef622..0f35a2c 100644 --- a/backup_filebase_locally.py +++ b/backup_filebase_locally.py @@ -6,6 +6,7 @@ that were pinned in filebase. """ import os +from dotenv import load_dotenv from typing import Dict, List import logging diff --git a/get_filebase_cids.py b/get_filebase_cids.py index 7f248d3..5de5d61 100644 --- a/get_filebase_cids.py +++ b/get_filebase_cids.py @@ -5,6 +5,7 @@ """ from typing import List +from dotenv import load_dotenv from filebase_pin_api import FilebasePinAPI