A easier way to log data to database or files when building applications fast.
Project description
AI Logging Guide
Overview
The Logger class now supports automatic logging of AI model interactions when using the @logger.log(include_ai=True) decorator.
How It Works
1. Decorator Setup
Use the include_ai=True parameter when decorating your AI function:
@my_logger.log(include_ai=True)
def AI_Run(content):
# Your AI code here
return response # Must return the full response object
2. Return the Full Response Object
IMPORTANT: Your function must return the complete Azure OpenAI response object, not just the content string.
# ✅ CORRECT
return response
# ❌ WRONG
return response.choices[0].message.content
3. What Gets Captured
The decorator automatically extracts and logs:
- AI Provider: "Azure OpenAI" (hardcoded, can be modified)
- Model:
response.model(e.g., "gpt-4", "gpt-3.5-turbo") - Prompt: First positional argument or
contentkeyword argument - Completion:
response.choices[0].message.content - Tokens Prompt:
response.usage.prompt_tokens - Tokens Completion:
response.usage.completion_tokens - Tokens Total:
response.usage.total_tokens - Timestamp: Automatically generated
4. Logging Destinations
File Logging (Always)
AI metrics are logged to your log file:
16 October 2025 14:23:45.123 - INFO - Calling function: AI_Run
16 October 2025 14:23:46.456 - INFO - AI Provider: Azure OpenAI
16 October 2025 14:23:46.456 - INFO - Model: gpt-4
16 October 2025 14:23:46.456 - INFO - Prompt Tokens: 15
16 October 2025 14:23:46.456 - INFO - Completion Tokens: 150
16 October 2025 14:23:46.456 - INFO - Total Tokens: 165
Database Logging (If Enabled)
If include_database=True in Logger initialization, AI interactions are inserted into your database table.
Required Database Schema:
CREATE TABLE ConsumptionLogs (
LogID INT PRIMARY KEY IDENTITY(1,1),
AIProvider VARCHAR(100),
Model VARCHAR(100),
Prompt TEXT,
Completion TEXT,
TokensPrompt INT,
TokensCompletion INT,
TokensTotal INT,
userId VARCHAR(100) NULL,
userName VARCHAR(100) NULL,
userEmail VARCHAR(100) NULL,
userDept VARCHAR(100) NULL,
companyCode VARCHAR(100) NULL,
LogTime DATETIME DEFAULT GETDATE()
)
5. Complete Example
from betterlogger.main import Logger
from openai import AzureOpenAI
import os
import dotenv
# Initialize logger with database support
my_logger = Logger(
log_file_name="AI_System",
log_dir="./logs",
include_database=True, # Enable database logging
database_username="your_username",
database_password="your_password",
database_server="your_server",
database_name="your_database",
database_type="mssql",
table={"table_name": "ConsumptionLogs"}
)
@my_logger.log(include_ai=True)
def AI_Run(content):
dotenv.load_dotenv()
client = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2025-01-01-preview",
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
)
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": content
}
]
)
print(response.choices[0].message.content)
return response # MUST return full response object
# Use the function
result = AI_Run("Write a poem about Python logging")
Technical Details
How Detection Works
The decorator checks if the returned object has these attributes:
hasattr(result, 'usage') and hasattr(result, 'model')
If both exist, it treats it as an AI response and extracts the data.
Error Handling
- If AI logging fails, an error is logged but the function continues
- The original function's return value is preserved
- Exceptions in AI logging don't break your application
Supported AI Providers
Currently optimized for Azure OpenAI, but can be extended to support:
- OpenAI API
- Anthropic Claude
- Google PaLM
- Other providers with similar response structures
Customization
Adding User Context
You can extend _log_ai_interaction() to accept additional parameters:
self._log_ai_interaction(
ai_provider="Azure OpenAI",
model=result.model,
prompt=prompt,
completion=completion,
tokens_prompt=result.usage.prompt_tokens,
tokens_completion=result.usage.completion_tokens,
tokens_total=result.usage.total_tokens,
timestamp=timestamp,
user_id="user123", # Optional
user_name="John Doe", # Optional
user_email="john@example.com", # Optional
user_dept="Engineering", # Optional
company_code="COMP001" # Optional
)
Changing AI Provider Detection
Modify the log() decorator to detect other AI providers:
if include_ai:
# Azure OpenAI
if hasattr(result, 'usage') and hasattr(result, 'model'):
# Azure OpenAI logging
# Anthropic Claude
elif hasattr(result, 'usage') and hasattr(result, 'model'):
# Claude logging
Troubleshooting
Issue: AI data not logging
Solution: Ensure you're returning the full response object, not just the content.
Issue: Database insert fails
Solution:
- Check database connection parameters
- Verify table schema matches expected structure
- Check logs for specific SQL errors
Issue: Tokens showing as 0
Solution: Verify your API response includes usage information. Some API configurations may not return token counts.
Benefits
- Automatic Token Tracking: Monitor AI usage costs automatically
- Audit Trail: Complete record of all AI interactions
- Performance Metrics: Track response times alongside token usage
- Error Tracking: Captures failures in AI calls with full context
- Compliance: Maintain logs of AI interactions for regulatory requirements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bester_logger-0.0.2.tar.gz.
File metadata
- Download URL: bester_logger-0.0.2.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c131914b480ffe0f5e8fb621a2ba3b50ed3099f300718c8d86a74b3baf312379
|
|
| MD5 |
de8b889ccffa424a8df0cc55903ea5a6
|
|
| BLAKE2b-256 |
6282e6d81985ecb4e4f6c3c805c5d29b6b487f7b84755b527882df04a42610a5
|
File details
Details for the file bester_logger-0.0.2-py3-none-any.whl.
File metadata
- Download URL: bester_logger-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b989a47806a31a200af3f2a7c839a59cc03816b4d9875523a69e19804eb75c55
|
|
| MD5 |
1f114bf27421fa657fef281f60420961
|
|
| BLAKE2b-256 |
262dc1f40ff0d1ffd8c2b6ed405c1d893aa5103a134014764e23ca70aae9c1a4
|