Skip to main content

test_quickwit/
test_quickwit.rs

1//! 测试 Quickwit 连接的独立脚本
2//!
3//! 用于验证 Quickwit 服务是否正常运行以及配置是否正确
4
5use log_full::quickwit::{QuickwitConfig, QuickwitClient, QuickwitLogEntry};
6use std::collections::HashMap;
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    println!("开始测试 Quickwit 连接...");
10    
11    // 创建 Quickwit 配置(不使用 token)
12    let config = QuickwitConfig::new(
13        "http://localhost:7280".to_string(),
14        "log_full".to_string(),
15    )
16    .with_timeout(10)
17    .with_batch_size(1);
18    
19    println!("配置: {:?}", config);
20    
21    // 创建客户端
22    let client = match QuickwitClient::new(config) {
23        Ok(client) => {
24            println!("✓ Quickwit 客户端创建成功");
25            client
26        },
27        Err(e) => {
28            println!("✗ Quickwit 客户端创建失败: {}", e);
29            return Err(e.into());
30        }
31    };
32    
33    // 创建测试日志条目
34    let log_entry = QuickwitLogEntry {
35        timestamp: std::time::SystemTime::now()
36            .duration_since(std::time::UNIX_EPOCH)
37            .unwrap()
38            .as_secs(),
39        level: "INFO".to_string(),
40        message: "测试日志条目 - Quickwit 连接测试".to_string(),
41        module: Some("test_quickwit".to_string()),
42        file: Some("test_quickwit.rs".to_string()),
43        line: Some(42),
44        process_id: Some(std::process::id()),
45        thread_id: Some("main".to_string()),
46        custom_fields: HashMap::new(),
47    };
48    
49    println!("准备发送测试日志条目...");
50    
51    // 发送日志
52    match client.send_log(&log_entry) {
53        Ok(()) => {
54            println!("✓ 日志发送成功!");
55            println!("请检查 Quickwit 索引 'log_full' 中是否有新的日志条目");
56        },
57        Err(e) => {
58            println!("✗ 日志发送失败: {}", e);
59            println!("可能的原因:");
60            println!("  1. Quickwit 服务未运行 (检查 http://localhost:7280)");
61            println!("  2. 索引 'log_full' 不存在");
62            println!("  3. 网络连接问题");
63            println!("  4. Quickwit 配置错误");
64            return Err(e.into());
65        }
66    }
67    
68    println!("测试完成!");
69    Ok(())
70}