Skip to content

Commit

Permalink
write logs to XDG_STATE_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 16, 2023
1 parent f8d7cfc commit a174f0d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import logging
import os
import sys
from pathlib import Path

logger = logging.getLogger()
logger.setLevel(logging.INFO)

STATE_HOME = Path(os.environ.get("XDG_STATE_HOME", "~/.local/state")).expanduser() / "rose"
STATE_HOME.mkdir(parents=True, exist_ok=True)
LOGFILE = STATE_HOME / "rose.log"

# Add a logging handler for stdout unless we are testing. Pytest
# captures logging output on its own.
if "pytest" not in sys.modules: # pragma: no cover
Expand All @@ -14,3 +20,11 @@
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(stream_formatter)
logger.addHandler(stream_handler)

file_formatter = logging.Formatter(
"[%(asctime)s] [%(name)s:%(lineno)s] %(levelname)s: %(message)s",
datefmt="%H:%M:%S",
)
file_handler = logging.FileHandler(LOGFILE)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

0 comments on commit a174f0d

Please sign in to comment.