hyperlane_log/log/impl.rs
1use crate::*;
2
3/// Blanket implementation for any function matching FileLoggerFuncTrait signature.
4///
5/// This allows any compatible closure or function to be used as a log formatter.
6impl<F, T> FileLoggerFuncTrait<T> for F
7where
8 F: Fn(T) -> String + Send + Sync,
9 T: AsRef<str>,
10{
11}
12
13/// Default implementation for FileLogger configuration.
14impl Default for FileLogger {
15 /// Creates default FileLogger configuration.
16 ///
17 /// # Returns
18 ///
19 /// - `Self` - Default FileLogger instance with default path and file size limit.
20 #[inline(always)]
21 fn default() -> Self {
22 Self {
23 path: DEFAULT_LOG_DIR.to_owned(),
24 limit_file_size: DEFAULT_LOG_FILE_SIZE,
25 trace_dir: TRACE_DIR.to_owned(),
26 debug_dir: DEBUG_DIR.to_owned(),
27 info_dir: INFO_DIR.to_owned(),
28 warn_dir: WARN_DIR.to_owned(),
29 error_dir: ERROR_DIR.to_owned(),
30 }
31 }
32}
33
34impl FileLogger {
35 /// Creates new FileLogger configuration with specified parameters.
36 ///
37 /// # Arguments
38 ///
39 /// - `P: AsRef<str>` - The path for storing log files, which will be converted to string slice.
40 /// - `usize` - The maximum file size limit in bytes.
41 ///
42 /// # Returns
43 ///
44 /// - `Self` - A new FileLogger instance with specified configuration.
45 #[inline(always)]
46 pub fn new<P: AsRef<str>>(path: P, limit_file_size: usize) -> Self {
47 Self {
48 path: path.as_ref().to_owned(),
49 limit_file_size,
50 trace_dir: TRACE_DIR.to_owned(),
51 debug_dir: DEBUG_DIR.to_owned(),
52 info_dir: INFO_DIR.to_owned(),
53 warn_dir: WARN_DIR.to_owned(),
54 error_dir: ERROR_DIR.to_owned(),
55 }
56 }
57
58 /// Gets the log file storage path.
59 ///
60 /// # Returns
61 ///
62 /// - `&String` - Reference to the log file storage path.
63 #[inline(always)]
64 pub fn get_path(&self) -> &String {
65 &self.path
66 }
67
68 /// Gets the maximum allowed size (in bytes) for individual log files.
69 ///
70 /// # Returns
71 ///
72 /// - `&usize` - Reference to the maximum file size limit in bytes.
73 #[inline(always)]
74 pub fn get_limit_file_size(&self) -> &usize {
75 &self.limit_file_size
76 }
77
78 /// Gets the trace log directory name.
79 ///
80 /// # Returns
81 ///
82 /// - `&String` - Reference to the trace log directory name.
83 #[inline(always)]
84 pub fn get_trace_dir(&self) -> &String {
85 &self.trace_dir
86 }
87
88 /// Gets the debug log directory name.
89 ///
90 /// # Returns
91 ///
92 /// - `&String` - Reference to the debug log directory name.
93 #[inline(always)]
94 pub fn get_debug_dir(&self) -> &String {
95 &self.debug_dir
96 }
97
98 /// Gets the info log directory name.
99 ///
100 /// # Returns
101 ///
102 /// - `&String` - Reference to the info log directory name.
103 #[inline(always)]
104 pub fn get_info_dir(&self) -> &String {
105 &self.info_dir
106 }
107
108 /// Gets the warn log directory name.
109 ///
110 /// # Returns
111 ///
112 /// - `&String` - Reference to the warn log directory name.
113 #[inline(always)]
114 pub fn get_warn_dir(&self) -> &String {
115 &self.warn_dir
116 }
117
118 /// Gets the error log directory name.
119 ///
120 /// # Returns
121 ///
122 /// - `&String` - Reference to the error log directory name.
123 #[inline(always)]
124 pub fn get_error_dir(&self) -> &String {
125 &self.error_dir
126 }
127
128 /// Sets the log file storage path.
129 ///
130 /// # Arguments
131 ///
132 /// - `P: AsRef<str>` - The new path for storing log files, which will be converted to string slice.
133 ///
134 /// # Returns
135 ///
136 /// - `&mut Self` - Mutable reference to self for method chaining.
137 #[inline(always)]
138 pub fn set_path<P: AsRef<str>>(&mut self, path: P) -> &mut Self {
139 self.path = path.as_ref().to_owned();
140 self
141 }
142
143 /// Sets the maximum size limit for log files.
144 ///
145 /// # Arguments
146 ///
147 /// - `usize` - The new maximum file size limit in bytes.
148 ///
149 /// # Returns
150 ///
151 /// - `&mut Self` - Mutable reference to self for method chaining.
152 #[inline(always)]
153 pub fn set_limit_file_size(&mut self, limit_file_size: usize) -> &mut Self {
154 self.limit_file_size = limit_file_size;
155 self
156 }
157
158 /// Sets the trace log directory name.
159 ///
160 /// # Arguments
161 ///
162 /// - `P: AsRef<str>` - The directory name for trace logs.
163 ///
164 /// # Returns
165 ///
166 /// - `&mut Self` - Mutable reference to self for method chaining.
167 #[inline(always)]
168 pub fn set_trace_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
169 self.trace_dir = dir.as_ref().to_owned();
170 self
171 }
172
173 /// Sets the debug log directory name.
174 ///
175 /// # Arguments
176 ///
177 /// - `P: AsRef<str>` - The directory name for debug logs.
178 ///
179 /// # Returns
180 ///
181 /// - `&mut Self` - Mutable reference to self for method chaining.
182 #[inline(always)]
183 pub fn set_debug_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
184 self.debug_dir = dir.as_ref().to_owned();
185 self
186 }
187
188 /// Sets the info log directory name.
189 ///
190 /// # Arguments
191 ///
192 /// - `P: AsRef<str>` - The directory name for info logs.
193 ///
194 /// # Returns
195 ///
196 /// - `&mut Self` - Mutable reference to self for method chaining.
197 #[inline(always)]
198 pub fn set_info_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
199 self.info_dir = dir.as_ref().to_owned();
200 self
201 }
202
203 /// Sets the warn log directory name.
204 ///
205 /// # Arguments
206 ///
207 /// - `P: AsRef<str>` - The directory name for warn logs.
208 ///
209 /// # Returns
210 ///
211 /// - `&mut Self` - Mutable reference to self for method chaining.
212 #[inline(always)]
213 pub fn set_warn_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
214 self.warn_dir = dir.as_ref().to_owned();
215 self
216 }
217
218 /// Sets the error log directory name.
219 ///
220 /// # Arguments
221 ///
222 /// - `P: AsRef<str>` - The directory name for error logs.
223 ///
224 /// # Returns
225 ///
226 /// - `&mut Self` - Mutable reference to self for method chaining.
227 #[inline(always)]
228 pub fn set_error_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
229 self.error_dir = dir.as_ref().to_owned();
230 self
231 }
232
233 /// Checks if logging is enabled.
234 ///
235 /// # Returns
236 ///
237 /// - `bool` - True if logging is enabled.
238 #[inline(always)]
239 pub fn is_enable(&self) -> bool {
240 self.limit_file_size != DISABLE_LOG_FILE_SIZE
241 }
242
243 /// Checks if logging is disabled.
244 ///
245 /// # Returns
246 ///
247 /// - `bool` - True if logging is disabled.
248 #[inline(always)]
249 pub fn is_disable(&self) -> bool {
250 !self.is_enable()
251 }
252
253 /// Writes log data synchronously to specified directory.
254 ///
255 /// # Arguments
256 ///
257 /// - `AsRef<str>` - The data to be logged, which will be converted to string slice.
258 /// - `L: FileLoggerFuncTrait<T>` - The log formatting function.
259 /// - `&str` - The subdirectory for log file.
260 ///
261 /// # Returns
262 ///
263 /// - `&Self` - Reference to self for method chaining.
264 fn write_sync<T, L>(&self, data: T, func: L, dir: &str) -> &Self
265 where
266 T: AsRef<str>,
267 L: FileLoggerFuncTrait<T>,
268 {
269 if self.is_disable() {
270 return self;
271 }
272 let out: String = func(data);
273 let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
274 let _ = append_to_file(&path, out.as_bytes());
275 self
276 }
277
278 /// Writes log data asynchronously to specified directory.
279 ///
280 /// # Arguments
281 ///
282 /// - `AsRef<str>` - The data to be logged, which will be converted to string slice.
283 /// - `L: FileLoggerFuncTrait<T>` - The log formatting function.
284 /// - `&str` - The subdirectory for log file.
285 ///
286 /// # Returns
287 ///
288 /// - `&Self` - Reference to self for method chaining.
289 async fn write_async<T, L>(&self, data: T, func: L, dir: &str) -> &Self
290 where
291 T: AsRef<str>,
292 L: FileLoggerFuncTrait<T>,
293 {
294 if self.is_disable() {
295 return self;
296 }
297 let out: String = func(data);
298 let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
299 let _ = async_append_to_file(&path, out.as_bytes()).await;
300 self
301 }
302
303 /// Logs trace message synchronously.
304 ///
305 /// # Arguments
306 ///
307 /// - `AsRef<str>` - Trace data to be logged, which will be converted to string slice.
308 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
309 ///
310 /// # Returns
311 ///
312 /// - `&Self` - Reference to self.
313 pub fn trace<T, L>(&self, data: T, func: L) -> &Self
314 where
315 T: AsRef<str>,
316 L: FileLoggerFuncTrait<T>,
317 {
318 self.write_sync(data, func, &self.trace_dir)
319 }
320
321 /// Logs trace message asynchronously.
322 ///
323 /// # Arguments
324 ///
325 /// - `AsRef<str>` - Trace data to be logged, which will be converted to string slice.
326 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
327 ///
328 /// # Returns
329 ///
330 /// - `&Self` - Reference to self.
331 pub async fn async_trace<T, L>(&self, data: T, func: L) -> &Self
332 where
333 T: AsRef<str>,
334 L: FileLoggerFuncTrait<T>,
335 {
336 self.write_async(data, func, &self.trace_dir).await
337 }
338
339 /// Logs debug message synchronously.
340 ///
341 /// # Arguments
342 ///
343 /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
344 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
345 ///
346 /// # Returns
347 ///
348 /// - `&Self` - Reference to self.
349 pub fn debug<T, L>(&self, data: T, func: L) -> &Self
350 where
351 T: AsRef<str>,
352 L: FileLoggerFuncTrait<T>,
353 {
354 self.write_sync(data, func, &self.debug_dir)
355 }
356
357 /// Logs debug message asynchronously.
358 ///
359 /// # Arguments
360 ///
361 /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
362 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
363 ///
364 /// # Returns
365 ///
366 /// - `&Self` - Reference to self.
367 pub async fn async_debug<T, L>(&self, data: T, func: L) -> &Self
368 where
369 T: AsRef<str>,
370 L: FileLoggerFuncTrait<T>,
371 {
372 self.write_async(data, func, &self.debug_dir).await
373 }
374
375 /// Logs info message synchronously.
376 ///
377 /// # Arguments
378 ///
379 /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
380 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
381 ///
382 /// # Returns
383 ///
384 /// - `&Self` - Reference to self.
385 pub fn info<T, L>(&self, data: T, func: L) -> &Self
386 where
387 T: AsRef<str>,
388 L: FileLoggerFuncTrait<T>,
389 {
390 self.write_sync(data, func, &self.info_dir)
391 }
392
393 /// Logs info message asynchronously.
394 ///
395 /// # Arguments
396 ///
397 /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
398 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
399 ///
400 /// # Returns
401 ///
402 /// - `&Self` - Reference to self.
403 pub async fn async_info<T, L>(&self, data: T, func: L) -> &Self
404 where
405 T: AsRef<str>,
406 L: FileLoggerFuncTrait<T>,
407 {
408 self.write_async(data, func, &self.info_dir).await
409 }
410
411 /// Logs warn message synchronously.
412 ///
413 /// # Arguments
414 ///
415 /// - `AsRef<str>` - Warn data to be logged, which will be converted to string slice.
416 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
417 ///
418 /// # Returns
419 ///
420 /// - `&Self` - Reference to self.
421 pub fn warn<T, L>(&self, data: T, func: L) -> &Self
422 where
423 T: AsRef<str>,
424 L: FileLoggerFuncTrait<T>,
425 {
426 self.write_sync(data, func, &self.warn_dir)
427 }
428
429 /// Logs warn message asynchronously.
430 ///
431 /// # Arguments
432 ///
433 /// - `AsRef<str>` - Warn data to be logged, which will be converted to string slice.
434 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
435 ///
436 /// # Returns
437 ///
438 /// - `&Self` - Reference to self.
439 pub async fn async_warn<T, L>(&self, data: T, func: L) -> &Self
440 where
441 T: AsRef<str>,
442 L: FileLoggerFuncTrait<T>,
443 {
444 self.write_async(data, func, &self.warn_dir).await
445 }
446
447 /// Logs error message synchronously.
448 ///
449 /// # Arguments
450 ///
451 /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
452 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
453 ///
454 /// # Returns
455 ///
456 /// - `&Self` - Reference to self.
457 pub fn error<T, L>(&self, data: T, func: L) -> &Self
458 where
459 T: AsRef<str>,
460 L: FileLoggerFuncTrait<T>,
461 {
462 self.write_sync(data, func, &self.error_dir)
463 }
464
465 /// Logs error message asynchronously.
466 ///
467 /// # Arguments
468 ///
469 /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
470 /// - `L: FileLoggerFuncTrait<T>` - FileLogger formatting function.
471 ///
472 /// # Returns
473 ///
474 /// - `&Self` - Reference to self.
475 pub async fn async_error<T, L>(&self, data: T, func: L) -> &Self
476 where
477 T: AsRef<str>,
478 L: FileLoggerFuncTrait<T>,
479 {
480 self.write_async(data, func, &self.error_dir).await
481 }
482}