Quick Start
Quick Start
Get logging running in your Python application in under 5 minutes.
Step 1: Set Your Project Token
Get your project token from the ByteHide Dashboard and set it in your application:
Python
from bytehide_logs import Log
Log.set_project_token("your-project-token-here")from bytehide_logs import Log
Log.set_project_token("your-project-token-here")Step 2: Initialize Logging
Create and configure a LogSettings object, then initialize:
Python
from bytehide_logs import Log, LogSettings
settings = LogSettings(console_enabled=True)
Log.initialize(settings)from bytehide_logs import Log, LogSettings
settings = LogSettings(console_enabled=True)
Log.initialize(settings)Step 3: Start Logging
Use any of the six log levels:
Python
Log.trace("Detailed trace information")
Log.debug("Debug message")
Log.info("Information message")
Log.warn("Warning message")
Log.error("Error message")
Log.critical("Critical error")Log.trace("Detailed trace information")
Log.debug("Debug message")
Log.info("Information message")
Log.warn("Warning message")
Log.error("Error message")
Log.critical("Critical error")Complete Example
Python
from bytehide_logs import Log, LogSettings, LogLevel
if __name__ == "__main__":
# 1. Configure token
Log.set_project_token("your-project-token")
# 2. Initialize settings
settings = LogSettings(
console_enabled=True,
minimum_level=LogLevel.INFO,
include_caller_info=True
)
Log.initialize(settings)
# 3. Start logging
Log.info("Application starting")
try:
# Your application code
result = 10 / 2
Log.info(f"Calculation result: {result}")
except Exception as e:
Log.error("Calculation failed", exception=e)
finally:
Log.info("Application shutdown")
Log.flush()from bytehide_logs import Log, LogSettings, LogLevel
if __name__ == "__main__":
# 1. Configure token
Log.set_project_token("your-project-token")
# 2. Initialize settings
settings = LogSettings(
console_enabled=True,
minimum_level=LogLevel.INFO,
include_caller_info=True
)
Log.initialize(settings)
# 3. Start logging
Log.info("Application starting")
try:
# Your application code
result = 10 / 2
Log.info(f"Calculation result: {result}")
except Exception as e:
Log.error("Calculation failed", exception=e)
finally:
Log.info("Application shutdown")
Log.flush()Adding Context
Make your logs more useful with contextual information:
Tags
Tag logs for easy filtering:
Python
Log.with_tags("auth", "user").info("User logged in")Log.with_tags("auth", "user").info("User logged in")Custom Context
Add key-value data to logs:
Python
Log.with_context("user_id", "12345").info("Action performed")Log.with_context("user_id", "12345").info("Action performed")Correlation ID
Track requests across multiple logs:
Python
Log.with_correlation_id("req-789").info("Request started")Log.with_correlation_id("req-789").info("Request started")Chaining
Combine multiple contexts:
Python
Log.with_tags("api").with_context("endpoint", "/users").with_correlation_id("req-123").info("API call")Log.with_tags("api").with_context("endpoint", "/users").with_correlation_id("req-123").info("API call")User Identification
Track logs by authenticated users:
Python
from bytehide_logs import AuthUser
user = AuthUser(
id="user-123",
email="user@example.com",
token="user-token"
)
Log.identify(user, update_previous_logs=True)
Log.info("User action logged")
# Later, when user logs out
Log.logout()from bytehide_logs import AuthUser
user = AuthUser(
id="user-123",
email="user@example.com",
token="user-token"
)
Log.identify(user, update_previous_logs=True)
Log.info("User action logged")
# Later, when user logs out
Log.logout()File Logging
Enable file persistence with rotation:
Python
from bytehide_logs import Log, LogSettings, RollingInterval
settings = LogSettings(
console_enabled=True,
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.DAY
)
Log.initialize(settings)from bytehide_logs import Log, LogSettings, RollingInterval
settings = LogSettings(
console_enabled=True,
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.DAY
)
Log.initialize(settings)Production Setup
Python
from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
import os
if __name__ == "__main__":
# Get token from environment
token = os.getenv("PROJECT_TOKEN")
if not token:
raise ValueError("PROJECT_TOKEN environment variable not set")
Log.set_project_token(token)
# Production settings
settings = LogSettings(
console_enabled=False, # Disable console in production
persist=True,
file_path="/var/log/myapp/app.log",
minimum_level=LogLevel.INFO,
include_caller_info=True,
rolling_interval=RollingInterval.DAY,
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800, # 50MB
mask_sensitive_data=["password", "api_key", "token"]
)
Log.initialize(settings)
try:
# Application code
Log.info("Application started")
# ... your application logic ...
except Exception as e:
Log.error("Application error", exception=e)
finally:
Log.flush()from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
import os
if __name__ == "__main__":
# Get token from environment
token = os.getenv("PROJECT_TOKEN")
if not token:
raise ValueError("PROJECT_TOKEN environment variable not set")
Log.set_project_token(token)
# Production settings
settings = LogSettings(
console_enabled=False, # Disable console in production
persist=True,
file_path="/var/log/myapp/app.log",
minimum_level=LogLevel.INFO,
include_caller_info=True,
rolling_interval=RollingInterval.DAY,
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800, # 50MB
mask_sensitive_data=["password", "api_key", "token"]
)
Log.initialize(settings)
try:
# Application code
Log.info("Application started")
# ... your application logic ...
except Exception as e:
Log.error("Application error", exception=e)
finally:
Log.flush()Environment-Based Configuration
Python
import os
from bytehide_logs import Log, LogSettings, LogLevel
def setup_logging():
env = os.getenv("ENV", "development")
Log.set_project_token(os.getenv("PROJECT_TOKEN"))
if env == "production":
settings = LogSettings(
console_enabled=False,
persist=True,
minimum_level=LogLevel.WARN
)
else:
settings = LogSettings(
console_enabled=True,
persist=False,
minimum_level=LogLevel.DEBUG
)
Log.initialize(settings)
if __name__ == "__main__":
setup_logging()
Log.info("Running in production" if os.getenv("ENV") == "production" else "Running in development")import os
from bytehide_logs import Log, LogSettings, LogLevel
def setup_logging():
env = os.getenv("ENV", "development")
Log.set_project_token(os.getenv("PROJECT_TOKEN"))
if env == "production":
settings = LogSettings(
console_enabled=False,
persist=True,
minimum_level=LogLevel.WARN
)
else:
settings = LogSettings(
console_enabled=True,
persist=False,
minimum_level=LogLevel.DEBUG
)
Log.initialize(settings)
if __name__ == "__main__":
setup_logging()
Log.info("Running in production" if os.getenv("ENV") == "production" else "Running in development")Common Patterns
Exception Logging with Context
Python
try:
user_id = "user-123"
result = process_user(user_id)
except Exception as e:
Log.error(
"Failed to process user",
exception=e,
context={
"user_id": user_id,
"operation": "process_user"
}
)try:
user_id = "user-123"
result = process_user(user_id)
except Exception as e:
Log.error(
"Failed to process user",
exception=e,
context={
"user_id": user_id,
"operation": "process_user"
}
)Sensitive Data Protection
Python
settings = LogSettings(
mask_sensitive_data=["password", "token", "api_key"]
)
# This will be automatically masked
Log.info(f"Login attempt with password: {user_password}")
# Output: Login attempt with password: ***settings = LogSettings(
mask_sensitive_data=["password", "token", "api_key"]
)
# This will be automatically masked
Log.info(f"Login attempt with password: {user_password}")
# Output: Login attempt with password: ***Request Tracking
Python
def handle_request(request_id, user_id):
with_correlation_id = Log.with_correlation_id(request_id)
with_correlation_id.info("Request received")
with_correlation_id.with_context("user_id", user_id).info("Processing")
with_correlation_id.info("Request completed")def handle_request(request_id, user_id):
with_correlation_id = Log.with_correlation_id(request_id)
with_correlation_id.info("Request received")
with_correlation_id.with_context("user_id", user_id).info("Processing")
with_correlation_id.info("Request completed")Next Steps
- Configuration - Customize all settings
- Project Token - Learn about token management
- Log Levels - Understand log filtering
- File Logging - Advanced file configuration