Skip to main content

LogRotator

Struct LogRotator 

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

日志轮转器(内存模拟)。

维护一个当前日志缓冲与一组已轮转的历史日志。当当前缓冲满足轮转策略时, 将其移入历史列表并开启新的空缓冲。历史列表长度不超过 max_files, 超出时丢弃最旧的。

实际生产环境中,轮转器会将缓冲刷盘到文件(如 app.log -> app.log.1), 此处用内存 Vec<u8> 模拟以便测试。

Implementations§

Source§

impl LogRotator

Source

pub fn new(policy: RotationPolicy, max_files: usize) -> Self

创建轮转器

§参数
  • policy:轮转策略
  • max_files:保留的最大历史文件数(0 表示不保留)
Source

pub fn write(&self, data: &[u8])

写入一条日志(字节形式)。写入后自动检查是否需要轮转。

Source

pub fn rotate(&self)

手动触发轮转:将当前缓冲移入历史列表,开启新的空缓冲。

Source

pub fn current_size(&self) -> usize

获取当前缓冲的字节大小

Source

pub fn rotated_count(&self) -> usize

获取已轮转的历史日志数量

Source

pub fn total_rotations(&self) -> u64

获取总轮转次数(含因 max_files 限制被丢弃的)

Source

pub fn current_content(&self) -> Vec<u8>

获取当前缓冲内容的快照(拷贝)

Source

pub fn rotated_content(&self, index: usize) -> Option<Vec<u8>>

获取第 index 个历史日志的快照(0 = 最近一次轮转)

Source

pub fn current_age(&self) -> Duration

获取当前缓冲已存活的时长

Source

pub fn policy(&self) -> &RotationPolicy

获取轮转策略引用

Source

pub fn max_files(&self) -> usize

获取最大保留文件数

Source

pub fn clear(&self)

清空所有日志(当前缓冲 + 历史)

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.