Skip to content

Commit f3ca80e

Browse files
author
Kavyansh Chourasia
committed
Shiksha Ingestion: Pipeline Logging config INIT
1 parent 4668147 commit f3ca80e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Centralized logging configuration for all pipeline runners.
3+
4+
This module provides a consistent logging setup across all pipeline runner files.
5+
It configures logging to show INFO level and above, while suppressing DEBUG logs
6+
from noisy third-party modules.
7+
8+
Usage:
9+
In any pipeline runner file, simply import and use:
10+
11+
```python
12+
from logging_config import setup_logging
13+
14+
# Setup logging for this module
15+
logger = setup_logging("your_module_name")
16+
17+
# Use the logger
18+
logger.info("This is an info message")
19+
logger.error("This is an error message")
20+
```
21+
22+
Features:
23+
- Consistent log format across all modules
24+
- Suppresses DEBUG logs from noisy third-party libraries
25+
- Easy to use single function setup
26+
- Centralized configuration for easy maintenance
27+
"""
28+
29+
import logging
30+
from typing import Optional
31+
32+
33+
def setup_logging(logger_name: Optional[str] = None) -> logging.Logger:
34+
"""
35+
Set up logging configuration with INFO level and suppress DEBUG logs from noisy modules.
36+
37+
Args:
38+
logger_name: Name for the logger. If None, uses the calling module's name.
39+
40+
Returns:
41+
Configured logger instance.
42+
"""
43+
# Setup basic logging configuration
44+
logging.basicConfig(
45+
level=logging.INFO,
46+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
47+
)
48+
49+
# Set root logger level to INFO to suppress DEBUG logs
50+
logging.getLogger().setLevel(logging.INFO)
51+
52+
# Suppress DEBUG logs from common noisy modules
53+
_suppress_noisy_loggers()
54+
55+
# Create and return logger for the specific module
56+
if logger_name is None:
57+
logger_name = __name__
58+
59+
return logging.getLogger(logger_name)
60+
61+
62+
def _suppress_noisy_loggers():
63+
"""Suppress DEBUG logs from commonly noisy third-party modules."""
64+
noisy_modules = [
65+
"urllib3",
66+
"requests",
67+
"httpx",
68+
"httpcore",
69+
"openai",
70+
"asyncio",
71+
"boto3",
72+
"botocore",
73+
"s3transfer",
74+
"matplotlib",
75+
"PIL",
76+
"transformers",
77+
]
78+
79+
for module in noisy_modules:
80+
logging.getLogger(module).setLevel(logging.WARNING)
81+
82+
83+
def get_logger(name: str) -> logging.Logger:
84+
"""
85+
Get a logger instance for the given name.
86+
Assumes setup_logging() has already been called.
87+
88+
Args:
89+
name: Name for the logger (typically the module name).
90+
91+
Returns:
92+
Logger instance.
93+
"""
94+
return logging.getLogger(name)

0 commit comments

Comments
 (0)