From 37f13c188233641ae6444102d81be88b082a6a2f Mon Sep 17 00:00:00 2001 From: blissful Date: Mon, 16 Oct 2023 11:58:37 -0400 Subject: [PATCH] write logs to XDG_STATE_HOME --- README.md | 10 +++++----- rose/__init__.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1ff0fa0..d80cde9 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,10 @@ remixer ::= ' remixed by ' name name ::= string ';' name | string ``` +## Metadata Management + +TODO + ## New Releases TODO @@ -221,11 +225,7 @@ TODO; example unit files to schedule Rosé with systemd. ## Logging -TODO - -## Metadata Management - -TODO +Logs are written to stdout and to `${XDG_STATE_HOME:-$HOME/.local/state}/rose/rose.log`. # Architecture diff --git a/rose/__init__.py b/rose/__init__.py index 228fe65..82226a7 100644 --- a/rose/__init__.py +++ b/rose/__init__.py @@ -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 @@ -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)