patternpythonModeratepending
Python logging configuration best practices
Viewed 0 times
loggingbasicConfiggetLoggerexceptionjson loggingstructured
Problem
Python logging is misconfigured: messages don't appear, go to wrong destinations, or have insufficient context.
Solution
Proper logging setup:
Rules:
import logging
import sys
# Configure once at application entry point
def setup_logging(level='INFO'):
logging.basicConfig(
level=level,
format='%(asctime)s %(levelname)s %(name)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.StreamHandler(sys.stdout),
]
)
# Quiet noisy libraries
logging.getLogger('urllib3').setLevel(logging.WARNING)
logging.getLogger('botocore').setLevel(logging.WARNING)
# In each module: get a named logger
logger = logging.getLogger(__name__)
def process_order(order_id):
logger.info('Processing order %s', order_id)
try:
result = charge(order_id)
logger.info('Order %s charged successfully', order_id)
return result
except PaymentError as e:
logger.error('Payment failed for order %s: %s', order_id, e)
raise
except Exception:
logger.exception('Unexpected error processing order %s', order_id)
# .exception() includes stack trace automatically
raise
# For JSON structured logging in production
import json
class JSONFormatter(logging.Formatter):
def format(self, record):
return json.dumps({
'timestamp': self.formatTime(record),
'level': record.levelname,
'logger': record.name,
'message': record.getMessage(),
'module': record.module,
'line': record.lineno,
})Rules:
- Use
logger.info('msg %s', var)notlogger.info(f'msg {var}')(lazy formatting) - Use
logger.exception()in except blocks (includes traceback) - Get logger per module with
__name__ - Configure logging ONCE at entry point, not in libraries
Why
Python's logging module is powerful but its configuration is confusing. Getting it right once means consistent, useful logs across the entire application.
Gotchas
- basicConfig() only works once - second call is silently ignored
- Libraries should NOT call basicConfig() - only applications should
- print() is not logging - it bypasses log levels, formatting, and routing
Context
Python applications needing proper logging setup
Revisions (0)
No revisions yet.