logging_example/
logging_example.rs

1use tushare_api::{TushareClient, LogLevel, LogConfig, Api, TushareRequest};
2use std::time::Duration;
3use std::collections::HashMap;
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // 注意:在实际使用中,您需要初始化一个日志记录器(如 env_logger)
8    // env_logger::init();
9
10    println!("=== Tushare API 日志功能演示 ===\n");
11
12    // 示例 1: 使用默认日志配置
13    println!("1. 使用默认日志配置:");
14    let client1 = TushareClient::builder()
15        .with_token("your_token_here")
16        .build()?;
17
18    // 示例 2: 自定义日志级别
19    println!("\n2. 自定义日志级别为 Debug:");
20    let client2 = TushareClient::builder()
21        .with_token("your_token_here")
22        .with_log_level(LogLevel::Debug)
23        .build()?;
24
25    // 示例 3: 详细的日志配置
26    println!("\n3. 详细的日志配置:");
27    let client3 = TushareClient::builder()
28        .with_token("your_token_here")
29        .with_log_level(LogLevel::Trace)
30        .log_requests(true)
31        .log_responses(true)
32        .log_sensitive_data(false) // 生产环境建议设为 false
33        .log_performance(true)
34        .with_connect_timeout(Duration::from_secs(5))
35        .with_timeout(Duration::from_secs(30))
36        .build()?;
37
38    // 示例 4: 使用自定义 LogConfig
39    println!("\n4. 使用自定义 LogConfig:");
40    let log_config = LogConfig {
41        level: LogLevel::Info,
42        log_requests: true,
43        log_responses: false,
44        log_responses_err: true,
45        log_sensitive_data: false,
46        log_performance: true,
47    };
48    
49    let client4 = TushareClient::builder()
50        .with_token("your_token_here")
51        .with_log_config(log_config)
52        .build()?;
53
54    // 示例 5: 关闭日志
55    println!("\n5. 关闭日志:");
56    let client5 = TushareClient::builder()
57        .with_token("your_token_here")
58        .with_log_level(LogLevel::Off)
59        .build()?;
60
61    // 演示 API 调用(需要有效的 token)
62    if std::env::var("TUSHARE_TOKEN").is_ok() {
63        println!("\n=== 实际 API 调用演示 ===");
64        
65        let client = TushareClient::builder()
66            .with_token(&std::env::var("TUSHARE_TOKEN")?)
67            .with_log_level(LogLevel::Info)
68            .log_performance(true)
69            .build()?;
70
71        let mut params = HashMap::new();
72        params.insert("list_status".to_string(), "L".to_string());
73        
74        let req = TushareRequest {
75            api_name: Api::StockBasic,
76            params,
77            fields: vec!["ts_code".to_string(), "name".to_string()],
78        };
79
80        match client.call_api(&req).await {
81            Ok(response) => {
82                if let Some(data) = response.data {
83                    println!("✅ API 调用成功,返回 {} 条记录", data.items.len());
84                }
85            }
86            Err(e) => {
87                println!("❌ API 调用失败: {}", e);
88            }
89        }
90    } else {
91        println!("\n💡 提示: 设置 TUSHARE_TOKEN 环境变量以查看实际的 API 调用日志");
92    }
93
94    println!("\n=== 日志级别说明 ===");
95    println!("• Off    - 关闭所有日志");
96    println!("• Error  - 只记录错误信息");
97    println!("• Warn   - 记录错误和警告");
98    println!("• Info   - 记录基本信息(推荐)");
99    println!("• Debug  - 记录详细调试信息");
100    println!("• Trace  - 记录所有信息包括原始数据");
101
102    println!("\n=== 日志配置选项说明 ===");
103    println!("• log_requests      - 是否记录请求参数");
104    println!("• log_responses     - 是否记录响应内容(可能很大)");
105    println!("• log_sensitive_data - 是否记录敏感数据(如 token)");
106    println!("• log_performance   - 是否记录性能指标(耗时等)");
107
108    Ok(())
109}