SpotifySaver Logging Configuration: A Complete Guide to Customization
SpotifySaver uses Python’s built-in logging module with a centralized LoggerConfig class in spotifysaver/spotlog/log_config.py that writes to logs/app.log and supports runtime customization via environment variables or direct programmatic overrides.
The gabrielbaute/spotify-saver repository implements a flexible, environment-driven logging system designed for both development debugging and production monitoring. Understanding this logging configuration allows you to control output destinations, verbosity levels, and formatting without modifying core application logic.
How SpotifySaver Configures Logging
The logging infrastructure centers on the LoggerConfig class defined in spotifysaver/spotlog/log_config.py. This class provides a static setup() method that initializes the entire logging stack when the application starts.
File-Based Logging Architecture
By default, all log messages route to a dedicated file rather than stdout. The configuration establishes:
- Destination:
logs/app.log(created automatically if missing) - Format:
%(asctime)s [%(levelname)s] [%(name)s]: %(message)s - Handler:
FileHandlerwith UTF-8 encoding
This file-centric approach ensures persistent logs in containerized or headless environments where console output might be ephemeral.
Environment-Driven Log Levels
The system reads the desired verbosity from the LOG_LEVEL environment variable, loaded via Config.LOG_LEVEL in spotifysaver/config/setting_environment.py. The LoggerConfig.get_log_level() method maps string values to Python logging constants:
debug→logging.DEBUGinfo→logging.INFO(default)warning→logging.WARNINGerror→logging.ERRORcritical→logging.CRITICAL
Conditional Console Output
SpotifySaver implements a dual-handler strategy for console visibility. The setup() method attaches a StreamHandler to the root logger only when the configured level is DEBUG. For INFO and higher severities, it attaches a NullHandler to suppress console output, ensuring production logs remain clean while file output continues uninterrupted.
Customizing the Logging Configuration
You can modify SpotifySaver’s logging behavior through four primary mechanisms without altering the source code directly.
Change Log Level via Environment Variables
The simplest customization involves setting the LOG_LEVEL variable in your shell or .env file. This affects the entire application on the next startup.
# Enable debug logging for troubleshooting
export LOG_LEVEL=debug
# Or in a .env file
LOG_LEVEL=error
Override Log Level Programmatically
For library usage or testing scenarios, bypass the environment variable by passing an explicit level to LoggerConfig.setup():
import logging
from spotifysaver.spotlog.log_config import LoggerConfig
# Force WARNING level regardless of environment
LoggerConfig.setup(level=logging.WARNING)
This approach is useful when integrating SpotifySaver components into larger applications with their own logging strategies.
Redirect Log Output Location
Modify the LOG_DIR and LOG_FILE class attributes before calling setup() to change where logs persist:
import os
from spotifysaver.spotlog.log_config import LoggerConfig
# Change log location at runtime
LoggerConfig.LOG_DIR = os.path.join(os.getcwd(), "custom_logs")
LoggerConfig.LOG_FILE = os.path.join(LoggerConfig.LOG_DIR, "myapp.log")
LoggerConfig.setup()
This method ensures compliance with organizational logging standards or container volume mounts.
Enable Console Logging in Production
To force console output regardless of the log level, manually attach a StreamHandler after the initial setup:
import logging
from spotifysaver.spotlog.log_config import LoggerConfig
LoggerConfig.setup()
# Add permanent console handler
console = logging.StreamHandler()
console.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logging.getLogger().addHandler(console)
This configuration is ideal for Docker containers or systemd services where both file and stdout logging are required.
Code Examples
Basic Initialization
from spotifysaver.spotlog.log_config import LoggerConfig
# Standard usage with environment-based configuration
LoggerConfig.setup()
logger = LoggerConfig.get_logger("spotifysaver.downloader")
logger.info("Starting download process")
logger.debug("Connection established with Spotify API")
Testing with Custom Levels
import logging
from spotifysaver.spotlog.log_config import LoggerConfig
def test_error_handling():
# Suppress info logs during test
LoggerConfig.setup(level=logging.ERROR)
logger = LoggerConfig.get_logger("test")
# This will not appear
logger.info("This is hidden")
# This will appear in logs/app.log
logger.error("Critical failure detected")
Summary
- SpotifySaver implements centralized logging through the
LoggerConfigclass inspotifysaver/spotlog/log_config.py. - Default behavior writes formatted logs to
logs/app.logwithINFOlevel, suppressing console output unlessDEBUGis enabled. - Environment customization uses the
LOG_LEVELvariable defined inspotifysaver/config/setting_environment.pyto control verbosity without code changes. - Programmatic customization allows overriding destinations via
LOG_DIR/LOG_FILEattributes or forcing specific levels throughLoggerConfig.setup(level=...). - Console output can be permanently enabled by manually adding a
StreamHandlerafter initialization.
Frequently Asked Questions
Where are SpotifySaver logs stored by default?
By default, SpotifySaver writes all log output to logs/app.log relative to the project root. This path is defined in the LoggerConfig class within spotifysaver/spotlog/log_config.py. The directory is created automatically if it does not exist, ensuring the application starts cleanly in fresh environments.
How do I enable debug logging in SpotifySaver?
Set the LOG_LEVEL environment variable to debug before starting the application. This can be done via shell export (export LOG_LEVEL=debug) or in a .env file. When LOG_LEVEL is set to debug, LoggerConfig.setup() automatically attaches a StreamHandler to output detailed messages to the console in addition to the file log.
Can I log to both file and console simultaneously?
Yes, although SpotifySaver only enables console output by default when the level is DEBUG, you can force simultaneous logging by manually adding a StreamHandler after calling LoggerConfig.setup(). Retrieve the root logger via logging.getLogger(), create a StreamHandler, apply your preferred formatter, and add it to the logger. This configuration persists for the application lifetime.
What is the default log format used by SpotifySaver?
The default format string is %(asctime)s [%(levelname)s] [%(name)s]: %(message)s, which produces timestamps like 2024-01-15 10:30:45,123 [INFO] [spotifysaver.auth]: Authentication successful. This format is applied to both the file handler and the conditional console handler in spotifysaver/spotlog/log_config.py.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →