FileProcessor

Struct FileProcessor 

Source
pub struct FileProcessor { /* private fields */ }
Expand description

文件日志处理器 - 实现LogProcessor trait

Implementations§

Source§

impl FileProcessor

Source

pub fn new(config: FileConfig) -> Self

创建新的文件处理器

Source

pub fn with_config(config: FileProcessorConfig) -> Self

使用配置创建文件处理器

Examples found in repository?
examples/config_validation_test.rs (line 59)
7fn main() {
8    println!("=== 配置冲突检测测试 ===\n");
9
10    // 测试1: TermConfig 颜色冲突
11    println!("1. 测试 TermConfig 颜色冲突:");
12    let invalid_term_config = TermConfig {
13        enable_color: false,  // 禁用颜色
14        color: Some(ColorConfig::default()),  // 但提供了颜色配置
15        ..Default::default()
16    };
17
18    match std::panic::catch_unwind(|| {
19        LoggerBuilder::new()
20            .with_level(LevelFilter::Debug)
21            .add_terminal_with_config(invalid_term_config)
22            .build()
23    }) {
24        Ok(_) => println!("   ❌ 错误: 应该检测到配置冲突但没有"),
25        Err(_) => println!("   ✅ 正确: 成功检测到颜色配置冲突"),
26    }
27
28    // 测试2: TermConfig 无效批量大小
29    println!("\n2. 测试 TermConfig 无效批量大小:");
30    let invalid_batch_config = TermConfig {
31        batch_size: 0,  // 无效的批量大小
32        ..Default::default()
33    };
34
35    match std::panic::catch_unwind(|| {
36        LoggerBuilder::new()
37            .with_level(LevelFilter::Debug)
38            .add_terminal_with_config(invalid_batch_config)
39            .build()
40    }) {
41        Ok(_) => println!("   ❌ 错误: 应该检测到批量大小错误但没有"),
42        Err(_) => println!("   ✅ 正确: 成功检测到批量大小错误"),
43    }
44
45    // 测试3: FileConfig 原始模式冲突
46    println!("\n3. 测试 FileConfig 原始模式冲突:");
47    let invalid_file_config = FileConfig {
48        is_raw: true,  // 原始模式
49        format: Some(FormatConfig::default()),  // 但提供了格式配置
50        ..Default::default()
51    };
52
53    let file_processor_config = FileProcessorConfig {
54        file_config: invalid_file_config,
55        ..Default::default()
56    };
57
58    match std::panic::catch_unwind(|| {
59        rat_logger::handler::file::FileProcessor::with_config(file_processor_config)
60    }) {
61        Ok(_) => println!("   ❌ 错误: 应该检测到原始模式冲突但没有"),
62        Err(_) => println!("   ✅ 正确: 成功检测到原始模式冲突"),
63    }
64
65    // 测试4: FileConfig 无效文件大小
66    println!("\n4. 测试 FileConfig 无效文件大小:");
67    let invalid_size_config = FileConfig {
68        max_file_size: 0,  // 无效的文件大小
69        ..Default::default()
70    };
71
72    let file_processor_config = FileProcessorConfig {
73        file_config: invalid_size_config,
74        ..Default::default()
75    };
76
77    match std::panic::catch_unwind(|| {
78        rat_logger::handler::file::FileProcessor::with_config(file_processor_config)
79    }) {
80        Ok(_) => println!("   ❌ 错误: 应该检测到文件大小错误但没有"),
81        Err(_) => println!("   ✅ 正确: 成功检测到文件大小错误"),
82    }
83
84    // 测试5: 正确配置应该工作
85    println!("\n5. 测试正确配置:");
86    let valid_term_config = TermConfig {
87        enable_color: true,
88        color: Some(ColorConfig::default()),
89        format: Some(FormatConfig::default()),
90        ..Default::default()
91    };
92
93    match std::panic::catch_unwind(|| {
94        LoggerBuilder::new()
95            .with_level(LevelFilter::Debug)
96            .add_terminal_with_config(valid_term_config)
97            .build()
98    }) {
99        Ok(_) => println!("   ✅ 正确: 有效配置正常工作"),
100        Err(_) => println!("   ❌ 错误: 有效配置被错误拒绝"),
101    }
102
103    println!("\n=== 配置冲突检测测试完成 ===");
104}
Source

pub fn with_batch_size(self, batch_size: usize) -> Self

设置批量大小

Source

pub fn with_flush_interval(self, flush_interval_ms: u64) -> Self

设置刷新间隔

Source§

impl FileProcessor

Source

pub fn with_formatter<F>(self, formatter: F) -> Self
where F: Fn(&mut dyn Write, &Record) -> Result<()> + Send + Sync + 'static,

设置自定义格式化函数

Source

pub fn with_format(self, format_config: FormatConfig) -> Self

使用格式配置

Trait Implementations§

Source§

impl Drop for FileProcessor

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl LogProcessor for FileProcessor

Source§

fn name(&self) -> &'static str

处理器名称
Source§

fn process(&mut self, data: &[u8]) -> Result<(), String>

处理单个日志数据
Source§

fn process_batch(&mut self, batch: &[Vec<u8>]) -> Result<(), String>

批量处理日志数据 - 保持原有优化逻辑
Source§

fn handle_rotate(&mut self) -> Result<(), String>

处理文件轮转命令 - 默认忽略(只有文件处理器需要处理)
Source§

fn handle_compress(&mut self, path: &Path) -> Result<(), String>

处理文件压缩命令 - 默认忽略(只有文件处理器需要处理)
Source§

fn flush(&mut self) -> Result<(), String>

刷新操作
Source§

fn cleanup(&mut self) -> Result<(), String>

清理资源

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.