Skip to content

Commit c97a22f

Browse files
committed
Production version 1.0.0
1 parent 63d238c commit c97a22f

File tree

4 files changed

+247
-50
lines changed

4 files changed

+247
-50
lines changed

README.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ import (
4747
"github.com/goloop/log"
4848
"github.com/goloop/log/layout"
4949
"github.com/goloop/log/level"
50+
51+
// It is not necessary to import this module, the three-valued logic
52+
// constants can be represented using integers where: -1 is False,
53+
// 0 is Unknown, and 1 is True.
54+
"github.com/goloop/trit"
5055
)
5156

5257
func main() {
@@ -68,7 +73,7 @@ func main() {
6873
// Set the outputs of the log to our file.
6974
// We can set many different outputs to record
7075
// individual errors, debug or mixed data.
71-
log.SetOutputs(
76+
err = log.SetOutputs(
7277
log.Output{
7378
Name: "stdout",
7479
Writer: os.Stdout,
@@ -77,15 +82,24 @@ func main() {
7782
WithColor: 1, // or trit.True, see github.com/goloop/trit
7883
},
7984
log.Output{
80-
Name: "file-errors",
81-
Writer: file,
82-
Levels: level.Warn | level.Error, // only errors and warnings
83-
Layouts: layout.Default,
85+
Name: "errorsJSONFile",
86+
Writer: file,
87+
Levels: level.Warn | level.Error, // only errors and warnings
88+
Layouts: layout.Default,
89+
TextStyle: trit.False, // or just -1
8490
},
8591
)
8692

87-
// Now, any log messages will be written to the file.
93+
if err != nil {
94+
log.Fatal(err)
95+
}
96+
97+
// A message will be output to a file and to the console.
98+
// * stdout and errorsJSONFile has Error level.
8899
log.Errorln("This is a test log message with ERROR.")
100+
101+
// A message will be output to the console only.
102+
// * stdout has Debug level, but errorsJSONFile has not.
89103
log.Debugln("This is a test log message with DEBUG.")
90104
}
91105
```

logger.go

+66-34
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,36 @@ const (
5050

5151
// The outLevelFormat is the default level format for the output.
5252
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"
5383
)
5484

5585
var (
@@ -85,7 +115,8 @@ var (
85115
LevelFormat: outLevelFormat,
86116
}
87117

88-
// Default ...
118+
// Default is output that processes all types of levels
119+
// and outputs the result to stdout.
89120
Default = Output{
90121
Name: "default",
91122
Writer: os.Stdout,
@@ -100,7 +131,8 @@ var (
100131
LevelFormat: outLevelFormat,
101132
}
102133

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.
104136
exit = os.Exit
105137
)
106138

@@ -152,7 +184,7 @@ type Output struct {
152184
// (or don't change, for edit mode).
153185
//
154186
// 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.
156188
WithPrefix trit.Trit
157189

158190
// WithColor is the flag that determines whether to use color for the
@@ -168,7 +200,7 @@ type Output struct {
168200
// (or don't change, for edit mode).
169201
//
170202
// 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.
172204
//
173205
// The color scheme works only for UNIX-like systems.
174206
// The color scheme works for flat format only (i.e. display of log
@@ -186,7 +218,7 @@ type Output struct {
186218
// (or don't change, for edit mode).
187219
//
188220
// 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.
190222
Enabled trit.Trit
191223

192224
// TextStyle is the flag that determines whether to use text style for
@@ -201,7 +233,7 @@ type Output struct {
201233
// (or don't change, for edit mode).
202234
//
203235
// 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.
205237
TextStyle trit.Trit
206238

207239
// 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) {
608640
// for its operands and writes to w. Spaces are added between operands
609641
// when neither is a string.
610642
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...)
612644
panic(fmt.Sprint(a...))
613645
}
614646

@@ -623,15 +655,15 @@ func (logger *Logger) Fpanicf(w io.Writer, format string, a ...any) {
623655
// for its operands and writes to w. Spaces are always added between
624656
// operands and a newline is appended.
625657
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...)
627659
panic(fmt.Sprintln(a...))
628660
}
629661

630662
// Panic creates message with Panic level, using the default formats
631663
// for its operands and writes to log.Writer. Spaces are added between
632664
// operands when neither is a string.
633665
func (logger *Logger) Panic(a ...any) {
634-
logger.echo(nil, level.Panic, "%s", a...)
666+
logger.echo(nil, level.Panic, formatStr, a...)
635667
panic(fmt.Sprint(a...))
636668
}
637669

@@ -646,15 +678,15 @@ func (logger *Logger) Panicf(format string, a ...any) {
646678
// for its operands and writes to log.Writer. Spaces are always added
647679
// between operands and a newline is appended.
648680
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...)
650682
panic(fmt.Sprintln(a...))
651683
}
652684

653685
// Ffatal creates message with Fatal level, using the default formats
654686
// for its operands and writes to w. Spaces are added between operands
655687
// when neither is a string.
656688
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...)
658690
exit(logger.fatalStatusCode)
659691
}
660692

@@ -669,15 +701,15 @@ func (logger *Logger) Ffatalf(w io.Writer, format string, a ...any) {
669701
// for its operands and writes to w. Spaces are always added between
670702
// operands and a newline is appended.
671703
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...)
673705
exit(logger.fatalStatusCode)
674706
}
675707

676708
// Fatal creates message with Fatal level, using the default formats
677709
// for its operands and writes to log.Writer. Spaces are added between
678710
// operands when neither is a string.
679711
func (logger *Logger) Fatal(a ...any) {
680-
logger.echo(nil, level.Fatal, "%s", a...)
712+
logger.echo(nil, level.Fatal, formatStr, a...)
681713
exit(logger.fatalStatusCode)
682714
}
683715

@@ -692,15 +724,15 @@ func (logger *Logger) Fatalf(format string, a ...any) {
692724
// for its operands and writes to log.Writer. Spaces are always added
693725
// between operands and a newline is appended.
694726
func (logger *Logger) Fatalln(a ...any) {
695-
logger.echo(nil, level.Fatal, "%s\n", a...)
727+
logger.echo(nil, level.Fatal, formatStrLn, a...)
696728
exit(logger.fatalStatusCode)
697729
}
698730

699731
// Ferror creates message with Error level, using the default formats
700732
// for its operands and writes to w. Spaces are added between operands
701733
// when neither is a string.
702734
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...)
704736
}
705737

706738
// 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) {
713745
// for its operands and writes to w. Spaces are always added between
714746
// operands and a newline is appended.
715747
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...)
717749
}
718750

719751
// Error creates message with Error level, using the default formats
720752
// for its operands and writes to log.Writer. Spaces are added between
721753
// operands when neither is a string.
722754
func (logger *Logger) Error(a ...any) {
723-
logger.echo(nil, level.Error, "%s", a...)
755+
logger.echo(nil, level.Error, formatStr, a...)
724756
}
725757

726758
// Errorf creates message with Error level, according to a format specifier
@@ -733,14 +765,14 @@ func (logger *Logger) Errorf(f string, a ...any) {
733765
// for its operands and writes to log.Writer. Spaces are always added
734766
// between operands and a newline is appended.
735767
func (logger *Logger) Errorln(a ...any) {
736-
logger.echo(nil, level.Error, "%s\n", a...)
768+
logger.echo(nil, level.Error, formatStrLn, a...)
737769
}
738770

739771
// Fwarn creates message with Warn level, using the default formats
740772
// for its operands and writes to w. Spaces are added between operands
741773
// when neither is a string.
742774
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...)
744776
}
745777

746778
// 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) {
753785
// for its operands and writes to w. Spaces are always added between
754786
// operands and a newline is appended.
755787
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...)
757789
}
758790

759791
// Warn creates message with Warn level, using the default formats
760792
// for its operands and writes to log.Writer. Spaces are added between
761793
// operands when neither is a string.
762794
func (logger *Logger) Warn(a ...any) {
763-
logger.echo(nil, level.Warn, "%s", a...)
795+
logger.echo(nil, level.Warn, formatStr, a...)
764796
}
765797

766798
// Warnf creates message with Warn level, according to a format specifier
@@ -773,14 +805,14 @@ func (logger *Logger) Warnf(format string, a ...any) {
773805
// for its operands and writes to log.Writer. Spaces are always added
774806
// between operands and a newline is appended.
775807
func (logger *Logger) Warnln(a ...any) {
776-
logger.echo(nil, level.Warn, "%s\n", a...)
808+
logger.echo(nil, level.Warn, formatStrLn, a...)
777809
}
778810

779811
// Finfo creates message with Info level, using the default formats
780812
// for its operands and writes to w. Spaces are added between operands
781813
// when neither is a string.
782814
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...)
784816
}
785817

786818
// 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) {
793825
// for its operands and writes to w. Spaces are always added between
794826
// operands and a newline is appended.
795827
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...)
797829
}
798830

799831
// Info creates message with Info level, using the default formats
800832
// for its operands and writes to log.Writer. Spaces are added between
801833
// operands when neither is a string.
802834
func (logger *Logger) Info(a ...any) {
803-
logger.echo(nil, level.Info, "%s", a...)
835+
logger.echo(nil, level.Info, formatStr, a...)
804836
}
805837

806838
// Infof creates message with Info level, according to a format specifier
@@ -813,14 +845,14 @@ func (logger *Logger) Infof(format string, a ...any) {
813845
// for its operands and writes to log.Writer. Spaces are always added
814846
// between operands and a newline is appended.
815847
func (logger *Logger) Infoln(a ...any) {
816-
logger.echo(nil, level.Info, "%s\n", a...)
848+
logger.echo(nil, level.Info, formatStrLn, a...)
817849
}
818850

819851
// Fdebug creates message with Debug level, using the default formats
820852
// for its operands and writes to w. Spaces are added between operands
821853
// when neither is a string.
822854
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...)
824856
}
825857

826858
// 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) {
833865
// for its operands and writes to w. Spaces are always added between
834866
// operands and a newline is appended.
835867
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...)
837869
}
838870

839871
// Debug creates message with Debug level, using the default formats
840872
// for its operands and writes to log.Writer. Spaces are added between
841873
// operands when neither is a string.
842874
func (logger *Logger) Debug(a ...any) {
843-
logger.echo(nil, level.Debug, "%s", a...)
875+
logger.echo(nil, level.Debug, formatStr, a...)
844876
}
845877

846878
// Debugf creates message with Debug level, according to a format specifier
@@ -854,14 +886,14 @@ func (logger *Logger) Debugf(format string, a ...any) {
854886
// for its operands and writes to log.Writer. Spaces are always added
855887
// between operands and a newline is appended.
856888
func (logger *Logger) Debugln(a ...any) {
857-
logger.echo(nil, level.Debug, "%s\n", a...)
889+
logger.echo(nil, level.Debug, formatStrLn, a...)
858890
}
859891

860892
// Ftrace creates message with Trace level, using the default formats
861893
// for its operands and writes to w. Spaces are added between operands
862894
// when neither is a string.
863895
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...)
865897
}
866898

867899
// 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) {
874906
// for its operands and writes to w. Spaces are always added between
875907
// operands and a newline is appended.
876908
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...)
878910
}
879911

880912
// Trace creates message with Trace level, using the default formats
881913
// for its operands and writes to log.Writer. Spaces are added between
882914
// operands when neither is a string.
883915
func (logger *Logger) Trace(a ...any) {
884-
logger.echo(nil, level.Trace, "%s", a...)
916+
logger.echo(nil, level.Trace, formatStr, a...)
885917
}
886918

887919
// Tracef creates message with Trace level, according to a format specifier
@@ -894,5 +926,5 @@ func (logger *Logger) Tracef(format string, a ...any) {
894926
// for its operands and writes to log.Writer. Spaces are always added
895927
// between operands and a newline is appended.
896928
func (logger *Logger) Traceln(a ...any) {
897-
logger.echo(nil, level.Trace, "%s\n", a...)
929+
logger.echo(nil, level.Trace, formatStrLn, a...)
898930
}

0 commit comments

Comments
 (0)