Skip to content

Commit e6702e5

Browse files
DiegoDAFclaude
andcommitted
feat: implement daily log rotation and /var/log/pgcli location (v4.3.7)
Improvements to logging system: - Implement automatic daily log rotation at midnight - Keep 30 days of log history - Change default log location to /var/log/pgcli/pgcli.log - Automatic fallback to ~/.config/pgcli/log if no system permissions - Use TimedRotatingFileHandler for better log management - Log files formatted as: pgcli.log.YYYY-MM-DD Changes: - pgcli/main.py: Added logging.handlers import and updated initialize_logging() - pgcli/pgclirc: Updated documentation for new log behavior - pgcli/__init__.py: Bumped version to 4.3.7 - changelog.rst: Added Internal section entry 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bbca4a1 commit e6702e5

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Internal:
4545
* Use github trusted publisher for pypi release
4646
* Update dev requirements and replace requirements-dev.txt with pyproject.toml
4747
* Use ruff instead of black
48+
* Implement daily log rotation at midnight with 30-day retention using TimedRotatingFileHandler
49+
* Change default log location to /var/log/pgcli/pgcli.log (with automatic fallback to ~/.config/pgcli/log if no permissions)
4850

4951
Bug fixes:
5052
----------

pgcli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "4.3.6"
1+
__version__ = "4.3.7"

pgcli/main.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import traceback
1111
import logging
12+
import logging.handlers
1213
import threading
1314
import shutil
1415
import functools
@@ -550,7 +551,18 @@ def write_to_file(self, pattern, **_):
550551
def initialize_logging(self):
551552
log_file = self.config["main"]["log_file"]
552553
if log_file == "default":
553-
log_file = config_location() + "log"
554+
# Try /var/log/pgcli first, fallback to user config dir if no permissions
555+
default_system_log = "/var/log/pgcli/pgcli.log"
556+
try:
557+
os.makedirs(os.path.dirname(default_system_log), exist_ok=True)
558+
# Test if we can write to /var/log/pgcli
559+
with open(default_system_log, "a"):
560+
pass
561+
log_file = default_system_log
562+
except (OSError, PermissionError):
563+
# Fallback to user's config directory if no system permissions
564+
log_file = config_location() + "log"
565+
554566
ensure_dir_exists(log_file)
555567
log_level = self.config["main"]["log_level"]
556568

@@ -559,7 +571,18 @@ def initialize_logging(self):
559571
if log_level.upper() == "NONE":
560572
handler = logging.NullHandler()
561573
else:
562-
handler = logging.FileHandler(os.path.expanduser(log_file))
574+
# Use TimedRotatingFileHandler for daily log rotation
575+
# Rotates at midnight, keeps 30 days of logs
576+
expanded_log_file = os.path.expanduser(log_file)
577+
handler = logging.handlers.TimedRotatingFileHandler(
578+
expanded_log_file,
579+
when='midnight',
580+
interval=1,
581+
backupCount=30,
582+
encoding='utf-8'
583+
)
584+
# Format: pgcli.log.2025-12-02
585+
handler.suffix = "%Y-%m-%d"
563586

564587
level_map = {
565588
"CRITICAL": logging.CRITICAL,

pgcli/pgclirc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ generate_aliases = False
6666
alias_map_file =
6767

6868
# log_file location.
69-
# In Unix/Linux: ~/.config/pgcli/log
70-
# In Windows: %USERPROFILE%\AppData\Local\dbcli\pgcli\log
71-
# %USERPROFILE% is typically C:\Users\{username}
69+
# Default behavior:
70+
# - Tries /var/log/pgcli/pgcli.log first (if running with permissions)
71+
# - Falls back to ~/.config/pgcli/log (Unix/Linux) or %USERPROFILE%\AppData\Local\dbcli\pgcli\log (Windows)
72+
# Log rotation: Daily at midnight, keeps 30 days of logs
73+
# Format: pgcli.log.YYYY-MM-DD
74+
# You can specify a custom path here if needed
7275
log_file = default
7376

7477
# keyword casing preference. Possible values: "lower", "upper", "auto"

0 commit comments

Comments
 (0)