-
Notifications
You must be signed in to change notification settings - Fork 211
style guide #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+68
−5
Merged
style guide #49
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Style Guide | ||
|
||
## Overview | ||
|
||
The Strands Agents style guide aims to establish consistent formatting, naming conventions, and structure across all code in the repository. We strive to make our code clean, readable, and maintainable. | ||
|
||
Where possible, we will codify these style guidelines into our linting rules and pre-commit hooks to automate enforcement and reduce the manual review burden. | ||
|
||
## Log Formatting | ||
|
||
The format for Strands Agents logs is as follows: | ||
|
||
```python | ||
logger.debug("field1=<%s>, field2=<%s>, ... | human readable message", field1, field2, ...) | ||
``` | ||
|
||
### Guidelines | ||
|
||
1. **Context**: | ||
- Add context as `<FIELD>=<VALUE>` pairs at the beginning of the log | ||
- Many log services (CloudWatch, Splunk, etc.) look for these patterns to extract fields for searching | ||
- Use `,`'s to separate pairs | ||
- Enclose values in `<>` for readability | ||
- This is particularly helpful in displaying empty values (`field=` vs `field=<>`) | ||
- Use `%s` for string interpolation as recommended by Python logging | ||
- This is an optimization to skip string interpolation when the log level is not enabled | ||
|
||
1. **Messages**: | ||
- Add human readable messages at the end of the log | ||
- Use lowercase for consistency | ||
- Avoid punctuation (periods, exclamation points, etc.) to reduce clutter | ||
- Keep messages concise and focused on a single statement | ||
- If multiple statements are needed, separate them with the pipe character (`|`) | ||
- Example: `"processing request | starting validation"` | ||
|
||
### Examples | ||
|
||
#### Good | ||
|
||
```python | ||
logger.debug("user_id=<%s>, action=<%s> | user performed action", user_id, action) | ||
logger.info("request_id=<%s>, duration_ms=<%d> | request completed", request_id, duration) | ||
logger.warning("attempt=<%d>, max_attempts=<%d> | retry limit approaching", attempt, max_attempts) | ||
``` | ||
|
||
#### Poor | ||
|
||
```python | ||
# Avoid: No structured fields, direct variable interpolation in message | ||
logger.debug(f"User {user_id} performed action {action}") | ||
|
||
# Avoid: Inconsistent formatting, punctuation | ||
logger.info("Request completed in %d ms.", duration) | ||
|
||
# Avoid: No separation between fields and message | ||
logger.warning("Retry limit approaching! attempt=%d max_attempts=%d", attempt, max_attempts) | ||
``` | ||
|
||
By following these log formatting guidelines, we ensure that logs are both human-readable and machine-parseable, making debugging and monitoring more efficient. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.