MCPcopy Create free account
hub / github.com/ClickHouse/ai-sdk-cpp / demonstrate_logging

Function demonstrate_logging

examples/error_handling.cpp:271–306  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

269}
270
271void demonstrate_logging() {
272 std::cout << "7. Error Logging Best Practices\n";
273 std::cout << "===============================\n\n";
274
275 std::cout << "Best practices for error logging in AI applications:\n\n";
276
277 std::cout << "1. Log error details but not sensitive data:\n";
278 auto client = ai::openai::create_client();
279 ai::GenerateOptions options;
280 options.model = "";
281 options.prompt = "Secret information";
282 auto result = client.generate_text(options);
283 if (!result) {
284 // Good: Log error type and code
285 std::cout << " ERROR: API call failed - " << result.error_message()
286 << "\n";
287 // Bad: Don't log API keys, user data, etc.
288 }
289
290 std::cout << "\n2. Include context information:\n";
291 std::cout << " ERROR: Text generation failed\n";
292 std::cout << " Context: model=gpt-4o, prompt_length=156, user_id=12345\n";
293 std::cout << " Details: " << result.error_message() << "\n";
294
295 std::cout << "\n3. Use structured logging:\n";
296 std::cout << " "
297 "{\"level\":\"error\",\"component\":\"ai-sdk\",\"operation\":"
298 "\"generate_text\",";
299 std::cout << "\"model\":\"gpt-4o\",\"error\":\"invalid_model\"}\n";
300
301 std::cout << "\n4. Log performance metrics:\n";
302 std::cout << " INFO: Text generation completed in 1250ms, 45 tokens used\n";
303
304 std::cout << "\n5. Don't log every retry, but log final failures:\n";
305 std::cout << " ERROR: Text generation failed after 3 retries\n\n";
306}
307
308int main() {
309 std::cout << "AI SDK C++ - Error Handling Examples\n";

Callers 1

mainFunction · 0.85

Calls 3

create_clientFunction · 0.50
generate_textMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected