This directory contains examples of how to use LogLama in different scenarios, from simple Python scripts to complex multi-component workflows. These examples demonstrate how to use LogLama as the primary service for centralized environment management and logging in the PyLama ecosystem.
The basic_python_example.py
script demonstrates how to use LogLama for logging in a simple Python script. It shows how to initialize the logger, set log levels, and log messages at different levels.
python basic_python_example.py
The standalone_example.py
script is a self-contained example that includes all necessary imports and fallback mechanisms to use LogLama in a single file. This makes it easy to copy and use in any project, even if the full PyLama ecosystem is not installed.
python standalone_example.py
The bash_example.sh
script demonstrates how to use LogLama from a Bash script. It shows how to initialize the LogLama environment and log messages at different levels from a shell script.
chmod +x bash_example.sh
./bash_example.sh
The devlama_integration_example.py
script demonstrates how to use LogLama in a script that interacts with other PyLama components like PyLLM and BEXY. It shows how to leverage the centralized environment system to access shared configuration and dependencies.
python devlama_integration_example.py
The multi_component_example
directory contains a comprehensive example of using LogLama in a multi-component workflow. It demonstrates how to use LogLama to coordinate and log a workflow that involves multiple components and scripts.
run_workflow.sh
- The main script that orchestrates the workflowdata_collector.py
- Collects data from various sourcesdata_processor.py
- Processes the collected datamodel_inference.py
- Performs model inference on the processed dataresults_analyzer.py
- Analyzes the inference results and generates a reportcd multi_component_example
chmod +x run_workflow.sh
./run_workflow.sh
After running any of these examples, you can view the logs using the LogLama CLI:
python -m loglama.cli.main logs
Or view them in the web interface:
python -m loglama.cli.main web
To use LogLama in your own projects, follow these steps:
from loglama.core.logger import get_logger, setup_logging
from loglama.core.env_manager import load_central_env
load_central_env()
setup_logging()
logger = get_logger("your_module_name")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
# Log with context
# Note: Avoid using reserved LogRecord attributes in 'extra'
# Reserved names include: 'args', 'asctime', 'created', 'exc_info', 'exc_text',
# 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module',
# 'msecs', 'message', 'msg', 'name', 'pathname', 'process',
# 'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName'
logger.info("Processing file", extra={"file_name": "example.txt", "file_size": 1024})
# Log with timing
with logger.time("operation_time"):
# Your code here
pass
To use LogLama from a Bash script, you can use the following pattern:
#!/bin/bash
# Set the path to the PyLama root directory
DEVLAMA_ROOT="/path/to/py-lama"
# Function to log a message using LogLama
log_message() {
local level="$1"
local message="$2"
local component="${3:-bash_script}"
# Use Python to call LogLama's logger
python3 -c "import sys; sys.path.append('$DEVLAMA_ROOT'); from loglama.core.logger import get_logger; logger = get_logger('$component'); logger.$level('$message')"
}
# Initialize LogLama environment
initialize_loglama() {
python3 -c "import sys; sys.path.append('$DEVLAMA_ROOT'); from loglama.core.env_manager import load_central_env; load_central_env()"
}
# Initialize LogLama
initialize_loglama
# Log messages
log_message "info" "Starting script"
log_message "warning" "This is a warning"
log_message "error" "This is an error"