Tags
Tags allow you to categorize and organize your logs for easier filtering and analysis. Use tags to group related logs by feature, module, or any custom criteria.
Adding Tags
Use the tags property in the options object:
JavaScript
// Add multiple tags
Log.info('Database query completed', {
tags: ['performance', 'database']
});
// Single tag
Log.warn('Failed login attempt', {
tags: ['authentication']
});
// Tags with context
Log.error('External API call failed', {
tags: ['api', 'external'],
context: { endpoint: '/payments' }
}, error);// Add multiple tags
Log.info('Database query completed', {
tags: ['performance', 'database']
});
// Single tag
Log.warn('Failed login attempt', {
tags: ['authentication']
});
// Tags with context
Log.error('External API call failed', {
tags: ['api', 'external'],
context: { endpoint: '/payments' }
}, error);Common Tag Categories
Feature-Based Tags
JavaScript
// Authentication feature
Log.info('Login attempt successful', {
tags: ['authentication', 'security']
});
// Payment processing
Log.error('Payment gateway timeout', {
tags: ['payment', 'transaction']
});
// Database operations
Log.warn('Slow query detected', {
tags: ['database', 'performance']
});// Authentication feature
Log.info('Login attempt successful', {
tags: ['authentication', 'security']
});
// Payment processing
Log.error('Payment gateway timeout', {
tags: ['payment', 'transaction']
});
// Database operations
Log.warn('Slow query detected', {
tags: ['database', 'performance']
});Component-Based Tags
JavaScript
// Cache operations
Log.info('Cache entry expired', {
tags: ['cache', 'redis']
});
// External APIs
Log.error('External service unavailable', {
tags: ['api', 'external', 'payment-gateway']
});
// Background jobs
Log.info('Email processing completed', {
tags: ['background-job', 'email']
});// Cache operations
Log.info('Cache entry expired', {
tags: ['cache', 'redis']
});
// External APIs
Log.error('External service unavailable', {
tags: ['api', 'external', 'payment-gateway']
});
// Background jobs
Log.info('Email processing completed', {
tags: ['background-job', 'email']
});Express.js Integration
JavaScript
app.post('/api/orders', async (req, res) => {
Log.info('Order creation started', {
tags: ['api', 'orders', 'create'],
context: { customerId: req.body.customerId }
});
try {
const order = await orderService.create(req.body);
Log.info('Order created successfully', {
tags: ['api', 'orders', 'success'],
context: { orderId: order.id }
});
res.json(order);
} catch (error) {
Log.error('Order creation failed', {
tags: ['api', 'orders', 'error']
}, error);
res.status(500).json({ error: 'Failed to create order' });
}
});app.post('/api/orders', async (req, res) => {
Log.info('Order creation started', {
tags: ['api', 'orders', 'create'],
context: { customerId: req.body.customerId }
});
try {
const order = await orderService.create(req.body);
Log.info('Order created successfully', {
tags: ['api', 'orders', 'success'],
context: { orderId: order.id }
});
res.json(order);
} catch (error) {
Log.error('Order creation failed', {
tags: ['api', 'orders', 'error']
}, error);
res.status(500).json({ error: 'Failed to create order' });
}
});Tag Conventions
Naming Standards
JavaScript
// ✅ Good - Lowercase with hyphens
Log.info('Profile updated successfully', {
tags: ['user-management', 'profile']
});
// ✅ Good - Simple and clear
Log.warn('Slow query detected', {
tags: ['database', 'performance']
});
// ❌ Avoid - Inconsistent casing and spaces
Log.info('Profile updated', {
tags: ['UserManagement', 'PROFILE']
});// ✅ Good - Lowercase with hyphens
Log.info('Profile updated successfully', {
tags: ['user-management', 'profile']
});
// ✅ Good - Simple and clear
Log.warn('Slow query detected', {
tags: ['database', 'performance']
});
// ❌ Avoid - Inconsistent casing and spaces
Log.info('Profile updated', {
tags: ['UserManagement', 'PROFILE']
});Filtering and Analysis
Tags make it easy to filter and analyze logs:
JavaScript
// All payment-related logs can be filtered by "payment" tag
Log.info('Payment processing started', {
tags: ['payment', 'gateway']
});
Log.warn('Payment retry attempt', {
tags: ['payment', 'retry']
});
Log.error('Payment processing failed', {
tags: ['payment', 'error']
});// All payment-related logs can be filtered by "payment" tag
Log.info('Payment processing started', {
tags: ['payment', 'gateway']
});
Log.warn('Payment retry attempt', {
tags: ['payment', 'retry']
});
Log.error('Payment processing failed', {
tags: ['payment', 'error']
});Best Practices
Best Practices
- Use lowercase with hyphens for consistency (e.g., "user-management")
- Keep tags short and meaningful
- Use multiple tags for better filtering capabilities
- Document your tagging strategy for the team
- Combine tags with metadata and context for comprehensive logging
Next Steps
- Metadata & Context - Add context to individual logs
- Global Metadata - Add consistent context to all logs
- User Identification - Associate logs with users