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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//! [![github]](https://github.com/calizoots/luhlog) [![crates-io]](https://crates.io/crates/luhlog) [![docs-rs]](https://docs.rs/luhlog)
//!
//! [github]: https://img.shields.io/badge/github-calizoots/luhlog-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/crates/v/luhlog.svg?style=for-the-badge&color=fc8d62&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-luhlog-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! # LuhLog
//! ---------------------------------------------------------------------
//! A way to log your messages, it is nothing special at all.
//! It has a formatting system, convient macros and a global logger system >.<
//! > It is made with love though s.c <3
//!
//! So as a library we expose `luhlog::Logger` and trait `luhlog::Log`
//! for making custom log and also exposing `luhlog::LogFormatter` for
//! formatting.
//!
//! `luhlog::Level` has 6 levels similar to the log crate with Trace
//! being the lowest and Critical being the heighest in terms of precedence
//! we have corresponding macros for those levels for now they only
//! correspond to the global logger instance through get_logger().
//!
//! > This is still in dev stages I will amend that soon.
//! > Thank you for your patience.
//!
//! We also provide `luhlog::GlobalLogger` for making your own global
//! logger instance. Look at examples below for usage...
//!
//!
//! ## Examples
//! ---------------------------------------------------------------------
//! ```
//! use luhlog::{set_logger, Level, CompactFormatter, info, warn, trace};
//! fn main() {
//! // level sets the requirement... logs below this wont be logged
//! // formatter sets the formatter needs to implement LogFormatter
//! set_logger!(level: Level::Info, formatter: CompactFormatter);
//! info!("hello world");
//! warn!(target: "main", "targeting main <3");
//! // wont be printed
//! trace!("bine");
//! }
//! ```
//! ---------------------------------------------------------------------
//! ```
//! use luhlog::{set_logger, get_logger, Level, LogBuilder, Log, Logger, CompactFormatter};
//! fn main() {
//! set_logger!(level: Level::Trace);
//! let logger = get_logger();
//! let other_logger = Logger::with_formatter(
//! Level::Info,
//! std::sync::Arc::new(CompactFormatter)
//! ).no_stdout().file("test.log").expect("failed to open test.log");
//! // this allows for finer control of what your printing
//! logger.log(
//! // this has a default level of Info
//! LogBuilder::new("hello guys <3")
//! .level(Level::Trace)
//! .build()
//! );
//! other_logger.log(
//! LogBuilder::new("in the file >.<")
//! .target("main")
//! .level(Level::Warn)
//! .location(file!(), line!())
//! .build()
//! );
//! }
//! ```
//! ---------------------------------------------------------------------
//! ```
//! use luhlog::{GlobalLogger, Log, Level, LogBuilder};
//! static LOG: GlobalLogger = GlobalLogger::new();
//! fn main() {
//! LOG.get().log(
//! LogBuilder::new("test for GlobalLogger :3")
//! .target("LOG")
//! .level(Level::Info)
//! .location(file!(), line!())
//! .build()
//! );
//! }
//! ```
//! ---------------------------------------------------------------------
//! Hope you guys enjoy... any features or issues please message me
//! * Thank you for reading :)
use fmt;
use ;
use PathBuf;
use HashMap;
use ;
use ;
use ;
/// **`luhlog::Level`** is an enum representing the verbosity of a given message
/// `luhlog::LogRecord` carries all appropriate metadata for a given
/// log entry. There are two ways this is useful to you.
///
/// ## Provided Methods
/// ----------------------------------------------------------------
///
/// ### `new(level: Level, msg: impl Into<String>) -> Self`
/// Creates a new log record with the provided [`Level`] and message.
///
/// ### `with_target(target: impl Into<String>) -> Self`
/// Sets the record’s target (e.g., subsystem, module, etc).
///
/// ### `with_location(file: impl Into<String>, line: u32) -> Self`
/// Attaches file and line information to the record.
///
/// ### `with_metadata(key: impl Into<String>, value: impl Into<String>) -> Self`
/// Adds a custom metadata key–value pair to the record.
///
///
/// ## Examples
/// ----------------------------------------------------------------
/// > First example here is using `LogRecord::new` directly.
/// ```
/// use luhlog::{set_logger, get_logger, LogRecord, Log, Level};
/// fn main() {
/// set_logger!(level: Level::Trace);
/// let logger = get_logger();
/// logger.log(
/// LogRecord::new(Level::Info, "bonjour people :3")
/// .with_target("main")
/// .with_location(file!(), line!())
/// );
/// }
/// ```
/// ----------------------------------------------------------------
/// > Second example here is using `LogBuilder`.
/// ```
/// use luhlog::{set_logger, get_logger, LogBuilder, Log, Level};
/// fn main() {
/// set_logger!(level: Level::Trace);
/// let logger = get_logger();
/// logger.log(
/// // this has a default level of Info
/// LogBuilder::new("hello guys <3")
/// .level(Level::Info)
/// .target("main")
/// .location(file!(), line!())
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
/// `luhlog::LogBuilder` is an easy way to build a `LogRecord`.
/// Doesn't really do much apart from shorter combinators and
/// a predefined level for the record.
///
/// ## Provided Methods
/// ----------------------------------------------------------------
///
/// ### `new(msg: impl Into<String>) -> Self`
/// Creates a new builder with a default [`Level::Info`] and message.
///
/// ### `level(level: Level) -> Self`
/// Sets the log level for this record.
///
/// ### `target(target: impl Into<String>) -> Self`
/// Sets the target (arbitrary name).
///
/// ### `location(file: impl Into<String>, line: u32) -> Self`
/// Attaches file and line info to the record.
///
/// ### `metadata(key: impl Into<String>, value: impl Into<String>) -> Self`
/// Adds an arbitrary key–value pair to the record’s metadata.
///
/// ### `build() -> LogRecord`
/// Finalises and returns the constructed [`LogRecord`].
///
/// ## Example
/// ----------------------------------------------------------------
/// ```
/// use luhlog::{set_logger, get_logger, LogBuilder, Log, Level};
/// fn main() {
/// set_logger!(level: Level::Trace);
/// let logger = get_logger();
/// logger.log(
/// // this has a default level of Info
/// LogBuilder::new("hello guys <3")
/// .level(Level::Info)
/// .target("main")
/// .location(file!(), line!())
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
/// `luhlog::LogFormatter` decides **how your logs look**.
///
/// This trait is what `luhlog::Logger` uses internally to turn a raw
/// [`LogRecord`](crate::LogRecord) into something actually readable.
///
/// By default, we ship a few built-ins like:
/// - [`DefaultFormatter`](crate::DefaultFormatter) → timestamp, level, file, line, etc
/// - [`CompactFormatter`](crate::CompactFormatter) → small, just level + msg
/// - [`JsonFormatter`](crate::JsonFormatter) → *“please don’t use this in prod” edition*
///
/// But of course, you can make your own formatter that fits your needs/wants.
///
/// ## Methods
/// ----------------------------------------------------------------
///
/// ### `time_format(&self) -> &str`
///
/// Returns a strftime-style format string used for timestamps.
/// Default is `"%H:%M:%S"`.
///
/// ```ignore
/// fn time_format(&self) -> &str {
/// "%Y-%m-%d %H:%M:%S"
/// }
/// ```
///
/// ### `format(&self, record: &LogRecord) -> String`
///
/// Called for every log record to generate the final output string.
///
/// ```ignore
/// fn format(&self, record: &LogRecord) -> String {
/// format!("[{}] >> {}", record.level, record.msg)
/// }
/// ```
///
/// ## Example
/// ----------------------------------------------------------------
/// ```
/// use luhlog::{LogFormatter, Logger, set_logger, Level, LogRecord, info};
///
/// #[derive(Debug)]
/// struct MyFormatter;
///
/// impl LogFormatter for MyFormatter {
/// fn format(&self, record: &LogRecord) -> String {
/// format!(">>> {} <<<", record.msg)
/// }
/// }
///
/// fn main() {
/// set_logger!(level: Level::Trace, formatter: MyFormatter);
/// info!("new custom format");
/// }
/// ```
/// ----------------------------------------------------------------
/// In the name a default formatter <3
;
/// Small basic formatter
;
/// `luhlog::JsonFormatter` **barely works at all if you need this
/// write your own implementation using serde escaping json
/// this will not work in prod!!!**
;
/// `luhlog::Log` is the core trait that powers all logging backends.
///
/// Implementing this allows you to define how your logs are handled —
/// whether you want to print them, write them to disk, send them over
/// a socket, or whatever you want g.
///
/// The default provided [`Logger`](crate::Logger) struct already implements
/// this trait, but if you want complete control, make your own.
///
/// ## Required Methods
/// ----------------------------------------------------------------
///
/// ### `enabled(&self, level: Level) -> bool`
/// Used to determine if a given log level should be processed.
/// This is where you can implement filtering logic, e.g.:
/// ```ignore
/// fn enabled(&self, level: Level) -> bool {
/// level >= self.min_level
/// }
/// ```
///
/// ### `log(&self, record: LogRecord)`
/// This is called when an event is ready to be logged.
/// Handle it however you like — format it, send it, print it, etc.
///
/// ```ignore
/// fn log(&self, record: LogRecord) {
/// let formatted = self.formatter.format(&record);
/// println!("{}", formatted);
/// }
/// ```
///
/// ### `flush(&self)`
/// Optional cleanup method. Called when the logger is dropped or
/// when [`flush()`](crate::flush) is manually invoked.
/// Perfect for file-based or buffered loggers.
///
/// ## Example
/// ----------------------------------------------------------------
/// ```
/// use luhlog::{Log, LogRecord, Level};
/// struct MyLogger;
/// impl Log for MyLogger {
/// fn enabled(&self, level: Level) -> bool {
/// level >= Level::Info
/// }
/// fn log(&self, record: LogRecord) {
/// println!("[{}] {}", record.level, record.msg);
/// }
/// fn flush(&self) {
/// println!("flushing logs :)");
/// }
/// }
/// ```
/// ----------------------------------------------------------------
///
/// > ✨ tip: you almost never need to implement this manually unless
/// you’re doing something different — like remote log streaming or sumn.
/// Otherwise, [`Logger`](crate::Logger) is plenty.
/// `luhlog::Logger` is the main logging backend implementation.
/// It handles log filtering, formatting, writing to files, and optional
/// output to stdout.
///
/// The `Logger` implements [`luhlog::Log`], so it can be used
/// anywhere a generic `Log` trait object is expected.
///
/// ## Provided Methods
/// ----------------------------------------------------------------
///
/// ### `new(min_level: Level) -> Self`
/// Creates a new logger with the given minimum level and a [`DefaultFormatter`] attached.
///
/// ### `with_formatter(min_level: Level, formatter: Arc<dyn LogFormatter>) -> Self`
/// Creates a logger with a custom formatter instead of the default one.
///
/// ### `set_formatter(&self, new_fmt: Arc<dyn LogFormatter>)`
/// Replaces the formatter of the current logger instance.
///
/// ### `file(path: impl Into<PathBuf>) -> Result<Self, io::Error>`
/// Enables file logging. Creates (or appends to) a file at the given path.
/// Returns a `Result<Self>` for error handling.
///
/// ### `level(&self) -> Level`
/// Returns the current minimum log level for this logger.
///
/// ### `no_stdout(self) -> Self`
/// Disables logging to stdout entirely (useful for file-only logging).
///
/// ### `with_stdout(self) -> Self`
/// Re-enables logging to stdout if previously disabled.
///
/// ### `get_records(&self) -> Vec<LogRecord>`
/// Returns a **cloned** vector of all log records stored in memory.
/// This can be expensive for large logs—use carefully.
///
/// ### `clear_records(&self)`
/// Clears all stored log records from memory.
///
///
/// ## Examples
/// ----------------------------------------------------------------
///
/// ```
/// use luhlog::{Logger, Log, Level, DefaultFormatter, LogBuilder};
/// use std::sync::Arc;
/// fn main() {
/// // create a logger with a default formatter
/// let logger = Logger::new(Level::Info);
/// // log something
/// logger.log(
/// LogBuilder::new("Hello world from the logger!")
/// .level(Level::Info)
/// .build()
/// );
/// // switch to a compact formatter mid-run
/// use luhlog::CompactFormatter;
/// logger.set_formatter(Arc::new(CompactFormatter));
/// logger.log(
/// LogBuilder::new("Compact mode enabled!")
/// .level(Level::Warn)
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
///
/// ```
/// use luhlog::{Logger, Log, Level, LogBuilder};
/// fn main() {
/// // logging to a file instead of stdout
/// let logger = Logger::new(Level::Info)
/// .file("example.log")
/// .expect("failed to open file");
/// logger.log(
/// LogBuilder::new("This will go into the log file!")
/// .level(Level::Info)
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
/// `luhlog::GlobalLogger` provides a simple thread-safe global logging
/// container. It allows you to store and retrieve a single shared
/// logger instance across your entire program.
///
/// This is mainly used behind the global functions like
/// [`luhlog::set_logger`], [`luhlog::get_logger`], and
/// [`luhlog::clear_logger`], but you can also use it directly if you
/// need a custom global logging instance.
///
/// ## Provided Methods
/// ----------------------------------------------------------------
///
/// ### `new() -> Self`
/// Creates a new, empty `GlobalLogger` with no active logger set.
///
/// ### `set(&self, logger: Arc<dyn Log>)`
/// Stores a logger inside the global container.
/// Overwrites any existing logger if one is already set.
///
/// ### `clear(&self)`
/// Removes the currently stored logger, returning the global state
/// to `None`.
///
/// ### `get(&self) -> Arc<dyn Log>`
/// Retrieves the currently stored logger instance.
/// If no logger has been set, it will automatically return a new
/// default [`Logger`] with `Level::Info`.
///
///
/// ## Examples
/// ----------------------------------------------------------------
///
/// ```
/// use luhlog::{GlobalLogger, Logger, Level, Log, LogBuilder};
/// use std::sync::Arc;
/// static GLOBAL: GlobalLogger = GlobalLogger::new();
/// fn main() {
/// // create and assign a global logger
/// let logger = Arc::new(Logger::new(Level::Trace));
/// GLOBAL.set(logger);
/// // use the global logger directly
/// GLOBAL.get().log(
/// LogBuilder::new("Hello from the global logger!")
/// .level(Level::Info)
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
///
/// ```
/// use luhlog::{GlobalLogger, Logger, Level, Log};
/// use std::sync::Arc;
/// static LOG: GlobalLogger = GlobalLogger::new();
/// fn main() {
/// // clear global logger to reset state
/// LOG.clear();
/// // retrieve will still return a default logger
/// let default_logger = LOG.get();
/// default_logger.log(
/// luhlog::LogBuilder::new("Using the default global logger")
/// .level(Level::Warn)
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
/// Global logger instance for luhlog.
///
/// This is a [`GlobalLogger`] that stores a single, shared [`Log`]
/// used by all logging macros like [`info!`], [`warn!`], and [`error!`].
///
/// By default, it’s empty and falls back to a basic `Logger` with
/// `Level::Info` until explicitly set using [`set_logger`].
static LOGGER: GlobalLogger = new;
/// Sets the global logger for this library instance.
///
/// Once set, all global logging macros (e.g. [`info!`], [`debug!`])
/// will write through this logger.
///
/// ## Example
/// ----------------------------------------------------------------
/// ```
/// use luhlog::{set_logger, Logger, Level};
/// use std::sync::Arc;
/// fn main() {
/// set_logger(Arc::new(Logger::new(Level::Trace)));
/// }
/// ```
/// ----------------------------------------------------------------
/// Clears the currently active global logger.
///
/// After calling this, any call to [`get_logger`] will automatically
/// return a new default [`Logger`] instance instead.
/// Retrieves the active global logger instance.
///
/// If no logger has been set with [`set_logger`], a default
/// [`Logger`] with `Level::Info` will be returned automatically.
///
/// ## Example
/// ----------------------------------------------------------------
/// ```
/// use luhlog::{get_logger, LogBuilder, Level, Log};
/// fn main() {
/// let logger = get_logger();
/// logger.log(
/// LogBuilder::new("using the global logger directly")
/// .level(Level::Info)
/// .build()
/// );
/// }
/// ```
/// ----------------------------------------------------------------
/// Flushes the global logger’s internal records.
///
/// This simply calls [`Log::flush()`] on the current global logger.
/// If no logger is set, it flushes the default fallback logger.