Skip to content

Commit 8ca8bac

Browse files
committed
feat: Improve logging configuration
- Use XDG_STATE_HOME or fallback for log directory - Add rotating file handler and console handler with different levels - Update CHANGELOG for v0.4.0-beta
1 parent f12bf57 commit 8ca8bac

3 files changed

Lines changed: 45 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22
All notable changes to this project will be documented in this file. Commits automatically generated by github actions.
33

4-
## v0.3.2-beta
4+
## v0.4.0-beta
55

66
## v0.3.1-beta
77
### Changes

main.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,56 @@
55
"""
66

77
import logging
8+
from logging.handlers import RotatingFileHandler
89
import os
910
import sys
1011
from typing import List
12+
from pathlib import Path
1113

1214
from src.commands import DecryptCommand, EncryptCommand, ExtractCommand
1315
from src.facade import BackupFacade
1416

15-
16-
def setup_logging():
17-
"""Configure application logging."""
18-
log_dir = "logs"
19-
os.makedirs(log_dir, exist_ok=True)
20-
21-
logging.basicConfig(
22-
level=logging.INFO,
23-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
24-
filename=f"{log_dir}/AutoTarCompress.log",
25-
)
17+
def get_xdg_state_home() -> Path:
18+
"""Get the XDG state home directory."""
19+
xdg_state_home = os.getenv("XDG_STATE_HOME")
20+
if not xdg_state_home or not Path(xdg_state_home).is_absolute():
21+
return Path.home() / ".local" / "state"
22+
return Path(xdg_state_home)
23+
24+
def setup_logging() -> None:
25+
"""Configure logging for the application."""
26+
log_dir_base = get_xdg_state_home()
27+
28+
log_dir = log_dir_base / "autotarcompress"
29+
log_dir.mkdir(parents=True, exist_ok=True) # Use parents=True to create intermediate dirs
30+
log_file = "autotarcompress.log"
31+
log_file_path = log_dir / log_file
32+
33+
# Configure file handler for all log levels
34+
file_handler = RotatingFileHandler(log_file_path, maxBytes=1024 * 1024, backupCount=3)
35+
file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s")
36+
file_handler.setFormatter(file_formatter)
37+
file_handler.setLevel(logging.DEBUG)
38+
39+
# Configure console handler for ERROR and above only
40+
console_handler = logging.StreamHandler()
41+
console_handler.setLevel(logging.ERROR) # Only show errors and above in console
42+
console_formatter = logging.Formatter("%(message)s") # Simpler format for console
43+
console_handler.setFormatter(console_formatter)
44+
45+
# Get root logger and configure it
46+
logger = logging.getLogger()
47+
logger.setLevel(logging.DEBUG)
48+
49+
# Remove any existing handlers (in case this function is called multiple times)
50+
for handler in logger.handlers[:]:
51+
logger.removeHandler(handler)
52+
53+
# Add the configured handlers
54+
logger.addHandler(file_handler)
55+
logger.addHandler(console_handler)
56+
57+
logging.info("Logging configured with DEBUG level")
2658

2759

2860
def select_file(files: List[str], backup_folder: str) -> str:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = 'AutoTarCompress'
3-
version = '0.3.2-beta'
3+
version = '0.4.0-beta'
44
description = 'It downloads/updates appimages via GitHub API. It also validates the appimage with SHA256 and SHA512.'
55
requires-python = ">= 3.8"
66
dependencies = [

0 commit comments

Comments
 (0)