3.0 KiB
3.0 KiB
Logging Configuration
The AlpineBits Python server uses a centralized logging system that can be configured via the config.yaml file.
Configuration
Add the following section to your config/config.yaml:
logger:
level: "INFO" # Options: DEBUG, INFO, WARNING, ERROR, CRITICAL
file: "logs/alpinebits.log" # Optional: path to log file (omit or set to null for console-only)
Log Levels
- DEBUG: Detailed diagnostic information (very verbose)
- INFO: General informational messages about application progress
- WARNING: Warning messages about potential issues
- ERROR: Error messages when something goes wrong
- CRITICAL: Critical errors that may cause application failure
Log Output
- Console: Logs are always written to console (stdout)
- File: Optionally write logs to a file by specifying the
fileparameter- File logs include the same timestamp and formatting as console logs
- Log directory will be created automatically if it doesn't exist
Usage in Code
To use logging in any module:
from alpine_bits_python.logging_config import get_logger
_LOGGER = get_logger(__name__)
# Then use the logger
_LOGGER.info("Application started")
_LOGGER.debug("Detailed debug information: %s", some_variable)
_LOGGER.warning("Something unusual happened")
_LOGGER.error("An error occurred: %s", error_message)
_LOGGER.exception("Critical error with stack trace")
Log Format
All log entries include:
- Timestamp (YYYY-MM-DD HH:MM:SS)
- Module name (logger name)
- Log level
- Message
Example:
2025-10-09 14:23:45 - alpine_bits_python.api - INFO - Application startup initiated
2025-10-09 14:23:45 - alpine_bits_python.api - INFO - Logging configured at INFO level
2025-10-09 14:23:46 - alpine_bits_python.api - INFO - Database tables checked/created at startup.
Best Practices
-
Use structured logging: Pass variables as arguments, not f-strings
# Good _LOGGER.info("Processing reservation %s for hotel %s", reservation_id, hotel_code) # Avoid (performance overhead, linting warnings) _LOGGER.info(f"Processing reservation {reservation_id} for hotel {hotel_code}") -
Use appropriate log levels:
DEBUG: Detailed tracing for developmentINFO: Normal application flow eventsWARNING: Unexpected but handled situationsERROR: Errors that need attentionCRITICAL: Severe errors requiring immediate action
-
Use
exception()for error handling:try: risky_operation() except Exception: _LOGGER.exception("Operation failed") # Automatically includes stack trace -
Don't log sensitive data: Avoid logging passwords, tokens, or personal data
Examples
Console-only logging (development)
logger:
level: "DEBUG"
File logging (production)
logger:
level: "INFO"
file: "/var/log/alpinebits/app.log"
Minimal logging
logger:
level: "WARNING"
file: "logs/warnings.log"