Skip to content

Conversation

@cfsmp3
Copy link
Contributor

@cfsmp3 cfsmp3 commented Jan 4, 2026

Summary

Fixes crash on startup when log file is owned by a different user (e.g., root vs www-data).

Problem: When the web service runs under www-data but the log file /var/www/sample-platform/logs/platform.log is owned by root, the LogConfiguration class would throw a PermissionError and crash the entire application on startup.

Solution: Handle permission errors gracefully by falling back to console-only logging instead of crashing.

Changes

  • Catch PermissionError and OSError when creating the file handler
  • Fall back to console-only logging instead of crashing
  • Print helpful warning message to stderr with fix instructions:
    [WARNING] Cannot write to log file /path/to/log: Permission denied. 
    Falling back to console-only logging. 
    Fix: sudo chown www-data:www-data /path/to/logs -R
    
  • Update file_logger property to return Optional[RotatingFileHandler]
  • Update create_logger to skip file handler when unavailable
  • Added 3 new tests for error handling scenarios

Test plan

  • Existing tests pass
  • New tests for PermissionError handling
  • New tests for OSError handling
  • New tests for create_logger without file logger

🤖 Generated with Claude Code

When the log file is owned by a different user (e.g., root vs www-data),
the app would crash on startup with PermissionError. This was causing
workers to fail immediately.

Changes:
- Catch PermissionError and OSError when creating the file handler
- Fall back to console-only logging instead of crashing
- Print helpful warning message with fix instructions to stderr
- Update file_logger property to return Optional[RotatingFileHandler]
- Update create_logger to skip file handler when unavailable

This prevents the entire application from crashing due to a log file
permission issue, allowing it to continue operating with console logging
while still alerting operators to fix the underlying permission problem.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Comment on lines +40 to +48
except PermissionError as e:
# Log file owned by different user (e.g., root vs www-data)
# Fall back to console-only logging rather than crashing
print(
f"[WARNING] Cannot write to log file {path}: {e}. "
f"Falling back to console-only logging. "
f"Fix: sudo chown www-data:www-data {log_dir} -R",
file=sys.stderr
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't crash, this will just go unnoticed and we'll lose a shit ton of logs. I'd rather see extra changes in the deploy script that guarantee the logs are correctly owned...

Per review feedback: ensure logs directory is owned by www-data
during deployment, preventing silent fallback to console-only logging.

The pre-deploy script now:
- Checks if logs directory has correct ownership
- Automatically fixes ownership if possible (when run as root)
- Fails deployment if ownership cannot be fixed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Jan 4, 2026

Good point! I've added a check in pre_deploy.sh that:

  1. Verifies the logs directory has correct ownership (www-data)
  2. Automatically fixes ownership if running as root (which the deploy script does)
  3. Fails deployment if ownership cannot be fixed

This ensures the issue is caught and fixed during deployment rather than silently falling back to console-only logging.

The graceful fallback in log_configuration.py still serves as a safety net (e.g., if permissions change outside of deployment), but the primary fix is now in the deploy script as you suggested.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 4, 2026

@canihavesomecoffee canihavesomecoffee merged commit 5f16676 into master Jan 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants