baichun-framework-logger 0.1.0

Logger module for Baichun-Rust framework
Documentation
# baichun-framework-logger

[![Crates.io](https://img.shields.io/crates/v/baichun-framework-logger.svg)](https://crates.io/crates/baichun-framework-logger)
[![Documentation](https://docs.rs/baichun-framework-logger/badge.svg)](https://docs.rs/baichun-framework-logger)
[![License](https://img.shields.io/crates/l/baichun-framework-logger.svg)](LICENSE)

`baichun-framework-logger` 是一个功能强大的日志模块,基于 tracing 构建,提供了丰富的日志记录功能。

## 特性

- 多种日志格式支持
  - 文本格式(可自定义字段)
  - JSON 格式(支持结构化日志)
- 灵活的日志滚动策略
  - 按时间滚动(小时/天/周/月)
  - 按大小滚动
  - 混合滚动策略
- 日志文件管理
  - 自动创建日志目录
  - 文件自动清理
  - 多级别日志分离
- 异步写入支持
- 控制台输出配置
  - 彩色日志支持
  - 可配置输出级别

## 安装

将以下内容添加到你的 `Cargo.toml` 文件中:

```toml
[dependencies]
baichun-framework-logger = "0.1"
```

## 快速开始

```rust
use baichun_framework_logger::{
    LoggerConfig, LogLevel, LogFormat, TextFormatConfig,
    TimePeriod, TimeRollingConfig, RollingPolicy,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建日志配置
    let config = LoggerConfig {
        level: LogLevel::Info,
        dir: "logs".into(),
        filename: "app.log".into(),
        rolling_policy: RollingPolicy::Time(TimeRollingConfig {
            period: TimePeriod::Daily,
            keep_days: 7,
        }),
        format: LogFormat::Text(TextFormatConfig {
            time_format: "%Y-%m-%d %H:%M:%S".to_string(),
            show_level: true,
            show_target: true,
            show_thread_id: true,
            show_file: true,
            show_line: true,
        }),
        async_write: true,
        split_by_level: false,
        console: None,
    };

    // 初始化日志系统
    baichun_framework_logger::init(config)?;

    // 使用 tracing 宏记录日志
    tracing::info!("应用程序启动");
    tracing::error!("发生错误:{}", "连接失败");

    Ok(())
}
```

## 高级用法

### JSON 格式日志

```rust
use baichun_framework_logger::{LoggerConfig, LogFormat, JsonFormatConfig};

let config = LoggerConfig {
    format: LogFormat::Json(JsonFormatConfig {
        time_format: "%Y-%m-%d %H:%M:%S".to_string(),
        pretty: true,
        include_caller: true,
        include_thread: true,
    }),
    ..Default::default()
};
```

### 按级别分离日志文件

```rust
let config = LoggerConfig {
    split_by_level: true,
    ..Default::default()
};
```

### 混合滚动策略

```rust
use baichun_framework_logger::{
    RollingPolicy, TimeRollingConfig, SizeRollingConfig,
    TimePeriod,
};

let config = LoggerConfig {
    rolling_policy: RollingPolicy::Compound {
        time: TimeRollingConfig {
            period: TimePeriod::Daily,
            keep_days: 7,
        },
        size: SizeRollingConfig {
            max_size: 100 * 1024 * 1024, // 100MB
            max_files: 5,
        },
    },
    ..Default::default()
};
```

## 许可证

本项目采用 MIT 许可证。详见 [LICENSE](LICENSE) 文件。