LogrotateConfig

Struct LogrotateConfig 

Source
pub struct LogrotateConfig {
Show 48 fields pub enabled: bool, pub path: String, pub rotate: String, pub size: Option<String>, pub maxsize: Option<String>, pub minsize: Option<String>, pub rotate_count: u32, pub maxage: Option<u32>, pub start: u32, pub compress: bool, pub delaycompress: u32, pub compresscmd: Option<String>, pub uncompresscmd: Option<String>, pub compressext: String, pub compressoptions: Option<String>, pub create: bool, pub create_mode: String, pub create_owner: Option<String>, pub copy: bool, pub copytruncate: bool, pub postrotate: bool, pub postrotate_script: Option<String>, pub prerotate: bool, pub prerotate_script: Option<String>, pub firstaction: bool, pub firstaction_script: Option<String>, pub lastaction: bool, pub lastaction_script: Option<String>, pub missingok: bool, pub notifempty: bool, pub ifempty: bool, pub sharedscripts: bool, pub dateext: bool, pub dateformat: String, pub olddir: Option<String>, pub noolddir: bool, pub extension: Option<String>, pub tabooext: Vec<String>, pub su: Option<String>, pub mail: bool, pub mailfirst: Option<String>, pub maillast: bool, pub nomail: bool, pub include: Vec<String>, pub shred: bool, pub shredcycles: u32, pub nocompress: bool, pub output_path: Option<String>,
}
Expand description

Logrotate 配置

Logrotate 是 Linux 系统上的日志轮转工具,用于管理日志文件的滚动、压缩和清理。 此配置用于生成 logrotate 配置文件,以便系统自动管理日志文件。

支持 logrotate 的完整配置选项,包括轮转策略、压缩、脚本执行、邮件通知等。

Fields§

§enabled: bool

是否启用 logrotate(默认 false,需要手动配置)

§path: String

日志文件路径模式(支持通配符,如 /var/log/app/*.log)

§rotate: String

轮转周期(daily | weekly | monthly | yearly | size)

  • daily: 每天轮转
  • weekly: 每周轮转
  • monthly: 每月轮转
  • yearly: 每年轮转
  • size: 按大小轮转(需要配合 size 或 maxsize 使用)
§size: Option<String>

文件大小限制(当日志文件达到此大小时进行轮转,单位:KB, MB, GB) 例如:100M, 1G。与 rotate 配合使用,当 rotate 为 size 时优先使用此选项

§maxsize: Option<String>

最大文件大小(当 rotate 为 size 时使用,单位:KB, MB, GB) 例如:100M, 1G(已废弃,建议使用 size)

§minsize: Option<String>

最小文件大小(小于此大小的文件不进行轮转,单位:KB, MB, GB) 例如:1M

§rotate_count: u32

保留的日志文件数量(超过此数量的旧日志将被删除)

§maxage: Option<u32>

最大保留天数(超过此天数的旧日志将被删除) 例如:30 表示保留 30 天

§start: u32

起始轮转编号(默认从 1 开始)

§compress: bool

是否压缩旧日志文件(默认 true)

§delaycompress: u32

压缩延迟(轮转后 N 个周期再压缩,0 表示立即压缩)

§compresscmd: Option<String>

压缩命令(默认使用 gzip) 例如:/usr/bin/bzip2

§uncompresscmd: Option<String>

解压缩命令(默认使用 gunzip) 例如:/usr/bin/bunzip2

§compressext: String

压缩文件扩展名(默认 .gz) 例如:.bz2, .xz

§compressoptions: Option<String>

压缩选项(传递给压缩命令的选项) 例如:-9(最高压缩级别)

§create: bool

是否在轮转时创建新文件(默认 true)

§create_mode: String

新文件的权限(八进制格式,如 0644)

§create_owner: Option<String>

新文件的所有者(user:group,如 app:app)

§copy: bool

是否使用 copy 模式(复制原文件后截断,而不是移动) 适用于无法重命名的日志文件(默认 false)

§copytruncate: bool

是否使用 copytruncate 模式(复制后截断原文件) 适用于无法重命名或发送信号的应用程序(默认 false)

§postrotate: bool

是否在轮转后执行脚本(默认 false)

§postrotate_script: Option<String>

轮转后执行的脚本命令(postrotate 为 true 时使用) 例如:“/bin/kill -HUP cat /var/run/app.pid 2> /dev/null 2> /dev/null || true”

§prerotate: bool

是否在轮转前执行脚本(默认 false)

§prerotate_script: Option<String>

轮转前执行的脚本命令(prerotate 为 true 时使用)

§firstaction: bool

是否在第一次轮转前执行脚本(默认 false)

§firstaction_script: Option<String>

第一次轮转前执行的脚本命令(firstaction 为 true 时使用)

§lastaction: bool

是否在最后一次轮转后执行脚本(默认 false)

§lastaction_script: Option<String>

最后一次轮转后执行的脚本命令(lastaction 为 true 时使用)

§missingok: bool

是否忽略缺失的日志文件(默认 false,缺失文件会报错)

§notifempty: bool

是否在日志文件为空时不轮转(默认 false)

§ifempty: bool

是否即使文件为空也轮转(与 notifempty 相反,默认 false)

§sharedscripts: bool

是否共享脚本(默认 false,每个日志组共享一个脚本)

§dateext: bool

是否使用日期扩展名(默认 false,使用数字扩展名 .1, .2, …) 为 true 时使用日期格式,如 .20260114

§dateformat: String

日期扩展名格式(dateext 为 true 时使用) 默认:%Y%m%d(年月日) 其他选项:%Y%m%d-%H%M%S(年月日-时分秒)

§olddir: Option<String>

旧日志文件目录(可选,用于将旧日志移动到指定目录) 例如:/var/log/app/archive

§noolddir: bool

是否不使用 olddir(默认 false)

§extension: Option<String>

压缩文件扩展名(用于指定压缩后的文件扩展名) 例如:.log.gz

§tabooext: Vec<String>

禁止的扩展名列表(这些扩展名的文件不会被轮转) 例如:[“~”, “.bak”]

§su: Option<String>

以指定用户身份运行脚本(user:group) 例如:app:app

§mail: bool

是否发送邮件通知(默认 false)

§mailfirst: Option<String>

邮件地址(轮转前发送邮件) 例如:admin@example.com

§maillast: bool

是否在轮转后发送邮件(默认 false)

§nomail: bool

是否不发送邮件(默认 false)

§include: Vec<String>

包含其他配置文件 例如:/etc/logrotate.d/includes

§shred: bool

是否安全删除文件(使用 shred 命令,默认 false)

§shredcycles: u32

安全删除循环次数(shred 为 true 时使用,默认 0)

§nocompress: bool

是否不压缩(与 compress 相反,默认 false)

§output_path: Option<String>

logrotate 配置文件输出路径(可选) 如果未指定,将使用可执行文件目录下的 logrotate/app.conf 例如:“./logrotate/app.conf” 或 “/etc/logrotate.d/app”

Implementations§

Source§

impl LogrotateConfig

Source

pub fn to_logrotate_config(&self) -> String

生成 logrotate 配置文件内容

Source

pub fn generate_config_file(&self, default_path: Option<&str>) -> Result<String>

生成并保存 logrotate 配置文件

§参数
  • default_path - 默认输出路径(当 output_path 未指定时使用) 如果为 None,则自动使用当前可执行文件目录下的 logrotate/app.conf
§返回值
  • 生成的配置文件路径

Trait Implementations§

Source§

impl Clone for LogrotateConfig

Source§

fn clone(&self) -> LogrotateConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LogrotateConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for LogrotateConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for LogrotateConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,