33## ⚡ TL;DR - 3 Step Process
44
551 . ** Import the logger** : ` from codegen.shared.logging.get_logger import get_logger `
6- 2 . ** Add ` extra={} ` to your log calls** : ` logger.info("message", extra={"key": "value"}) `
7- 3 . ** Enable telemetry** : ` codegen config telemetry enable `
6+ 1 . ** Add ` extra={} ` to your log calls** : ` logger.info("message", extra={"key": "value"}) `
7+ 1 . ** Enable telemetry** : ` codegen config telemetry enable `
88
99** That's it!** Your logs automatically go to Grafana Cloud when telemetry is enabled.
1010
@@ -22,11 +22,14 @@ from codegen.shared.logging.get_logger import get_logger
2222logger = get_logger(__name__ )
2323
2424# Find any existing console.print() or error handling and add:
25- logger.info(" Operation completed" , extra = {
26- " operation" : " command_name" ,
27- " org_id" : org_id, # if available
28- " success" : True
29- })
25+ logger.info(
26+ " Operation completed" ,
27+ extra = {
28+ " operation" : " command_name" ,
29+ " org_id" : org_id, # if available
30+ " success" : True ,
31+ },
32+ )
3033```
3134
3235### 2. Test the Integration Right Now
@@ -48,69 +51,56 @@ codegen config telemetry status
4851## 📝 Copy-Paste Patterns
4952
5053### Pattern 1: Operation Start/End
54+
5155``` python
5256logger = get_logger(__name__ )
5357
5458# At start of function
55- logger.info(" Operation started" , extra = {
56- " operation" : " command.subcommand" ,
57- " user_input" : relevant_input
58- })
59+ logger.info(" Operation started" , extra = {" operation" : " command.subcommand" , " user_input" : relevant_input})
5960
6061# At end of function
61- logger.info(" Operation completed" , extra = {
62- " operation" : " command.subcommand" ,
63- " success" : True
64- })
62+ logger.info(" Operation completed" , extra = {" operation" : " command.subcommand" , " success" : True })
6563```
6664
6765### Pattern 2: Error Handling
66+
6867``` python
6968try :
7069 # your existing code
7170 pass
7271except SomeSpecificError as e:
73- logger.error(" Specific error occurred" , extra = {
74- " operation" : " command.subcommand" ,
75- " error_type" : " specific_error" ,
76- " error_details" : str (e)
77- }, exc_info = True )
72+ logger.error(" Specific error occurred" , extra = {" operation" : " command.subcommand" , " error_type" : " specific_error" , " error_details" : str (e)}, exc_info = True )
7873 # your existing error handling
7974```
8075
8176### Pattern 3: API Calls
77+
8278``` python
8379# Before API call
84- logger.info(" Making API request" , extra = {
85- " operation" : " api.request" ,
86- " endpoint" : " agent/run" ,
87- " org_id" : org_id
88- })
80+ logger.info(" Making API request" , extra = {" operation" : " api.request" , " endpoint" : " agent/run" , " org_id" : org_id})
8981
9082# After successful API call
91- logger.info(" API request successful" , extra = {
92- " operation" : " api.request" ,
93- " endpoint" : " agent/run" ,
94- " response_id" : response.get(" id" ),
95- " status_code" : response.status_code
96- })
83+ logger.info(" API request successful" , extra = {" operation" : " api.request" , " endpoint" : " agent/run" , " response_id" : response.get(" id" ), " status_code" : response.status_code})
9784```
9885
9986## 🎯 What to Log (Priority Order)
10087
10188### 🔥 High Priority (Add These First)
89+
10290- ** Operation start/end** : When commands begin/complete
10391- ** API calls** : Requests to your backend
10492- ** Authentication events** : Login/logout/token issues
10593- ** Errors** : Any exception or failure
10694- ** User actions** : Commands run, options selected
10795
10896### ⭐ Medium Priority
97+
10998- ** Performance** : Duration of operations
11099- ** State changes** : Status updates, configuration changes
111100- ** External tools** : Claude CLI detection, git operations
112101
113102### 💡 Low Priority (Nice to Have)
103+
114104- ** Debug info** : Internal state, validation steps
115105- ** User behavior** : Which features are used most
116106
@@ -163,15 +153,19 @@ def create(prompt: str, org_id: int | None = None, ...):
163153
164154logger = get_logger(__name__ )
165155
156+
166157def _run_claude_interactive (resolved_org_id : int , no_mcp : bool | None ) -> None :
167158 session_id = generate_session_id()
168159
169160 # ADD: Log session start
170- logger.info(" Claude session started" , extra = {
171- " operation" : " claude.session_start" ,
172- " session_id" : session_id[:8 ], # Short version for privacy
173- " org_id" : resolved_org_id
174- })
161+ logger.info(
162+ " Claude session started" ,
163+ extra = {
164+ " operation" : " claude.session_start" ,
165+ " session_id" : session_id[:8 ], # Short version for privacy
166+ " org_id" : resolved_org_id,
167+ },
168+ )
175169
176170 # Your existing code...
177171
@@ -180,30 +174,23 @@ def _run_claude_interactive(resolved_org_id: int, no_mcp: bool | None) -> None:
180174 returncode = process.wait()
181175
182176 # ADD: Log session end
183- logger.info(" Claude session completed" , extra = {
184- " operation" : " claude.session_complete" ,
185- " session_id" : session_id[:8 ],
186- " exit_code" : returncode,
187- " status" : " COMPLETE" if returncode == 0 else " ERROR"
188- })
177+ logger.info(
178+ " Claude session completed" , extra = {" operation" : " claude.session_complete" , " session_id" : session_id[:8 ], " exit_code" : returncode, " status" : " COMPLETE" if returncode == 0 else " ERROR" }
179+ )
189180
190181 except Exception as e:
191182 # ADD: Log session error
192- logger.error(" Claude session failed" , extra = {
193- " operation" : " claude.session_error" ,
194- " session_id" : session_id[:8 ],
195- " error" : str (e)
196- })
183+ logger.error(" Claude session failed" , extra = {" operation" : " claude.session_error" , " session_id" : session_id[:8 ], " error" : str (e)})
197184```
198185
199186## 🧪 Verification
200187
201188After making changes:
202189
2031901 . ** Run the command** : Execute your enhanced CLI command
204- 2 . ** Check telemetry status** : ` codegen config telemetry status `
205- 3 . ** Look for logs in Grafana Cloud** : Search for your operation names
206- 4 . ** Test with telemetry disabled** : ` codegen config telemetry disable ` - should still work normally
191+ 1 . ** Check telemetry status** : ` codegen config telemetry status `
192+ 1 . ** Look for logs in Grafana Cloud** : Search for your operation names
193+ 1 . ** Test with telemetry disabled** : ` codegen config telemetry disable ` - should still work normally
207194
208195## 🚀 Progressive Enhancement
209196
0 commit comments