Skip to content

Commit 7cfd42e

Browse files
Fix issue #7147: Document Logging
1 parent 0519e9e commit 7cfd42e

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

docs/modules/usage/logging.md

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Logging in OpenHands
2+
3+
OpenHands provides a robust and configurable logging system that helps developers track application behavior, debug issues, and monitor LLM interactions. This guide covers the key aspects of OpenHands' logging functionality.
4+
5+
## Environment Variables
6+
7+
OpenHands' logging behavior can be customized through several environment variables:
8+
9+
### Core Logging Controls
10+
11+
- `LOG_LEVEL`: Sets the logging level (default: 'INFO')
12+
- Valid values: DEBUG, INFO, WARNING, ERROR, CRITICAL
13+
- Can also be enabled by setting `DEBUG=true`
14+
15+
- `LOG_TO_FILE`: Enables file-based logging (default: false)
16+
- When enabled, logs are written to `logs/openhands_YYYY-MM-DD.log`
17+
- Automatically enabled when in DEBUG mode
18+
19+
### JSON Logging
20+
21+
- `LOG_JSON`: Enables structured JSON logging (default: false)
22+
- When enabled, logs are output in JSON format for better machine readability
23+
- Useful for log aggregation and analysis systems
24+
25+
- `LOG_JSON_LEVEL_KEY`: Customizes the key name for log levels in JSON output (default: 'level')
26+
- Example: `{"timestamp": "2025-04-07 10:00:00", "level": "INFO", "message": "..."}`
27+
28+
### Debug Options
29+
30+
- `DEBUG`: Enables debug mode (default: false)
31+
- Sets LOG_LEVEL to DEBUG
32+
- Enables stack traces for errors
33+
- Automatically enables file logging
34+
35+
- `DEBUG_LLM`: Enables detailed LLM interaction logging (default: false)
36+
- **WARNING**: May expose sensitive information like API keys
37+
- Requires explicit confirmation when enabled
38+
- Should never be enabled in production
39+
40+
- `DEBUG_RUNTIME`: Enables runtime environment debugging (default: false)
41+
- Streams Docker container logs
42+
- Useful for debugging sandbox environments
43+
44+
### Event Logging
45+
46+
- `LOG_ALL_EVENTS`: Enables verbose event logging (default: false)
47+
- Logs all events with additional context
48+
- Useful for debugging agent behavior
49+
50+
## Log File Structure
51+
52+
When file logging is enabled (`LOG_TO_FILE=true`), logs are organized as follows:
53+
54+
```
55+
logs/
56+
├── openhands_YYYY-MM-DD.log # Main application log
57+
└── llm/ # LLM interaction logs
58+
└── [session]/ # Session-specific logs
59+
├── prompt_001.log # LLM prompts
60+
└── response_001.log # LLM responses
61+
```
62+
63+
- Session directories are named:
64+
- In debug mode: `YY-MM-DD_HH-MM`
65+
- Otherwise: `default`
66+
67+
## Security Features
68+
69+
### SensitiveDataFilter
70+
71+
OpenHands includes a sophisticated filter to prevent sensitive data from appearing in logs:
72+
73+
1. **Environment Variables**
74+
- Automatically masks values from environment variables containing:
75+
- SECRET
76+
- _KEY
77+
- _CODE
78+
- _TOKEN
79+
80+
2. **Known Sensitive Patterns**
81+
- Masks common sensitive values like:
82+
- API keys
83+
- Access tokens
84+
- Authentication credentials
85+
- AWS credentials
86+
- GitHub tokens
87+
88+
Example:
89+
```python
90+
# Original log message
91+
"API key: sk-1234567890, GitHub token: ghp_abcdef"
92+
93+
# Filtered log message
94+
"API key: ******, GitHub token: ******"
95+
```
96+
97+
## Log Colors and Formatting
98+
99+
When logging to the console, OpenHands uses color-coded output for better readability:
100+
101+
- ACTION: Green
102+
- USER_ACTION: Light Red
103+
- OBSERVATION: Yellow
104+
- USER_OBSERVATION: Light Green
105+
- DETAIL: Cyan
106+
- ERROR: Red
107+
- PLAN: Light Magenta
108+
109+
## Best Practices
110+
111+
1. **Production Settings**
112+
- Keep `DEBUG` and `DEBUG_LLM` disabled
113+
- Consider enabling `LOG_JSON` for structured logging
114+
- Use appropriate `LOG_LEVEL` (INFO or WARNING recommended)
115+
116+
2. **Development Settings**
117+
- Enable `DEBUG` for detailed logging
118+
- Use `LOG_TO_FILE` to persist logs
119+
- Enable `LOG_ALL_EVENTS` when debugging agent behavior
120+
121+
3. **Security Considerations**
122+
- Never enable `DEBUG_LLM` in production
123+
- Regularly review logs for accidentally exposed sensitive data
124+
- Use `SensitiveDataFilter` for custom logging implementations
125+
126+
4. **Log Management**
127+
- Implement log rotation for production deployments
128+
- Monitor log directory size when `LOG_TO_FILE` is enabled
129+
- Consider forwarding JSON logs to a log aggregation service

docs/sidebars.ts

+5
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ const sidebars: SidebarsConfig = {
211211
label: 'Custom Sandbox',
212212
id: 'usage/how-to/custom-sandbox-guide',
213213
},
214+
{
215+
type: 'doc',
216+
label: 'Logging',
217+
id: 'usage/logging',
218+
},
214219
],
215220
},
216221
{

0 commit comments

Comments
 (0)