usage-logging
npx machina-cli add skill athola/claude-night-market/usage-logging --openclawTable of Contents
- Overview
- When to Use
- Core Concepts
- Session Management
- Log Entry Structure
- Quick Start
- Initialize Logger
- Log Operations
- Query Usage
- Integration Pattern
- Log Storage
- Detailed Resources
- Exit Criteria
Usage Logging
Overview
Session-aware logging infrastructure for tracking operations across plugins. Provides structured JSONL logging with automatic session management for audit trails and analytics.
When To Use
- Need audit trails for operations
- Tracking costs across sessions
- Building usage analytics
- Debugging with operation history
When NOT To Use
- Simple operations without logging needs
Core Concepts
Session Management
Sessions group related operations:
- Auto-created on first operation
- Timeout after 1 hour of inactivity
- Unique session IDs for tracking
Log Entry Structure
{
"timestamp": "2025-12-05T10:30:00Z",
"session_id": "session_1733394600",
"service": "my-service",
"operation": "analyze_files",
"tokens": 5000,
"success": true,
"duration_seconds": 2.5,
"metadata": {}
}
Verification: Run the command with --help flag to verify availability.
Quick Start
Initialize Logger
from leyline.usage_logger import UsageLogger
logger = UsageLogger(service="my-service")
Verification: Run the command with --help flag to verify availability.
Log Operations
logger.log_usage(
operation="analyze_files",
tokens=5000,
success=True,
duration=2.5,
metadata={"files": 10}
)
Verification: Run the command with --help flag to verify availability.
Query Usage
# Recent operations
recent = logger.get_recent_operations(hours=24)
# Usage summary
summary = logger.get_usage_summary(days=7)
print(f"Total tokens: {summary['total_tokens']}")
print(f"Total cost: ${summary['estimated_cost']:.2f}")
# Recent errors
errors = logger.get_recent_errors(count=10)
Verification: Run the command with --help flag to verify availability.
Integration Pattern
# In your skill's frontmatter
dependencies: [leyline:usage-logging]
Verification: Run the command with --help flag to verify availability.
Standard integration flow:
- Initialize logger for your service
- Log operations after completion
- Query for analytics and debugging
Log Storage
Default location: ~/.claude/leyline/usage/{service}.jsonl
# View recent logs
tail -20 ~/.claude/leyline/usage/my-service.jsonl | jq .
# Query by date
grep "2025-12-05" ~/.claude/leyline/usage/my-service.jsonl
Verification: Run the command with --help flag to verify availability.
Detailed Resources
- Session Patterns: See
modules/session-patterns.mdfor session management - Log Formats: See
modules/log-formats.mdfor structured formats
Exit Criteria
- Operation logged with all required fields
- Session tracked for grouping
- Logs queryable for analytics
Troubleshooting
Common Issues
Command not found Ensure all dependencies are installed and in PATH
Permission errors Check file permissions and run with appropriate privileges
Unexpected behavior
Enable verbose logging with --verbose flag
Source
git clone https://github.com/athola/claude-night-market/blob/master/plugins/leyline/skills/usage-logging/SKILL.mdView on GitHub Overview
Usage Logging provides a session-aware logging infrastructure for tracking operations across plugins. It delivers structured JSONL logs with automatic session management to support audit trails and analytics.
How This Skill Works
Initialize a UsageLogger for your service, then call log_usage after each operation. Logs are emitted as structured JSON lines containing fields like timestamp, session_id, service, operation, tokens, duration, and metadata. Sessions auto-create on first operation and timeout after 1 hour of inactivity.
When to Use It
- Audit trails for operations
- Tracking costs across sessions
- Building usage analytics
- Debugging with operation history
- Managing session logging across plugins
Quick Start
- Step 1: Initialize the logger from leyline.usage_logger import UsageLogger logger = UsageLogger(service="my-service")
- Step 2: Log an operation logger.log_usage(operation="analyze_files", tokens=5000, success=True, duration=2.5, metadata={"files": 10})
- Step 3: Query usage recent = logger.get_recent_operations(hours=24); summary = logger.get_usage_summary(days=7)
Best Practices
- Use a single UsageLogger per service to keep sessions consistent
- Enforce the structured JSONL format for all entries
- Rely on auto-created sessions and the 1-hour timeout
- Include all required fields: timestamp, session_id, service, operation, tokens, duration, success, metadata
- Regularly back up and secure log storage at ~/.claude/leyline/usage/{service}.jsonl
Example Use Cases
- Audit file-processing workflows by logging each analyze_files operation with tokens and duration
- Track token usage and costs across sessions for a billing-friendly view
- Aggregate usage analytics across plugins to improve UX and resource planning
- Debug failures by inspecting recent operations and session context
- Maintain session-based logs for multi-step collaboration tasks
Frequently Asked Questions
Related Skills
terraform
chaterm/terminal-skills
Terraform 基础设施即代码
makefile-generation
athola/claude-night-market
Generate language-specific Makefiles with testing, linting, and automation targets. Use for project initialization and workflow standardization. Skip if Makefile exists.
precommit-setup
athola/claude-night-market
Configure three-layer pre-commit system with linting, type checking, and testing hooks. Use for quality gate setup and code standards. Skip if pre-commit is optimally configured.
error-patterns
athola/claude-night-market
'Standardized error handling patterns with classification, recovery,
risk-classification
athola/claude-night-market
'Inline risk classification for agent tasks using a 4-tier model. Hybrid
quota-management
athola/claude-night-market
'Quota tracking, threshold monitoring, and graceful degradation for rate-limited