PYTHON Reference Guide
Revision Time: 3 mins

Logging Reference

Standard logger outputs, configuration levels, and file tracking.

basicConfig()
Return: None

Configure basic settings for default logger.

Used In: Setting up logs in simple scripts and ETL files.

Syntax signature:logging.basicConfig(**kwargs)
Code snippet:
python
import logging
logging.basicConfig(level=logging.INFO)
logging.info('Pipeline Started')
Expected Output:INFO:root:Pipeline Started

Time Complexity: O(1)

Common Mistakes: Calling basicConfig() multiple times; only the first call configures the root logger.

Related Methods: getLogger()

Comparison:

print() vs logging: print() prints directly to standard output; logging supports log levels, handlers, and formats.

Remember: Should be called at the start of your script before logging messages.
Logging Levels
Return: None

Show hierarchical importance of log statements.

Used In: Filtering verbose logs in production pipelines.

Syntax signature:DEBUG < INFO < WARNING < ERROR < CRITICAL
Code snippet:
python
import logging
logging.warning('Disk space 85%')
Expected Output:WARNING:root:Disk space 85%

Time Complexity: O(1)

Remember: Default root logger level is WARNING; messages below this level are ignored unless basicConfig(level) is configured.
FileHandler
Return: FileHandler

Redirect log statements to write directly to a local file.

Used In: Persisting pipeline traces.

Syntax signature:logging.FileHandler(filename)
Code snippet:
python
import logging
handler = logging.FileHandler('etl.log')
print(handler.baseFilename.endswith('etl.log'))
Expected Output:True

Time Complexity: O(1)

Related Methods: StreamHandler

Remember: Commonly attached to custom loggers to capture audit files.
StreamHandler
Return: StreamHandler

Direct log statements to standard output console stream.

Used In: Docker container logging setups.

Syntax signature:logging.StreamHandler(sys.stdout)
Code snippet:
python
import logging, sys
handler = logging.StreamHandler(sys.stdout)
print(type(handler))
Expected Output:<class 'logging.StreamHandler'>

Time Complexity: O(1)

Related Methods: FileHandler

Remember: Ideal for container environments (Docker, Kubernetes) which capture stdout logs automatically.
Formatter
Return: Formatter

Standardize layout formatting of log message fields.

Used In: Formatting production log layouts.

Syntax signature:logging.Formatter(format_string)
Code snippet:
python
import logging
f = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
print(type(f))
Expected Output:<class 'logging.Formatter'>

Time Complexity: O(1)

Remember: Attaches to FileHandlers and StreamHandlers to display consistent timestamps.
logger.exception()
Return: None

Log message with ERROR level severity, appending full stack exception traceback.

Used In: Production pipelines error diagnostics.

Syntax signature:logger.exception(msg)
Code snippet:
python
import logging
logger = logging.getLogger('dev')
try:
    1 / 0
except ZeroDivisionError:
    logger.exception('Math error')
# Logs the message and stack trace
Expected Output:ERROR:dev:Math error\nTraceback (most recent call last)...

Time Complexity: O(1)

Remember: Only call inside except blocks; automatically gathers exception traceback variables.
RotatingFileHandler
Return: RotatingFileHandler

Log to local files, rotating records once size limit bounds are crossed.

Used In: Production log rotation setups.

Syntax signature:RotatingFileHandler(file, maxBytes=1000, backupCount=3)
Code snippet:
python
from logging.handlers import RotatingFileHandler
h = RotatingFileHandler('app.log', maxBytes=5000000, backupCount=5)
print(type(h))
Expected Output:<class 'logging.handlers.RotatingFileHandler'>

Time Complexity: O(1)

Related Methods: TimedRotatingFileHandler

Remember: Prevents log files from growing infinitely and crashing server storage capacity.
Logging Best Practices
Return: None

Key guidelines for logging in production.

Used In: Code audits.

Syntax signature:N/A
Code snippet:
python
# Use named loggers, avoid bare print() statements
Expected Output:N/A

Time Complexity: O(1)

Remember: 1. Standardize logging formats. 2. Capture stack traces using logger.exception. 3. Utilize rotating handlers. 4. Avoid writing sensitive secrets in logs.