-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import logging | ||
import os | ||
import sys | ||
from logging.handlers import RotatingFileHandler | ||
from typing import Optional | ||
|
||
# Constants with sane defaults | ||
DEFAULT_LOG_FILE = "app.log" | ||
DEFAULT_MAX_BYTES = 10 * 1024 * 1024 # 10MB | ||
DEFAULT_BACKUP_COUNT = 5 | ||
DEFAULT_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
|
||
_logger: Optional[logging.Logger] = None | ||
|
||
def setup_logging( | ||
log_file: str = os.getenv("LOG_FILE", DEFAULT_LOG_FILE), | ||
) -> logging.Logger: | ||
""" | ||
Configure and return a logger that: | ||
- Prints INFO and above to console | ||
- Writes DEBUG and above to rotating file | ||
""" | ||
global _logger | ||
if _logger is not None: | ||
return _logger | ||
|
||
logger = logging.getLogger("app") | ||
logger.setLevel(logging.DEBUG) # Capture all levels | ||
|
||
# Create formatter | ||
formatter = logging.Formatter(DEFAULT_FORMAT) | ||
|
||
# Remove any existing handlers | ||
logger.handlers.clear() | ||
|
||
# Console handler (stdout) - INFO and above | ||
console_handler = logging.StreamHandler(sys.stdout) | ||
console_handler.setLevel(logging.INFO) | ||
console_handler.setFormatter(formatter) | ||
logger.addHandler(console_handler) | ||
|
||
# File handler - DEBUG and above | ||
try: | ||
file_handler = RotatingFileHandler( | ||
log_file, | ||
maxBytes=DEFAULT_MAX_BYTES, | ||
backupCount=DEFAULT_BACKUP_COUNT, | ||
encoding='utf-8' | ||
) | ||
file_handler.setLevel(logging.DEBUG) | ||
file_handler.setFormatter(formatter) | ||
logger.addHandler(file_handler) | ||
except PermissionError: | ||
logger.warning(f"Unable to create/access log file: {log_file}") | ||
|
||
# Prevent logging from propagating to the root logger | ||
logger.propagate = False | ||
|
||
_logger = logger | ||
return logger | ||
|
||
def get_logger() -> logging.Logger: | ||
"""Get or create a logger instance.""" | ||
global _logger | ||
if _logger is None: | ||
_logger = setup_logging() | ||
return _logger | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.