Skip to content

Commit 09904a1

Browse files
Zero6992claude
andcommitted
fix: resolve Docker read-only filesystem logging issue
- Use /tmp directory for log files in Docker containers - Add error handling for read-only filesystem scenarios - Fall back to console-only logging if file writing fails - Maintain backward compatibility with local development - Ensure bot continues to run even if file logging is unavailable 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8b7b859 commit 09904a1

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/log.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,25 @@ def setup_logger(module_name:str) -> logging.Logger:
5353
logger.addHandler(console_handler)
5454

5555
if os.getenv("LOGGING") == "True": # Check if logging is enabled
56-
# specify that the log file path is the same as `main.py` file path
57-
grandparent_dir = os.path.abspath(f"{__file__}/../../")
56+
# Use /tmp for Docker read-only filesystem compatibility
57+
log_dir = "/tmp" if os.path.exists("/tmp") else os.path.abspath(f"{__file__}/../../")
5858
log_name = 'chatgpt_discord_bot.log'
59-
log_path = os.path.join(grandparent_dir, log_name)
60-
# create local log handler
61-
log_handler = logging.handlers.RotatingFileHandler(
62-
filename=log_path,
63-
encoding='utf-8',
64-
maxBytes=32 * 1024 * 1024, # 32 MiB
65-
backupCount=2, # Rotate through 5 files
66-
)
67-
log_handler.setFormatter(CustomFormatter())
68-
log_handler.setLevel(level)
69-
logger.addHandler(log_handler)
59+
log_path = os.path.join(log_dir, log_name)
60+
61+
try:
62+
# create local log handler
63+
log_handler = logging.handlers.RotatingFileHandler(
64+
filename=log_path,
65+
encoding='utf-8',
66+
maxBytes=32 * 1024 * 1024, # 32 MiB
67+
backupCount=2, # Rotate through 2 files
68+
)
69+
log_handler.setFormatter(CustomFormatter())
70+
log_handler.setLevel(level)
71+
logger.addHandler(log_handler)
72+
except (OSError, IOError) as e:
73+
# If file logging fails (e.g., read-only filesystem), continue with console only
74+
logger.warning(f"Could not create log file at {log_path}: {e}. Using console logging only.")
7075

7176
return logger
7277

0 commit comments

Comments
 (0)