@@ -50,6 +50,36 @@ const (
50
50
51
51
// The outLevelFormat is the default level format for the output.
52
52
outLevelFormat = "%s"
53
+
54
+ // There is a difference between text-style message formatting
55
+ // and JSON-style formatting.
56
+ //
57
+ // So, in text style: print simply prints all elements, pasting
58
+ // them together without separators; println - adds spaces between
59
+ // elements and adds \n at the end of the formed line; and printf -
60
+ // outputs according to a special format template.
61
+ //
62
+ // At the same time, for JSON: print should insert all elements
63
+ // without delimiters (as in text style), but all JSON blocks* don't
64
+ // contain \n at the end; but println inserts all message elements
65
+ // with a space added, and does not add \n to the end of the message,
66
+ // instead, each new JSON block* is printed on a new line (\n is appended
67
+ // to the JSON block); printf - \n is appended to the JSON block* and the
68
+ // message format matches the specified user format.
69
+ //
70
+ // So, to determine if a custom format pattern is specified, we use two
71
+ // constants formatStr and formatStrLn, which help determine the type of
72
+ // function (print, println, or printf) that renders the message, and the
73
+ // formatting methods for text or JSON style.
74
+ //
75
+ // * JSON block is the final text representation of the logger in
76
+ // JSON format written as text.
77
+
78
+ // The formatStr is the system format string for print-type functions.
79
+ formatStr = "format: string-without-a-newline-character"
80
+
81
+ // The formatStrLn is the system format string for println-type functions.
82
+ formatStrLn = "format: string-with-a-newline-character"
53
83
)
54
84
55
85
var (
85
115
LevelFormat : outLevelFormat ,
86
116
}
87
117
88
- // Default ...
118
+ // Default is output that processes all types of levels
119
+ // and outputs the result to stdout.
89
120
Default = Output {
90
121
Name : "default" ,
91
122
Writer : os .Stdout ,
@@ -100,7 +131,8 @@ var (
100
131
LevelFormat : outLevelFormat ,
101
132
}
102
133
103
- // The exit is ...
134
+ // The exit causes the current program to exit with the given status code.
135
+ // Redefined this function to be able to test *fatal* methods.
104
136
exit = os .Exit
105
137
)
106
138
@@ -152,7 +184,7 @@ type Output struct {
152
184
// (or don't change, for edit mode).
153
185
//
154
186
// We can also use the github.com/goloop/trit package and
155
- // the value trit.True or trit.False.
187
+ // the trit.True or trit.False value .
156
188
WithPrefix trit.Trit
157
189
158
190
// WithColor is the flag that determines whether to use color for the
@@ -168,7 +200,7 @@ type Output struct {
168
200
// (or don't change, for edit mode).
169
201
//
170
202
// We can also use the github.com/goloop/trit package and
171
- // the value trit.True or trit.False.
203
+ // the trit.True or trit.False value .
172
204
//
173
205
// The color scheme works only for UNIX-like systems.
174
206
// The color scheme works for flat format only (i.e. display of log
@@ -186,7 +218,7 @@ type Output struct {
186
218
// (or don't change, for edit mode).
187
219
//
188
220
// We can also use the github.com/goloop/trit package and
189
- // the value trit.True or trit.False.
221
+ // the trit.True or trit.False value .
190
222
Enabled trit.Trit
191
223
192
224
// TextStyle is the flag that determines whether to use text style for
@@ -201,7 +233,7 @@ type Output struct {
201
233
// (or don't change, for edit mode).
202
234
//
203
235
// We can also use the github.com/goloop/trit package and
204
- // the value trit.True or trit.False.
236
+ // the trit.True or trit.False value .
205
237
TextStyle trit.Trit
206
238
207
239
// TimestampFormat is the format of the timestamp in the log-message.
@@ -608,7 +640,7 @@ func (logger *Logger) echo(w io.Writer, l level.Level, f string, a ...any) {
608
640
// for its operands and writes to w. Spaces are added between operands
609
641
// when neither is a string.
610
642
func (logger * Logger ) Fpanic (w io.Writer , a ... any ) {
611
- logger .echo (w , level .Panic , "%s" , a ... )
643
+ logger .echo (w , level .Panic , formatStr , a ... )
612
644
panic (fmt .Sprint (a ... ))
613
645
}
614
646
@@ -623,15 +655,15 @@ func (logger *Logger) Fpanicf(w io.Writer, format string, a ...any) {
623
655
// for its operands and writes to w. Spaces are always added between
624
656
// operands and a newline is appended.
625
657
func (logger * Logger ) Fpanicln (w io.Writer , a ... any ) {
626
- logger .echo (w , level .Panic , "%s \n " , a ... )
658
+ logger .echo (w , level .Panic , formatStrLn , a ... )
627
659
panic (fmt .Sprintln (a ... ))
628
660
}
629
661
630
662
// Panic creates message with Panic level, using the default formats
631
663
// for its operands and writes to log.Writer. Spaces are added between
632
664
// operands when neither is a string.
633
665
func (logger * Logger ) Panic (a ... any ) {
634
- logger .echo (nil , level .Panic , "%s" , a ... )
666
+ logger .echo (nil , level .Panic , formatStr , a ... )
635
667
panic (fmt .Sprint (a ... ))
636
668
}
637
669
@@ -646,15 +678,15 @@ func (logger *Logger) Panicf(format string, a ...any) {
646
678
// for its operands and writes to log.Writer. Spaces are always added
647
679
// between operands and a newline is appended.
648
680
func (logger * Logger ) Panicln (a ... any ) (int , error ) {
649
- logger .echo (nil , level .Panic , "%s \n " , a ... )
681
+ logger .echo (nil , level .Panic , formatStrLn , a ... )
650
682
panic (fmt .Sprintln (a ... ))
651
683
}
652
684
653
685
// Ffatal creates message with Fatal level, using the default formats
654
686
// for its operands and writes to w. Spaces are added between operands
655
687
// when neither is a string.
656
688
func (logger * Logger ) Ffatal (w io.Writer , a ... any ) {
657
- logger .echo (w , level .Fatal , "%s" , a ... )
689
+ logger .echo (w , level .Fatal , formatStr , a ... )
658
690
exit (logger .fatalStatusCode )
659
691
}
660
692
@@ -669,15 +701,15 @@ func (logger *Logger) Ffatalf(w io.Writer, format string, a ...any) {
669
701
// for its operands and writes to w. Spaces are always added between
670
702
// operands and a newline is appended.
671
703
func (logger * Logger ) Ffatalln (w io.Writer , a ... any ) {
672
- logger .echo (w , level .Fatal , "%s \n " , a ... )
704
+ logger .echo (w , level .Fatal , formatStrLn , a ... )
673
705
exit (logger .fatalStatusCode )
674
706
}
675
707
676
708
// Fatal creates message with Fatal level, using the default formats
677
709
// for its operands and writes to log.Writer. Spaces are added between
678
710
// operands when neither is a string.
679
711
func (logger * Logger ) Fatal (a ... any ) {
680
- logger .echo (nil , level .Fatal , "%s" , a ... )
712
+ logger .echo (nil , level .Fatal , formatStr , a ... )
681
713
exit (logger .fatalStatusCode )
682
714
}
683
715
@@ -692,15 +724,15 @@ func (logger *Logger) Fatalf(format string, a ...any) {
692
724
// for its operands and writes to log.Writer. Spaces are always added
693
725
// between operands and a newline is appended.
694
726
func (logger * Logger ) Fatalln (a ... any ) {
695
- logger .echo (nil , level .Fatal , "%s \n " , a ... )
727
+ logger .echo (nil , level .Fatal , formatStrLn , a ... )
696
728
exit (logger .fatalStatusCode )
697
729
}
698
730
699
731
// Ferror creates message with Error level, using the default formats
700
732
// for its operands and writes to w. Spaces are added between operands
701
733
// when neither is a string.
702
734
func (logger * Logger ) Ferror (w io.Writer , a ... any ) {
703
- logger .echo (w , level .Error , "%s" , a ... )
735
+ logger .echo (w , level .Error , formatStr , a ... )
704
736
}
705
737
706
738
// Ferrorf creates message with Error level, according to a format
@@ -713,14 +745,14 @@ func (logger *Logger) Ferrorf(w io.Writer, f string, a ...any) {
713
745
// for its operands and writes to w. Spaces are always added between
714
746
// operands and a newline is appended.
715
747
func (logger * Logger ) Ferrorln (w io.Writer , a ... any ) {
716
- logger .echo (w , level .Error , "%s \n " , a ... )
748
+ logger .echo (w , level .Error , formatStrLn , a ... )
717
749
}
718
750
719
751
// Error creates message with Error level, using the default formats
720
752
// for its operands and writes to log.Writer. Spaces are added between
721
753
// operands when neither is a string.
722
754
func (logger * Logger ) Error (a ... any ) {
723
- logger .echo (nil , level .Error , "%s" , a ... )
755
+ logger .echo (nil , level .Error , formatStr , a ... )
724
756
}
725
757
726
758
// Errorf creates message with Error level, according to a format specifier
@@ -733,14 +765,14 @@ func (logger *Logger) Errorf(f string, a ...any) {
733
765
// for its operands and writes to log.Writer. Spaces are always added
734
766
// between operands and a newline is appended.
735
767
func (logger * Logger ) Errorln (a ... any ) {
736
- logger .echo (nil , level .Error , "%s \n " , a ... )
768
+ logger .echo (nil , level .Error , formatStrLn , a ... )
737
769
}
738
770
739
771
// Fwarn creates message with Warn level, using the default formats
740
772
// for its operands and writes to w. Spaces are added between operands
741
773
// when neither is a string.
742
774
func (logger * Logger ) Fwarn (w io.Writer , a ... any ) {
743
- logger .echo (w , level .Warn , "%s" , a ... )
775
+ logger .echo (w , level .Warn , formatStr , a ... )
744
776
}
745
777
746
778
// Fwarnf creates message with Warn level, according to a format
@@ -753,14 +785,14 @@ func (logger *Logger) Fwarnf(w io.Writer, format string, a ...any) {
753
785
// for its operands and writes to w. Spaces are always added between
754
786
// operands and a newline is appended.
755
787
func (logger * Logger ) Fwarnln (w io.Writer , a ... any ) {
756
- logger .echo (w , level .Warn , "%s \n " , a ... )
788
+ logger .echo (w , level .Warn , formatStrLn , a ... )
757
789
}
758
790
759
791
// Warn creates message with Warn level, using the default formats
760
792
// for its operands and writes to log.Writer. Spaces are added between
761
793
// operands when neither is a string.
762
794
func (logger * Logger ) Warn (a ... any ) {
763
- logger .echo (nil , level .Warn , "%s" , a ... )
795
+ logger .echo (nil , level .Warn , formatStr , a ... )
764
796
}
765
797
766
798
// Warnf creates message with Warn level, according to a format specifier
@@ -773,14 +805,14 @@ func (logger *Logger) Warnf(format string, a ...any) {
773
805
// for its operands and writes to log.Writer. Spaces are always added
774
806
// between operands and a newline is appended.
775
807
func (logger * Logger ) Warnln (a ... any ) {
776
- logger .echo (nil , level .Warn , "%s \n " , a ... )
808
+ logger .echo (nil , level .Warn , formatStrLn , a ... )
777
809
}
778
810
779
811
// Finfo creates message with Info level, using the default formats
780
812
// for its operands and writes to w. Spaces are added between operands
781
813
// when neither is a string.
782
814
func (logger * Logger ) Finfo (w io.Writer , a ... any ) {
783
- logger .echo (w , level .Info , "%s" , a ... )
815
+ logger .echo (w , level .Info , formatStr , a ... )
784
816
}
785
817
786
818
// Finfof creates message with Info level, according to a format
@@ -793,14 +825,14 @@ func (logger *Logger) Finfof(w io.Writer, format string, a ...any) {
793
825
// for its operands and writes to w. Spaces are always added between
794
826
// operands and a newline is appended.
795
827
func (logger * Logger ) Finfoln (w io.Writer , a ... any ) {
796
- logger .echo (w , level .Info , "%s \n " , a ... )
828
+ logger .echo (w , level .Info , formatStrLn , a ... )
797
829
}
798
830
799
831
// Info creates message with Info level, using the default formats
800
832
// for its operands and writes to log.Writer. Spaces are added between
801
833
// operands when neither is a string.
802
834
func (logger * Logger ) Info (a ... any ) {
803
- logger .echo (nil , level .Info , "%s" , a ... )
835
+ logger .echo (nil , level .Info , formatStr , a ... )
804
836
}
805
837
806
838
// Infof creates message with Info level, according to a format specifier
@@ -813,14 +845,14 @@ func (logger *Logger) Infof(format string, a ...any) {
813
845
// for its operands and writes to log.Writer. Spaces are always added
814
846
// between operands and a newline is appended.
815
847
func (logger * Logger ) Infoln (a ... any ) {
816
- logger .echo (nil , level .Info , "%s \n " , a ... )
848
+ logger .echo (nil , level .Info , formatStrLn , a ... )
817
849
}
818
850
819
851
// Fdebug creates message with Debug level, using the default formats
820
852
// for its operands and writes to w. Spaces are added between operands
821
853
// when neither is a string.
822
854
func (logger * Logger ) Fdebug (w io.Writer , a ... any ) {
823
- logger .echo (w , level .Debug , "%s" , a ... )
855
+ logger .echo (w , level .Debug , formatStr , a ... )
824
856
}
825
857
826
858
// Fdebugf creates message with Debug level, according to a format
@@ -833,14 +865,14 @@ func (logger *Logger) Fdebugf(w io.Writer, format string, a ...any) {
833
865
// for its operands and writes to w. Spaces are always added between
834
866
// operands and a newline is appended.
835
867
func (logger * Logger ) Fdebugln (w io.Writer , a ... any ) {
836
- logger .echo (w , level .Debug , "%s \n " , a ... )
868
+ logger .echo (w , level .Debug , formatStrLn , a ... )
837
869
}
838
870
839
871
// Debug creates message with Debug level, using the default formats
840
872
// for its operands and writes to log.Writer. Spaces are added between
841
873
// operands when neither is a string.
842
874
func (logger * Logger ) Debug (a ... any ) {
843
- logger .echo (nil , level .Debug , "%s" , a ... )
875
+ logger .echo (nil , level .Debug , formatStr , a ... )
844
876
}
845
877
846
878
// Debugf creates message with Debug level, according to a format specifier
@@ -854,14 +886,14 @@ func (logger *Logger) Debugf(format string, a ...any) {
854
886
// for its operands and writes to log.Writer. Spaces are always added
855
887
// between operands and a newline is appended.
856
888
func (logger * Logger ) Debugln (a ... any ) {
857
- logger .echo (nil , level .Debug , "%s \n " , a ... )
889
+ logger .echo (nil , level .Debug , formatStrLn , a ... )
858
890
}
859
891
860
892
// Ftrace creates message with Trace level, using the default formats
861
893
// for its operands and writes to w. Spaces are added between operands
862
894
// when neither is a string.
863
895
func (logger * Logger ) Ftrace (w io.Writer , a ... any ) {
864
- logger .echo (w , level .Trace , "%s" , a ... )
896
+ logger .echo (w , level .Trace , formatStr , a ... )
865
897
}
866
898
867
899
// Ftracef creates message with Trace level, according to a format
@@ -874,14 +906,14 @@ func (logger *Logger) Ftracef(w io.Writer, format string, a ...any) {
874
906
// for its operands and writes to w. Spaces are always added between
875
907
// operands and a newline is appended.
876
908
func (logger * Logger ) Ftraceln (w io.Writer , a ... any ) {
877
- logger .echo (w , level .Trace , "%s \n " , a ... )
909
+ logger .echo (w , level .Trace , formatStrLn , a ... )
878
910
}
879
911
880
912
// Trace creates message with Trace level, using the default formats
881
913
// for its operands and writes to log.Writer. Spaces are added between
882
914
// operands when neither is a string.
883
915
func (logger * Logger ) Trace (a ... any ) {
884
- logger .echo (nil , level .Trace , "%s" , a ... )
916
+ logger .echo (nil , level .Trace , formatStr , a ... )
885
917
}
886
918
887
919
// Tracef creates message with Trace level, according to a format specifier
@@ -894,5 +926,5 @@ func (logger *Logger) Tracef(format string, a ...any) {
894
926
// for its operands and writes to log.Writer. Spaces are always added
895
927
// between operands and a newline is appended.
896
928
func (logger * Logger ) Traceln (a ... any ) {
897
- logger .echo (nil , level .Trace , "%s \n " , a ... )
929
+ logger .echo (nil , level .Trace , formatStrLn , a ... )
898
930
}
0 commit comments