Logging Reference
Standard logger outputs, configuration levels, and file tracking.
Configure basic settings for default logger.
Used In: Setting up logs in simple scripts and ETL files.
logging.basicConfig(**kwargs)import logging
logging.basicConfig(level=logging.INFO)
logging.info('Pipeline Started')INFO:root:Pipeline StartedTime Complexity: O(1)
Common Mistakes: Calling basicConfig() multiple times; only the first call configures the root logger.
Related Methods: getLogger()
print() vs logging: print() prints directly to standard output; logging supports log levels, handlers, and formats.
Show hierarchical importance of log statements.
Used In: Filtering verbose logs in production pipelines.
DEBUG < INFO < WARNING < ERROR < CRITICALimport logging
logging.warning('Disk space 85%')WARNING:root:Disk space 85%Time Complexity: O(1)
Redirect log statements to write directly to a local file.
Used In: Persisting pipeline traces.
logging.FileHandler(filename)import logging
handler = logging.FileHandler('etl.log')
print(handler.baseFilename.endswith('etl.log'))TrueTime Complexity: O(1)
Related Methods: StreamHandler
Direct log statements to standard output console stream.
Used In: Docker container logging setups.
logging.StreamHandler(sys.stdout)import logging, sys
handler = logging.StreamHandler(sys.stdout)
print(type(handler))<class 'logging.StreamHandler'>Time Complexity: O(1)
Related Methods: FileHandler
Standardize layout formatting of log message fields.
Used In: Formatting production log layouts.
logging.Formatter(format_string)import logging
f = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
print(type(f))<class 'logging.Formatter'>Time Complexity: O(1)
Log message with ERROR level severity, appending full stack exception traceback.
Used In: Production pipelines error diagnostics.
logger.exception(msg)import logging
logger = logging.getLogger('dev')
try:
1 / 0
except ZeroDivisionError:
logger.exception('Math error')
# Logs the message and stack traceERROR:dev:Math error\nTraceback (most recent call last)...Time Complexity: O(1)
Log to local files, rotating records once size limit bounds are crossed.
Used In: Production log rotation setups.
RotatingFileHandler(file, maxBytes=1000, backupCount=3)from logging.handlers import RotatingFileHandler
h = RotatingFileHandler('app.log', maxBytes=5000000, backupCount=5)
print(type(h))<class 'logging.handlers.RotatingFileHandler'>Time Complexity: O(1)
Related Methods: TimedRotatingFileHandler
Key guidelines for logging in production.
Used In: Code audits.
N/A# Use named loggers, avoid bare print() statementsN/ATime Complexity: O(1)