Skip to content

Conversation

@DiegoDAF
Copy link

Summary

This PR adds flexible log rotation support to pgcli, inspired by PostgreSQL's log_filename configuration and pgadmin4's implementation.

Features

New Configuration Options

  1. log_rotation_mode - Controls how log files are rotated:

    • none (default) - Single log file pgcli.log (backward compatible)
    • day-of-week - Rotates by day name (Mon-Sun), overwrites weekly
    • day-of-month - Rotates by day number (01-31), overwrites monthly
    • date - Rotates by date (YYYYMMDD), never overwrites
  2. log_destination - Specifies log directory:

    • default - Uses standard config location (~/.config/pgcli/log)
    • Custom path - Any valid directory

Characteristics

  • Backward compatible: Works exactly as before without configuration
  • System locale aware: Uses system's day names via strftime("%a")
  • No automatic deletion: Old log files are never deleted automatically
  • Flexible: Allows custom log directory location

Example Configuration

[main]
log_rotation_mode = day-of-week
log_destination = default

Generated Filenames

  • none: pgcli.log
  • day-of-week: pgcli-Mon.log, pgcli-Tue.log, etc.
  • day-of-month: pgcli-01.log, pgcli-02.log, ..., pgcli-31.log
  • date: pgcli-20250127.log, pgcli-20250128.log, etc.

Testing

Complete test coverage included:

  • 5 pytest unit tests in tests/test_main.py
  • 4 behave integration tests in tests/features/log_rotation.feature

Related Issues

Closes #1541 - This is the corrected and complete implementation with proper tests.

Implements a flexible log rotation system inspired by PostgreSQL's
log_filename parameter and pgadmin4's rotation mechanism.

Features:
- Four rotation modes: none (default), day-of-week, day-of-month, date
- Customizable log destination directory
- Backward compatible with existing configurations
- Uses system locale for day names (strftime %a)
- No automatic deletion of old log files

Configuration:
- log_rotation_mode: Controls how logs are rotated
  * none: Single pgcli.log file (default, backward compatible)
  * day-of-week: pgcli-Mon.log through pgcli-Sun.log (overwrites weekly)
  * day-of-month: pgcli-01.log through pgcli-31.log (overwrites monthly)
  * date: pgcli-YYYYMMDD.log (never overwrites)
- log_destination: Specifies directory for log files (default: ~/.config/pgcli/log)

Tests:
- Added 5 pytest unit tests for all rotation modes
- Added behave integration tests with 4 scenarios
- All tests verify correct file naming and backward compatibility

Files modified:
- pgcli/main.py: Updated initialize_logging() with rotation logic
- pgcli/__init__.py: Bumped version to 4.3.9
- pgcli/pgclirc: Added new config parameters with documentation
- tests/test_main.py: Added unit tests
- tests/features/log_rotation.feature: Added integration tests
- tests/features/steps/log_rotation.py: Added test steps
- changelog.rst: Documented new feature in Upcoming section
- AUTHORS: Added Diego to contributors
- .gitignore: Excluded pgcli/__init__.py from tracking
- pyproject.toml: Fixed paramiko version constraint for sshtunnel compatibility

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

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
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.

1 participant