1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! # ltrace — Rolling file writer and multi-target logger for `tracing` and `log`
//!
//! This crate provides a high-performance rolling file writer with:
//!
//! - **Log rotation by time**: MINUTELY, HOURLY, DAILY
//! - **Log rotation by size**: configurable max file size (independent of time rotation)
//! - **Time + size combined rotation**: even when using time-based rotation (e.g. Daily),
//! a size limit still triggers additional rotations within that window (e.g., multiple
//! rotations per day if the log exceeds the size threshold)
//! - **Compression**: optional gzip compression for rotated files (feature `compression`),
//! performed on a background thread so it does not block writes
//! - **Max file count**: automatically prune old log files
//! - **Dynamic log level**: change the log level at runtime via [`LogHandle`]
//! - **Timezone configuration**: UTC (default) or local timezone for rotation timestamps
//! - **Auto-create directories**: parent directories are created automatically if they don't exist
//!
//! ## Using with `tracing`
//!
//! Since [`RollingWriter`] implements `std::io::Write`, you can use it with
//! `tracing_appender::non_blocking()` to get non-blocking writes:
//!
//! ```ignore
//! use ltrace::{RollingWriter, Rotation, Compression};
//!
//! let writer = RollingWriter::builder("/var/log/myapp/app.log")
//! .rotation(Rotation::Daily)
//! .max_file_size(10 * 1024 * 1024) // 10 MB
//! .max_files(10)
//! .compression(Compression::Gzip)
//! .build()
//! .unwrap();
//!
//! let (non_blocking, _guard) = tracing_appender::non_blocking(writer);
//! let subscriber = tracing_subscriber::fmt()
//! .with_writer(non_blocking)
//! .finish()
//! .init()?;
//! ```
//!
//! ## Output targets
//!
//! ltrace supports multiple log output targets that can be combined:
//!
//! - **Rolling file logs**: [`LogWriter`] for time/size-based rotating file logs
//! - **Console output**: [`ConsoleWriter`] for colored terminal output (feature `console`)
//! - **Multi-target output**: [`MultiLog`] to combine multiple writers together
//!
//! ## Rotated file naming
//!
//! Rotated filenames use a nanosecond-precision timestamp (nanoseconds since UNIX epoch)
//! to guarantee strict ordering and uniqueness, with an optional date prefix:
//!
//! - Daily rotation: `app.log.2024-01-15T1780358400000000000`
//! - Hourly rotation: `app.log.2024-01-15-14T1780358400000000000`
//! - Minutely rotation: `app.log.2024-01-15-14-30T1780358400000000000`
//! - Size-only (Never): `app.log.1780358400000000000`
//!
//! With gzip compression enabled: `app.log.2024-01-15T1780358400000000000.gz`
//!
//! # Quick start
//!
//! Enable the `log` feature in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! ltrace = { version = "0.1", features = ["log"] }
//! ```
//!
//! The recommended way is to call [`LogWriter::init`] directly:
//!
//! ```ignore
//! use ltrace::LogWriter;
//!
//! let handle = LogWriter::init("app.log")?;
//!
//! // Dynamically change level at runtime:
//! handle.set_level(log::LevelFilter::Debug);
//! ```
//!
//! # Full builder configuration
//!
//! Use [`LogWriter::builder`] for complete control over rotation, compression, and levels:
//!
//! ```ignore
//! use ltrace::{LogWriter, Rotation, Compression};
//!
//! let handle = LogWriter::builder("/var/log/myapp/app.log")
//! .rotation(Rotation::Daily)
//! .max_file_size(10 * 1024 * 1024) // also rotate within the same day if size exceeded
//! .max_files(5)
//! .compression(Compression::Gzip)
//! .level(log::LevelFilter::Info)
//! .init()?;
//! ```
//!
//! # Console output (feature `console`)
//!
//! Enable the `console` feature in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! ltrace = { version = "0.1", features = ["console"] }
//! ```
//!
//! ```ignore
//! use ltrace::ConsoleWriter;
//!
//! let handle = ConsoleWriter::builder()
//! .level(log::LevelFilter::Debug)
//! .init()?;
//! ```
//!
//! # Multi-target output (console + file)
//!
//! ```ignore
//! use ltrace::{MultiLog, ConsoleWriter, LogWriter, Rotation, Compression};
//!
//! let multi = MultiLog::new()
//! .writer(ConsoleWriter::builder()
//! .level(log::LevelFilter::Info)
//! .build()?)
//! .writer(LogWriter::builder("/var/log/myapp/app.log")
//! .rotation(Rotation::Daily)
//! .compression(Compression::Gzip)
//! .level(log::LevelFilter::Debug)
//! .build()?)
//! .init()?;
//! ```
//!
//! # Multi-target with per-writer level control
//!
//! ```ignore
//! use ltrace::{MultiLog, ConsoleWriter, LogWriter};
//!
//! let (multi, handles) = MultiLog::new()
//! .writer_with_handle(ConsoleWriter::builder().build()?)
//! .writer_with_handle(LogWriter::builder("app.log").build()?)
//! .build_with_handles()?;
//!
//! // Adjust each writer's level independently:
//! handles[0].set_level(log::LevelFilter::Warn); // console: only warn+
//! handles[1].set_level(log::LevelFilter::Debug); // file: debug+
//! ```
//!
//! # Dynamic log level control
//!
//! [`LogHandle`] is `Clone + Send + Sync`, so you can move it to other threads:
//!
//! ```ignore
//! let handle = LogWriter::init("app.log")?;
//!
//! // Spawn a thread that listens for signals to change the log level.
//! let handle_clone = handle.clone();
//! std::thread::spawn(move || {
//! // e.g., listen for SIGHUP or an HTTP endpoint
//! handle_clone.set_level(log::LevelFilter::Debug);
//! });
//! ```
// Core types (always available)
pub use ;
pub use RollingWriter;
pub use Compression;
// Log layer types (feature: log)
pub use LogHandle;
pub use ;
// Console writer types (feature: console)
pub use ;
// Multi writer types (feature: log)
pub use ;