Basic Logging
Basic Logging
The ByteHide Logs SDK for Python provides six logging levels that cover the full spectrum of application events, from debug information to critical errors.
Logging Levels
The SDK supports the standard logging levels:
Python
from bytehide_logs import Log, LogLevel
# Debug level - detailed information for debugging
Log.debug("Application started in development mode")
# Info level - general informational messages
Log.info("User login successful")
# Warning level - warning messages about potential issues
Log.warning("API rate limit approaching: 95% used")
# Error level - error messages for recoverable errors
Log.error("Failed to connect to database")
# Critical level - critical messages for severe errors
Log.critical("Payment processing service unavailable")
# Trace level - extremely detailed diagnostic information
Log.trace("Processing request in authentication middleware")from bytehide_logs import Log, LogLevel
# Debug level - detailed information for debugging
Log.debug("Application started in development mode")
# Info level - general informational messages
Log.info("User login successful")
# Warning level - warning messages about potential issues
Log.warning("API rate limit approaching: 95% used")
# Error level - error messages for recoverable errors
Log.error("Failed to connect to database")
# Critical level - critical messages for severe errors
Log.critical("Payment processing service unavailable")
# Trace level - extremely detailed diagnostic information
Log.trace("Processing request in authentication middleware")Logging with Exception Handling
Capture exception details automatically using the exception parameter:
Python
try:
result = perform_payment()
except Exception as e:
Log.error("Payment processing failed", exception=e)try:
result = perform_payment()
except Exception as e:
Log.error("Payment processing failed", exception=e)The SDK automatically captures:
- Exception type and message
- Full stack trace
- Exception chain (if applicable)
Logging with Context
Add contextual data to your log entries using the context parameter:
Python
Log.error(
"Database query failed",
exception=query_exception,
context={
"query": "SELECT * FROM users WHERE id = ?",
"database": "production",
"retry_count": 3
}
)Log.error(
"Database query failed",
exception=query_exception,
context={
"query": "SELECT * FROM users WHERE id = ?",
"database": "production",
"retry_count": 3
}
)Context helps with:
- Debugging specific issues
- Correlating related logs
- Understanding the state when an error occurred
Fluent Syntax
Chain multiple configuration methods together for expressive logging:
Python
Log.with_tags("payment", "stripe") \
.with_context("transaction_id", "txn_123456") \
.with_context("amount_cents", 9999) \
.info("Payment processed successfully")Log.with_tags("payment", "stripe") \
.with_context("transaction_id", "txn_123456") \
.with_context("amount_cents", 9999) \
.info("Payment processed successfully")Fluent syntax allows you to:
- Add multiple tags in a single call
- Build up context progressively
- Write readable, self-documenting logs
Fluent vs Direct Methods
Choose the style that fits your workflow:
Python
# Fluent style - method chaining
Log.with_tags("auth").with_context("user_id", 42).info("Login successful")
# Direct style - straightforward
Log.info("Login successful", context={"user_id": 42})# Fluent style - method chaining
Log.with_tags("auth").with_context("user_id", 42).info("Login successful")
# Direct style - straightforward
Log.info("Login successful", context={"user_id": 42})Both approaches are equally supported and produce identical log entries.
Complete Example
Python
from bytehide_logs import Log
def process_order(order_id, user_id):
"""Process a customer order with comprehensive logging."""
try:
Log.info(f"Starting order processing for order {order_id}")
# Perform order validation
validate_order(order_id)
# Process payment
payment_result = process_payment(order_id)
Log.with_tags("order", "payment") \
.with_context("order_id", order_id) \
.with_context("payment_id", payment_result.id) \
.info("Order processed successfully")
except ValidationError as e:
Log.warning(
f"Order validation failed for order {order_id}",
exception=e,
context={"order_id": order_id}
)
raise
except PaymentError as e:
Log.error(
f"Payment processing failed for order {order_id}",
exception=e,
context={
"order_id": order_id,
"user_id": user_id,
"retry_attempt": 1
}
)
raisefrom bytehide_logs import Log
def process_order(order_id, user_id):
"""Process a customer order with comprehensive logging."""
try:
Log.info(f"Starting order processing for order {order_id}")
# Perform order validation
validate_order(order_id)
# Process payment
payment_result = process_payment(order_id)
Log.with_tags("order", "payment") \
.with_context("order_id", order_id) \
.with_context("payment_id", payment_result.id) \
.info("Order processed successfully")
except ValidationError as e:
Log.warning(
f"Order validation failed for order {order_id}",
exception=e,
context={"order_id": order_id}
)
raise
except PaymentError as e:
Log.error(
f"Payment processing failed for order {order_id}",
exception=e,
context={
"order_id": order_id,
"user_id": user_id,
"retry_attempt": 1
}
)
raiseNext Steps
- Learn about exception handling for comprehensive error logging
- Explore tags to organize logs by categories
- Discover correlation IDs for request tracking