Skip to content

Conversation

@mkmeral
Copy link

@mkmeral mkmeral commented May 25, 2025

Description

This PR adds logging system to the Strands CLI. The implementation includes:

  1. Command-line arguments for configuring logging:

    • --log-level: Set logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
    • --log-file: Specify output file path for logs
  2. A new utility module logging_utils.py with functions for:

    • Configuring logging with file output
    • Getting available log levels
    • Checking logging status
    • Setting log levels for specific modules
  3. Integration into the main CLI entry point to initialize logging based on user preferences

  4. Comprehensive unit tests in tests/utils/test_logging_utils.py covering:

    • Log level validation
    • File handler creation
    • Directory creation
    • Exception handling
    • Logger state management
    • All edge cases and error conditions

This enhancement will help users debug issues, track agent behavior, and maintain operation logs for production deployments.

Related Issues

[No specific issue reference provided - please add if applicable]

Documentation PR

[Link to associated documentation PR when created]

Type of Change

  • New feature

Testing

The changes have been thoroughly tested with:

  • hatch fmt --linter - All linting checks pass
  • hatch fmt --formatter - Code formatting is consistent
  • hatch test --all - Full test suite passes

Additionally, manual testing with various logging configurations confirms the functionality:

# Test with default logging (INFO level)
strands --log-file=strands.log

# Test with debug level
strands --log-level=DEBUG --log-file=debug.log

# Verify log file is created with appropriate content
cat debug.log

See example

2025-05-25 11:06:53,210 - root - INFO - Logging configured with level: DEBUG
2025-05-25 11:06:53,210 - root - INFO - Log file: /Users/murmeral/.strands/logs/2025-05-25_11-06-52-DEBUG.log
2025-05-25 11:06:53,210 - root - INFO - Strands CLI started with log level DEBUG
2025-05-25 11:06:53,210 - root - INFO - Log file: /Users/murmeral/.strands/logs/2025-05-25_11-06-52-DEBUG.log
2025-05-25 11:06:53,210 - strands.models.bedrock - DEBUG - config=<{'model_id': 'us.anthropic.claude-3-7-sonnet-20250219-v1:0', 'max_tokens': 64000, 'additional_request_fields': {'thinking': {'type': 'enabled', 'budget_tokens': 2048}}}> | initializing
2025-05-25 11:06:53,211 - botocore.hooks - DEBUG - Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
...

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice

@mkmeral mkmeral requested a review from a team as a code owner May 25, 2025 10:53
@awsarron
Copy link
Member

Thanks for your contribution @mkmeral! I added a few comments to follow up on.

Murat Kaan Meral added 4 commits May 31, 2025 15:02
✅ Fixed Issues:
- Updated documentation from 'Strands CLI' to 'Strands Agent Builder'
- Removed shebang from utility module
- Enhanced logging behavior: stderr support when only --log-level provided
- Fixed logger usage: using module logger for startup messages
- Optimized logger configuration: simplified to parent loggers only
- Removed unused functions: get_logging_status() and set_log_level_for_module()

🚀 Improvements:
- Better UX: --log-level DEBUG works without --log-file (logs to stderr)
- Fallback handling: auto-fallback to stderr if file creation fails
- Enhanced CLI args: improved help text and argument behavior
- Comprehensive test suite: 9 unit tests covering all scenarios

📝 New Logging Behavior:
- No args = no logging (clean output)
- --log-level only = logs to stderr
- --log-file only = logs to file (INFO level)
- Both args = logs to file with specified level

All tests pass ✅
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants