GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-deadlines/utils/logging_config.py at main · python-spain/python-deadlines Expand file tree/ Copy pathlogging_config.pyMore file actions 168 lines (127 loc) · 4.6 KB / Copy pathlogging_config.pyFile metadata and controls168 lines (127 loc) · 4.6 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 """Logging configuration for Python Deadlines project.""" import logging import sys from pathlib import Path from typing import TYPE_CHECKING if TYPE_CHECKING: pass def setup_logging(level: str = "INFO", log_file: str | None = None, include_timestamp: bool = True) -> logging.Logger: """Set up logging configuration for the project. Args: level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) log_file: Optional log file path include_timestamp: Whether to include timestamps in log messages Returns ------- logging.Logger: Configured logger instance """ # Create logger logger = logging.getLogger("python_deadlines") logger.setLevel(getattr(logging, level.upper())) # Clear existing handlers to avoid duplicates logger.handlers.clear() # Create formatter if include_timestamp: formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) else: formatter = logging.Formatter("%(levelname)s - %(message)s") # Console handler console_handler = logging.StreamHandler(sys.stdout) console_handler.setFormatter(formatter) logger.addHandler(console_handler) # File handler (optional) if log_file: log_path = Path(log_file) log_path.parent.mkdir(parents=True, exist_ok=True) file_handler = logging.FileHandler(log_path, encoding="utf-8") file_handler.setFormatter(formatter) logger.addHandler(file_handler) return logger class TqdmLoggingHandler(logging.Handler): """Custom logging handler that uses tqdm.write() for tqdm-compatible output.""" def __init__(self, level=logging.NOTSET): super().__init__(level) def emit(self, record): try: msg = self.format(record) # Try to import and use tqdm.write, fallback to print try: from tqdm import tqdm tqdm.write(msg) except ImportError: print(msg) except Exception: self.handleError(record) def setup_tqdm_logging( level: str = "INFO", log_file: str | None = None, include_timestamp: bool = True, ) -> logging.Logger: """Set up tqdm-compatible logging configuration for the project. Args: level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) log_file: Optional log file path include_timestamp: Whether to include timestamps in log messages Returns ------- logging.Logger: Configured logger instance with tqdm-compatible output """ # Create logger logger = logging.getLogger("python_deadlines") logger.setLevel(getattr(logging, level.upper())) # Clear existing handlers to avoid duplicates logger.handlers.clear() # Create formatter if include_timestamp: formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) else: formatter = logging.Formatter("%(levelname)s - %(message)s") # Tqdm-compatible console handler tqdm_handler = TqdmLoggingHandler() tqdm_handler.setFormatter(formatter) logger.addHandler(tqdm_handler) # File handler (optional) if log_file: log_path = Path(log_file) log_path.parent.mkdir(parents=True, exist_ok=True) file_handler = logging.FileHandler(log_path, encoding="utf-8") file_handler.setFormatter(formatter) logger.addHandler(file_handler) return logger def get_logger(name: str | None = None) -> logging.Logger: """Get a logger instance with the project configuration. Args: name: Logger name (defaults to calling module) Returns ------- logging.Logger: Logger instance """ if name is None: name = "python_deadlines" logger = logging.getLogger(name) # Set up basic configuration if not already configured if not logger.handlers: return setup_tqdm_logging() return logger def get_tqdm_logger(name: str | None = None, level: str = "INFO") -> logging.Logger: """Get a tqdm-compatible logger instance. Args: name: Logger name (defaults to calling module) level: Logging level Returns ------- logging.Logger: Tqdm-compatible logger instance """ if name is None: name = "python_deadlines" logger = logging.getLogger(name) # Set up tqdm-compatible configuration if not already configured if not logger.handlers: return setup_tqdm_logging(level=level) return logger |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |