[Feature] Added RFC for Q cli logger #1405
Open
+452
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This RFC proposes adding a logging feature to the Amazon Q CLI tool that will record user prompts, commands, responses, and related metadata in a structured format. The feature will be opt-in, enabled via a command-line flag or in-session command, and will provide users with commands to view, filter, and manage their interaction logs.
Motivation
Users of the Amazon Q CLI tool often need to:
Currently, there is no built-in way to record and review these interactions, forcing users to manually copy and paste important information or lose their history entirely when closing the terminal. This feature addresses this gap by providing a structured, searchable log of all interactions that users can easily access and manage.
The expected outcomes are:
Guide-level explanation
Enabling Logging
There are two ways to enable logging for a Q CLI session:
1. At Startup
Use the
--enable-logging
flag when starting the tool:2. During a Session
Enable logging during an active session using the
/log enable
command:Similarly, you can disable logging during a session:
When logging is enabled, a new log file is created for the current session in the standard system log location for your operating system:
~/Library/Logs/AmazonQ/
~/.local/share/amazon-q/logs/
%LOCALAPPDATA%\Amazon\Q\Logs\
Viewing Logs
Once logging is enabled, you can view your logs using the
/log
command within the Q CLI:This displays the 10 most recent log entries in the current session. Each entry includes:
Log Command Options
The
/log
command supports several options:Example Usage
Here's an example of enabling logging and using the log commands:
Start Q CLI with logging enabled:
Use Q CLI normally:
View your recent interactions:
View only user prompts:
Enable logging mid-session:
Impact on Codebase Maintenance
The logging feature is designed with maintainability in mind:
This design makes the feature easy to maintain and extend in the future, with minimal impact on the core Q CLI functionality.
Reference-level explanation
Architecture
The Q CLI Logging Feature consists of four main components:
Component Interfaces
LogManager
LogEntry
LogCommandHandler
Data Storage
Log File Format
Logs are stored in JSONL (JSON Lines) format, with each line containing a single JSON object representing a log entry:
File Structure
The logging feature uses the following file structure:
Where
[log_directory]
is the platform-specific log directory.Implementation Details
Enabling Logging
When the
--enable-logging
flag is provided at startup or the/log enable
command is used during a session:LogManager
initializes with the new session ID and creates the log directory if it doesn't existq_session_YYYY-MM-DD_HH-MM-SS_[session_id].log
enabled
flag inLogManager
is set totrue
Similarly, when the
/log disable
command is used:enabled
flag inLogManager
is set tofalse
Logging Interactions
For each user interaction:
LogManager.log_interaction
method is called with the prompt, response, response time, context files, and a flag indicating whether it's a user prompt or system commandLogEntry
is created with a unique ID and the current timestampLogEntry.generate_summary
methodLog Truncation
When a log file exceeds 512MB:
LogManager.check_and_truncate_log_file
method is calledCommand Handling
When a
/log
command is entered:LogCommandHandler
is called:handle_show_command
for/log show
(with optional--all
,--desc
, or--only-user-prompts
flags)handle_tail_command
for/log --tail N
handle_head_command
for/log --head N
handle_delete_command
for/log delete
handle_enable_command
for/log enable
handle_disable_command
for/log disable
LogManager
, applying filters likeonly_user_prompts
if specifiedError Handling
The feature implements comprehensive error handling:
Log Directory Creation Failure
Log File Write Failure
Log File Read Failure
Log File Size Limit Exceeded
Invalid Command Arguments
Integration with Existing Codebase
The logging feature integrates with the existing Q CLI codebase by:
--enable-logging
flag to the command-line argument parser/log
command handler with the existing command processing system, including the newenable
,disable
, and--only-user-prompts
optionsThis integration is designed to be minimally invasive, with the logging functionality contained in its own module and only interacting with the main codebase through well-defined interfaces.
Drawbacks
There are several potential drawbacks to implementing this feature:
Increased Complexity: Adding logging functionality increases the complexity of the codebase, which could make maintenance more challenging.
Disk Space Usage: Logs can consume significant disk space over time, especially for power users. While we implement truncation at 512MB, this could still be an issue for users with limited storage.
Performance Impact: Writing logs to disk for each interaction could potentially impact performance, especially on systems with slow storage.
Privacy Concerns: Storing user interactions could raise privacy concerns, as sensitive information might be inadvertently logged. This is mitigated by making logging opt-in and storing logs locally only.
Maintenance Burden: The feature will require ongoing maintenance to ensure compatibility with future changes to the Q CLI tool.
Cross-Platform Complexity: Implementing proper log storage across different operating systems adds complexity and potential for platform-specific bugs.
Rationale and alternatives
Why this design?
This design was chosen because it:
Balances Simplicity and Functionality: The design provides comprehensive logging capabilities while maintaining a simple user interface.
Follows Platform Conventions: By storing logs in standard system locations, the feature integrates well with existing system tools and user expectations.
Minimizes Resource Usage: The JSONL format and line-based truncation approach minimize disk space usage and processing overhead.
Preserves User Privacy: The opt-in approach and local-only storage respect user privacy while still providing the benefits of logging.
Supports Future Extensions: The modular design allows for easy addition of new features like log compression, advanced filtering, and analytics.
Alternatives Considered
1. Persistent Logging (Always Enabled)
Instead of opt-in logging, we could enable logging by default for all sessions.
Rationale for not choosing: This would raise privacy concerns and consume disk space unnecessarily for users who don't need logging.
2. Database Storage
Instead of flat files, we could store logs in a lightweight database like SQLite.
Rationale for not choosing: This would add a dependency on a database library and increase complexity without significant benefits for the current use cases.
3. Remote Logging
We could offer an option to sync logs to a remote storage service.
Rationale for not choosing: This would raise significant privacy concerns and add unnecessary complexity for what is primarily a local tool.
4. Log Rotation Instead of Truncation
Instead of truncating large log files, we could implement log rotation with multiple files.
Rationale for not choosing: While this would preserve more historical data, it would consume more disk space and add complexity to the log management system.
Impact of Not Doing This
If we don't implement this feature:
Unresolved questions
Command Syntax: Should we use
/log
as the command prefix, or would another syntax be more consistent with existing commands?Log Retention Policy: Should we implement an automatic cleanup policy for older logs, or leave this to the user?
Response Summary Generation: What's the optimal approach for generating compact summaries of responses? Should we use a fixed character limit, extract the first few sentences, or use a more sophisticated approach?
Performance Impact: How significant is the performance impact of logging, especially on systems with slow storage? Do we need to implement buffering or asynchronous writes?
Cross-Session Queries: Should we support querying logs across multiple sessions, or keep the focus on the current session only?
Security Considerations: Should we implement any additional security measures for log files, such as encryption or redaction of sensitive information?
Future possibilities
There are several natural extensions to this feature that could be implemented in the future:
Log Compression: Automatically compress older log files to save disk space.
Advanced Filtering: Add support for filtering logs by date range, keywords, or other criteria.
Log Export: Allow users to export logs to different formats (CSV, PDF, etc.) for sharing or analysis.
Log Analytics: Implement basic analytics to show usage patterns, common queries, etc.
Cross-Session Queries: Add support for querying logs across multiple sessions.
Log Synchronization: Allow users to synchronize logs across multiple devices (with appropriate privacy controls).
Selective Logging: Allow users to mark certain interactions as private (not to be logged).
Log Annotations: Allow users to add notes or tags to log entries for easier reference.
Integration with Other Tools: Provide APIs or hooks for other tools to access and analyze logs.
Contextual Recall: Use logged interactions to improve the context and relevance of future responses.
These possibilities demonstrate the potential for the logging feature to evolve beyond simple record-keeping into a powerful tool for productivity, learning, and collaboration.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.