Skip to main content

Crate inklog

Crate inklog 

Source
Expand description

§inklog - 企业级 Rust 日志基础设施

inklog 是一个高性能、可扩展的日志库,专为生产环境设计。

§功能特性

  • 多输出目标: 支持 Console、File、Database 三种输出通道
  • 日志轮转: 支持按大小和按时间轮转
  • 压缩与加密: 支持 Zstandard 压缩和 AES-256-GCM 加密
  • 批量写入: 数据库批量写入,可配置批次大小和刷新间隔
  • 降级机制: DB → File → Console 三级降级
  • 健康监控: HTTP 端点暴露健康状态和 Prometheus 指标

§快速开始

§基础用法

use inklog::{LoggerManager, InklogConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 使用默认配置初始化
    let _logger = LoggerManager::new().await?;
     
    // 使用 tracing 宏记录日志
    tracing::info!("Hello, inklog!");
     
    Ok(())
}

§使用 Builder 模式配置

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    use inklog::LoggerManager;

    let _logger = LoggerManager::builder()
        .level("debug")
        .console(true)
        .file("logs/app.log")
        .enable_http_server(true)
        .http_port(9090)
        .build()
        .await?;

    Ok(())
}

§从配置文件加载

use inklog::LoggerManager;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 从指定文件加载
    let _logger = LoggerManager::from_file("config.toml").await?;
     
    // 或自动搜索配置文件
    // let _logger = LoggerManager::load().await?;
     
    Ok(())
}

§使用依赖注入模式

use std::sync::Arc;
use inklog::{LoggerManager, LoggerDependencies, InklogContainer};
use inklog::infrastructure::{OxCacheAdapter, InklogConfigAdapter};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 方式 1: 使用依赖注入容器
    let container = InklogContainer::new()?;
    let logger = container.create_logger().await?;
     
    // 方式 2: 使用 Builder 模式注入依赖
    let logger = LoggerManager::builder()
        .cache(Arc::new(OxCacheAdapter::new()?))
        .config(Arc::new(InklogConfigAdapter::new()?))
        .build().await?;
     
    // 方式 3: 使用 with_dependencies
    let deps = LoggerDependencies {
        cache: Some(Arc::new(OxCacheAdapter::new()?)),
        config: Some(Arc::new(InklogConfigAdapter::new()?)),
        ..Default::default()
    };
    let logger = LoggerManager::with_dependencies(deps).await?;
     
    Ok(())
}

§配置文件示例 (TOML)

[global]
level = "info"

[console_sink]
enabled = true

[file_sink]
enabled = true
path = "logs/app.log"
max_size = "100MB"
rotation = "daily"

[http_server]
enabled = true
host = "127.0.0.1"
port = 8080

Re-exports§

pub use domain::config;
pub use domain::types::log_record;
pub use support::io::sink;
pub use support::processing::template;
pub use support::processing::masking;
pub use domain::config::ChannelStrategy;
pub use domain::config::ConsoleSinkConfig;
pub use domain::config::DatabaseDriver;
pub use domain::config::DatabaseSinkConfig;
pub use domain::config::FileSinkConfig;
pub use domain::config::GlobalConfig;
pub use domain::config::HttpAuthConfig;
pub use domain::config::HttpErrorMode;
pub use domain::config::HttpServerConfig;
pub use domain::config::InklogConfig;
pub use domain::config::ParquetConfig;
pub use domain::config::PartitionStrategy;
pub use domain::config::PerformanceConfig;
pub use domain::db_provider::LogDbProvider;
pub use domain::types::error::InklogError;
pub use domain::types::log_record::LogRecord;
pub use integrations::dbnexus_adapter::DbNexusLogDbAdapter;
pub use integrations::kit::InklogModule;
pub use domain::core::InklogContainer;
pub use domain::core::InklogContainerBuilder;
pub use domain::core::LoggerBuilder;
pub use domain::core::LoggerDependencies;
pub use domain::core::LoggerManager;
pub use log_level::LogLevel;
pub use support::io::LogAdapter;
pub use support::io::LogLogger;
pub use support::observability::FallbackConfig;
pub use support::observability::FallbackState;
pub use support::observability::GaugeF64;
pub use support::observability::HealthStatus;
pub use support::observability::Metrics;
pub use support::observability::SinkHealthMonitor;
pub use support::observability::SinkStatus;
pub use support::processing::DataMasker;
pub use support::processing::LogTemplate;
pub use support::processing::ObjectPool;
pub use support::processing::ObjectPoolConfig;
pub use support::processing::get_log_record;
pub use support::processing::get_string_buffer;
pub use support::processing::put_log_record;
pub use support::processing::put_string_buffer;
pub use validation::EscapeMode;
pub use validation::LogSanitizer;
pub use validation::PathValidator;
pub use validation::PathValidatorConfig;
pub use validation::SanitizerConfig;
pub use validation::ValidationResult;

Modules§

domain
Domain module - core business logic layer.
i18n
ICU4X-backed internationalization formatting for log operations.
integrations
Integrations module - external service integrations.
log_level
日志级别枚举定义
support
Support layer module - functional support layer.
validation
Input validation module.