hyperlane-log 4.0.6

A Rust logging library that supports both asynchronous and synchronous logging. It provides multiple log levels, such as error, info, and debug. Users can define custom log handling methods and configure log file paths. The library supports log rotation, automatically creating a new log file when the current file reaches the specified size limit. It allows flexible logging configurations, making it suitable for both high-performance asynchronous applications and traditional synchronous logging scenarios. The asynchronous mode utilizes Tokio's async channels for efficient log buffering, while the synchronous mode writes logs directly to the file system.
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use crate::*;

/// Blanket implementation for any function matching FileLoggerFuncTrait signature.
///
/// This allows any compatible closure or function to be used as a log formatter.
impl<F, T> FileLoggerFuncTrait<T> for F
where
    F: Fn(T) -> String + Send + Sync,
    T: AsRef<str>,
{
}

/// Default implementation for FileLogger configuration.
impl Default for FileLogger {
    /// Creates default FileLogger configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Default FileLogger instance with default path and file size limit.
    #[inline(always)]
    fn default() -> Self {
        Self {
            path: DEFAULT_LOG_DIR.to_owned(),
            limit_file_size: DEFAULT_LOG_FILE_SIZE,
            trace_dir: TRACE_DIR.to_owned(),
            debug_dir: DEBUG_DIR.to_owned(),
            info_dir: INFO_DIR.to_owned(),
            warn_dir: WARN_DIR.to_owned(),
            error_dir: ERROR_DIR.to_owned(),
        }
    }
}

impl FileLogger {
    /// Creates new FileLogger configuration with specified parameters.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The path for storing log files, which will be converted to string slice.
    /// - `usize` - The maximum file size limit in bytes.
    ///
    /// # Returns
    ///
    /// - `Self` - A new FileLogger instance with specified configuration.
    #[inline(always)]
    pub fn new<P: AsRef<str>>(path: P, limit_file_size: usize) -> Self {
        Self {
            path: path.as_ref().to_owned(),
            limit_file_size,
            trace_dir: TRACE_DIR.to_owned(),
            debug_dir: DEBUG_DIR.to_owned(),
            info_dir: INFO_DIR.to_owned(),
            warn_dir: WARN_DIR.to_owned(),
            error_dir: ERROR_DIR.to_owned(),
        }
    }

    /// Gets the log file storage path.
    ///
    /// # Returns
    ///
    /// - `&String` - Reference to the log file storage path.
    #[inline(always)]
    pub fn get_path(&self) -> &String {
        &self.path
    }

    /// Gets the maximum allowed size (in bytes) for individual log files.
    ///
    /// # Returns
    ///
    /// - `&usize` - Reference to the maximum file size limit in bytes.
    #[inline(always)]
    pub fn get_limit_file_size(&self) -> &usize {
        &self.limit_file_size
    }

    /// Gets the trace log directory name.
    ///
    /// # Returns
    ///
    /// - `&String` - Reference to the trace log directory name.
    #[inline(always)]
    pub fn get_trace_dir(&self) -> &String {
        &self.trace_dir
    }

    /// Gets the debug log directory name.
    ///
    /// # Returns
    ///
    /// - `&String` - Reference to the debug log directory name.
    #[inline(always)]
    pub fn get_debug_dir(&self) -> &String {
        &self.debug_dir
    }

    /// Gets the info log directory name.
    ///
    /// # Returns
    ///
    /// - `&String` - Reference to the info log directory name.
    #[inline(always)]
    pub fn get_info_dir(&self) -> &String {
        &self.info_dir
    }

    /// Gets the warn log directory name.
    ///
    /// # Returns
    ///
    /// - `&String` - Reference to the warn log directory name.
    #[inline(always)]
    pub fn get_warn_dir(&self) -> &String {
        &self.warn_dir
    }

    /// Gets the error log directory name.
    ///
    /// # Returns
    ///
    /// - `&String` - Reference to the error log directory name.
    #[inline(always)]
    pub fn get_error_dir(&self) -> &String {
        &self.error_dir
    }

    /// Sets the log file storage path.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The new path for storing log files, which will be converted to string slice.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_path<P: AsRef<str>>(&mut self, path: P) -> &mut Self {
        self.path = path.as_ref().to_owned();
        self
    }

    /// Sets the maximum size limit for log files.
    ///
    /// # Arguments
    ///
    /// - `usize` - The new maximum file size limit in bytes.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_limit_file_size(&mut self, limit_file_size: usize) -> &mut Self {
        self.limit_file_size = limit_file_size;
        self
    }

    /// Sets the trace log directory name.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The directory name for trace logs.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_trace_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
        self.trace_dir = dir.as_ref().to_owned();
        self
    }

    /// Sets the debug log directory name.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The directory name for debug logs.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_debug_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
        self.debug_dir = dir.as_ref().to_owned();
        self
    }

    /// Sets the info log directory name.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The directory name for info logs.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_info_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
        self.info_dir = dir.as_ref().to_owned();
        self
    }

    /// Sets the warn log directory name.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The directory name for warn logs.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_warn_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
        self.warn_dir = dir.as_ref().to_owned();
        self
    }

    /// Sets the error log directory name.
    ///
    /// # Arguments
    ///
    /// - `P: AsRef<str>` - The directory name for error logs.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - Mutable reference to self for method chaining.
    #[inline(always)]
    pub fn set_error_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
        self.error_dir = dir.as_ref().to_owned();
        self
    }

    /// Checks if logging is enabled.
    ///
    /// # Returns
    ///
    /// - `bool` - True if logging is enabled.
    #[inline(always)]
    pub fn is_enable(&self) -> bool {
        self.limit_file_size != DISABLE_LOG_FILE_SIZE
    }

    /// Checks if logging is disabled.
    ///
    /// # Returns
    ///
    /// - `bool` - True if logging is disabled.
    #[inline(always)]
    pub fn is_disable(&self) -> bool {
        !self.is_enable()
    }

    /// Writes log data synchronously to specified directory.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - The data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - The log formatting function.
    /// - `&str` - The subdirectory for log file.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self for method chaining.
    fn write_sync<T, L>(&self, data: T, func: L, dir: &str) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        if self.is_disable() {
            return self;
        }
        let out: String = func(data);
        let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
        let _ = append_to_file(&path, out.as_bytes());
        self
    }

    /// Writes log data asynchronously to specified directory.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - The data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - The log formatting function.
    /// - `&str` - The subdirectory for log file.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self for method chaining.
    async fn write_async<T, L>(&self, data: T, func: L, dir: &str) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        if self.is_disable() {
            return self;
        }
        let out: String = func(data);
        let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
        let _ = async_append_to_file(&path, out.as_bytes()).await;
        self
    }

    /// Logs trace message synchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Trace data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub fn trace<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_sync(data, func, &self.trace_dir)
    }

    /// Logs trace message asynchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Trace data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub async fn async_trace<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_async(data, func, &self.trace_dir).await
    }

    /// Logs debug message synchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub fn debug<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_sync(data, func, &self.debug_dir)
    }

    /// Logs debug message asynchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub async fn async_debug<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_async(data, func, &self.debug_dir).await
    }

    /// Logs info message synchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub fn info<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_sync(data, func, &self.info_dir)
    }

    /// Logs info message asynchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub async fn async_info<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_async(data, func, &self.info_dir).await
    }

    /// Logs warn message synchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Warn data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub fn warn<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_sync(data, func, &self.warn_dir)
    }

    /// Logs warn message asynchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Warn data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub async fn async_warn<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_async(data, func, &self.warn_dir).await
    }

    /// Logs error message synchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub fn error<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_sync(data, func, &self.error_dir)
    }

    /// Logs error message asynchronously.
    ///
    /// # Arguments
    ///
    /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
    /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
    ///
    /// # Returns
    ///
    /// - `&Self` - Reference to self.
    pub async fn async_error<T, L>(&self, data: T, func: L) -> &Self
    where
        T: AsRef<str>,
        L: FileLoggerFuncTrait<T>,
    {
        self.write_async(data, func, &self.error_dir).await
    }
}