Guides
Logging
Logs that are actually useful. Not just console.log('here').
Logging
Structured logs you can search. Levels to filter noise. Automatic user context.
Usage
import { useLog } from '@thisbefine/analytics/react';
const MyComponent = () => {
const log = useLog();
const handlePayment = async () => {
log('Payment initiated', 'info', { amount: 99.99, currency: 'USD' });
try {
const result = await processPayment();
log('Payment successful', 'info', { transactionId: result.id });
} catch (error) {
log('Payment failed', 'error', { errorCode: error.code });
}
};
return <button onClick={handlePayment}>Pay Now</button>;
};getAnalytics()?.log('User action completed', 'info', { action: 'export', format: 'csv' });Log Levels
| Level | Use Case |
|---|---|
debug | Dev details, cache hits |
info | Normal operations |
warn | Concerning but not broken |
error | Something broke |
fatal | Critical failure |
log('Cache hit', 'debug', { key: 'user_123' });
log('User logged in', 'info', { method: 'oauth' });
log('Rate limit approaching', 'warn', { current: 900, limit: 1000 });
log('API request failed', 'error', { endpoint: '/api/users', status: 500 });
log('Database connection lost', 'fatal', { host: 'db.example.com' });Structured Metadata
Attach searchable metadata instead of string concatenation:
log('Order processed', 'info', {
orderId: 'ord_123',
userId: 'user_456',
itemCount: 3,
totalAmount: 149.97,
paymentMethod: 'stripe',
});Keep metadata under 10KB per log.
Common Patterns
// Application lifecycle
log('Application starting', 'info', { version: '1.0.0', environment: 'production' });
// User actions
log('Feature used', 'info', { feature: 'export', format: 'csv' });
// API operations
log('API request', 'debug', { method: 'POST', endpoint: '/api/orders' });
log('API error', 'error', { endpoint: '/api/orders', status: 500 });
// Background jobs
log('Job completed', 'info', { jobId: 'job_123', duration: 5234 });
log('Job failed', 'error', { jobId: 'job_123', error: 'SMTP timeout' });Logs vs Events vs Errors
| Type | Use Case |
|---|---|
track() | User actions, business metrics |
log() | Debugging, operational info |
captureException() | Application failures |
User Context
Logs automatically include userId after identify(). No manual passing needed.
Performance
Logs are batched with other events. Use debug level sparingly in production.
Searching
Filter by level, message, metadata fields, user, or time range in the dashboard.
Best Practices
Be descriptive: Include what failed, where, and why.
Include context: Add relevant IDs, endpoints, status codes.
Use appropriate levels: Don't make everything info.
Never log sensitive data: No passwords, API keys, credit cards, or SSNs.
Never log passwords or other sensitive data.