Skip to content

Commit fc60626

Browse files
committed
Softened Outptut's name validation
1 parent 031eb8b commit fc60626

File tree

6 files changed

+66
-49
lines changed

6 files changed

+66
-49
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ require (
77
github.com/goloop/trit v1.7.1
88
)
99

10-
require github.com/goloop/is v1.3.0
10+
require github.com/goloop/is v1.4.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/goloop/is v1.2.0 h1:dPsFRmcfrcz+DktkICGsq9mS+5mNC94xNv3LHXrZ8DQ=
1010
github.com/goloop/is v1.2.0/go.mod h1:wrFW9DTFePYdWXH1Y8ZGUWOi8N8XCZ3vMsV9xald618=
1111
github.com/goloop/is v1.3.0 h1:4Kw2r3PZNXEmI17HsfTZP7NBxZk+S2hxRXx7xBDEBXY=
1212
github.com/goloop/is v1.3.0/go.mod h1:wrFW9DTFePYdWXH1Y8ZGUWOi8N8XCZ3vMsV9xald618=
13+
github.com/goloop/is v1.4.0 h1:Ha9kvgku6AScWcNx/J9gq3iiD+XAqIRum09JgtRv2Zk=
14+
github.com/goloop/is v1.4.0/go.mod h1:wrFW9DTFePYdWXH1Y8ZGUWOi8N8XCZ3vMsV9xald618=
1315
github.com/goloop/trit v1.2.0 h1:BrJg7mfI3AHli40FA3Q77MH3MY/oSbbdgt58bwrmkyc=
1416
github.com/goloop/trit v1.2.0/go.mod h1:DVMcZPI0c2vjgl/F7SXsAE3AsDDEdVnAofRhnzqFsi0=
1517
github.com/goloop/trit v1.3.0 h1:SnYIt1qfiW+mT9pfd+FGlrWpbvEkGAywoflKdFFJUvs=

log_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,22 @@ func TestSetOutputs(t *testing.T) {
187187
Writer: os.Stdout,
188188
},
189189
},
190-
names: []string{"test"},
190+
names: []string{},
191191
hasErr: true,
192192
},
193193
{
194-
name: "Wrong output name, but is system output",
194+
name: "CSS-style output name",
195+
in: []Output{
196+
{
197+
Name: "file-name",
198+
Writer: os.Stdout,
199+
},
200+
},
201+
names: []string{"file-name"},
202+
hasErr: false,
203+
},
204+
{
205+
name: "Wrong output name, but it is system output",
195206
in: []Output{
196207
{
197208
Name: "*",

logger.go

+41-37
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,21 @@ const (
6767
// to the JSON block); printf - \n is appended to the JSON block* and the
6868
// message format matches the specified user format.
6969
//
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.
70+
// So, to determine if a custom format pattern is specified, we use
71+
// two constants formatPrint and formatPrintln, which help determine
72+
// the type of function (print, println, or printf) that renders
73+
// the message, and the formatting methods for text or JSON style.
7474
//
7575
// * JSON block is the final text representation of the logger in
7676
// JSON format written as text.
7777

78-
// The formatStr is the system format string for print-type functions.
79-
formatStr = "format: string-without-a-newline-character"
78+
// The formatPrint is the system format string
79+
// for print-type functions.
80+
formatPrint = "{$ string-without-a-newline-character $}"
8081

81-
// The formatStrLn is the system format string for println-type functions.
82-
formatStrLn = "format: string-with-a-newline-character"
82+
// The formatPrintln is the system format string
83+
// for println-type functions.
84+
formatPrintln = "{$ string-with-a-newline-character $}"
8385
)
8486

8587
var (
@@ -147,6 +149,8 @@ type Output struct {
147149
// The name must sound like a variable name in most programming languages:
148150
// it must have special characters, not start with a number, etc.
149151
// But the name can be a reserved word like return, for, if ... any.
152+
// The name can also be expressed as a selector name in CSS,
153+
// for example "some-log-file" (without the leading . or # symbols).
150154
Name string
151155

152156
// Writer is the point where the login data will be output,
@@ -440,7 +444,7 @@ func (logger *Logger) SetOutputs(outputs ...Output) error {
440444
// The name must be specified.
441445
if g.IsEmpty(o.Name) {
442446
return fmt.Errorf("the %d output has empty name", i)
443-
} else if !o.isSystem && !is.Var(o.Name) {
447+
} else if !o.isSystem && !is.SelectorName(o.Name, true) {
444448
return fmt.Errorf("the %d output has incorrect name '%s'",
445449
i, o.Name)
446450
}
@@ -640,7 +644,7 @@ func (logger *Logger) echo(w io.Writer, l level.Level, f string, a ...any) {
640644
// for its operands and writes to w. Spaces are added between operands
641645
// when neither is a string.
642646
func (logger *Logger) Fpanic(w io.Writer, a ...any) {
643-
logger.echo(w, level.Panic, formatStr, a...)
647+
logger.echo(w, level.Panic, formatPrint, a...)
644648
panic(fmt.Sprint(a...))
645649
}
646650

@@ -655,15 +659,15 @@ func (logger *Logger) Fpanicf(w io.Writer, format string, a ...any) {
655659
// for its operands and writes to w. Spaces are always added between
656660
// operands and a newline is appended.
657661
func (logger *Logger) Fpanicln(w io.Writer, a ...any) {
658-
logger.echo(w, level.Panic, formatStrLn, a...)
662+
logger.echo(w, level.Panic, formatPrintln, a...)
659663
panic(fmt.Sprintln(a...))
660664
}
661665

662666
// Panic creates message with Panic level, using the default formats
663667
// for its operands and writes to log.Writer. Spaces are added between
664668
// operands when neither is a string.
665669
func (logger *Logger) Panic(a ...any) {
666-
logger.echo(nil, level.Panic, formatStr, a...)
670+
logger.echo(nil, level.Panic, formatPrint, a...)
667671
panic(fmt.Sprint(a...))
668672
}
669673

@@ -678,15 +682,15 @@ func (logger *Logger) Panicf(format string, a ...any) {
678682
// for its operands and writes to log.Writer. Spaces are always added
679683
// between operands and a newline is appended.
680684
func (logger *Logger) Panicln(a ...any) (int, error) {
681-
logger.echo(nil, level.Panic, formatStrLn, a...)
685+
logger.echo(nil, level.Panic, formatPrintln, a...)
682686
panic(fmt.Sprintln(a...))
683687
}
684688

685689
// Ffatal creates message with Fatal level, using the default formats
686690
// for its operands and writes to w. Spaces are added between operands
687691
// when neither is a string.
688692
func (logger *Logger) Ffatal(w io.Writer, a ...any) {
689-
logger.echo(w, level.Fatal, formatStr, a...)
693+
logger.echo(w, level.Fatal, formatPrint, a...)
690694
exit(logger.fatalStatusCode)
691695
}
692696

@@ -701,15 +705,15 @@ func (logger *Logger) Ffatalf(w io.Writer, format string, a ...any) {
701705
// for its operands and writes to w. Spaces are always added between
702706
// operands and a newline is appended.
703707
func (logger *Logger) Ffatalln(w io.Writer, a ...any) {
704-
logger.echo(w, level.Fatal, formatStrLn, a...)
708+
logger.echo(w, level.Fatal, formatPrintln, a...)
705709
exit(logger.fatalStatusCode)
706710
}
707711

708712
// Fatal creates message with Fatal level, using the default formats
709713
// for its operands and writes to log.Writer. Spaces are added between
710714
// operands when neither is a string.
711715
func (logger *Logger) Fatal(a ...any) {
712-
logger.echo(nil, level.Fatal, formatStr, a...)
716+
logger.echo(nil, level.Fatal, formatPrint, a...)
713717
exit(logger.fatalStatusCode)
714718
}
715719

@@ -724,15 +728,15 @@ func (logger *Logger) Fatalf(format string, a ...any) {
724728
// for its operands and writes to log.Writer. Spaces are always added
725729
// between operands and a newline is appended.
726730
func (logger *Logger) Fatalln(a ...any) {
727-
logger.echo(nil, level.Fatal, formatStrLn, a...)
731+
logger.echo(nil, level.Fatal, formatPrintln, a...)
728732
exit(logger.fatalStatusCode)
729733
}
730734

731735
// Ferror creates message with Error level, using the default formats
732736
// for its operands and writes to w. Spaces are added between operands
733737
// when neither is a string.
734738
func (logger *Logger) Ferror(w io.Writer, a ...any) {
735-
logger.echo(w, level.Error, formatStr, a...)
739+
logger.echo(w, level.Error, formatPrint, a...)
736740
}
737741

738742
// Ferrorf creates message with Error level, according to a format
@@ -745,14 +749,14 @@ func (logger *Logger) Ferrorf(w io.Writer, f string, a ...any) {
745749
// for its operands and writes to w. Spaces are always added between
746750
// operands and a newline is appended.
747751
func (logger *Logger) Ferrorln(w io.Writer, a ...any) {
748-
logger.echo(w, level.Error, formatStrLn, a...)
752+
logger.echo(w, level.Error, formatPrintln, a...)
749753
}
750754

751755
// Error creates message with Error level, using the default formats
752756
// for its operands and writes to log.Writer. Spaces are added between
753757
// operands when neither is a string.
754758
func (logger *Logger) Error(a ...any) {
755-
logger.echo(nil, level.Error, formatStr, a...)
759+
logger.echo(nil, level.Error, formatPrint, a...)
756760
}
757761

758762
// Errorf creates message with Error level, according to a format specifier
@@ -765,14 +769,14 @@ func (logger *Logger) Errorf(f string, a ...any) {
765769
// for its operands and writes to log.Writer. Spaces are always added
766770
// between operands and a newline is appended.
767771
func (logger *Logger) Errorln(a ...any) {
768-
logger.echo(nil, level.Error, formatStrLn, a...)
772+
logger.echo(nil, level.Error, formatPrintln, a...)
769773
}
770774

771775
// Fwarn creates message with Warn level, using the default formats
772776
// for its operands and writes to w. Spaces are added between operands
773777
// when neither is a string.
774778
func (logger *Logger) Fwarn(w io.Writer, a ...any) {
775-
logger.echo(w, level.Warn, formatStr, a...)
779+
logger.echo(w, level.Warn, formatPrint, a...)
776780
}
777781

778782
// Fwarnf creates message with Warn level, according to a format
@@ -785,14 +789,14 @@ func (logger *Logger) Fwarnf(w io.Writer, format string, a ...any) {
785789
// for its operands and writes to w. Spaces are always added between
786790
// operands and a newline is appended.
787791
func (logger *Logger) Fwarnln(w io.Writer, a ...any) {
788-
logger.echo(w, level.Warn, formatStrLn, a...)
792+
logger.echo(w, level.Warn, formatPrintln, a...)
789793
}
790794

791795
// Warn creates message with Warn level, using the default formats
792796
// for its operands and writes to log.Writer. Spaces are added between
793797
// operands when neither is a string.
794798
func (logger *Logger) Warn(a ...any) {
795-
logger.echo(nil, level.Warn, formatStr, a...)
799+
logger.echo(nil, level.Warn, formatPrint, a...)
796800
}
797801

798802
// Warnf creates message with Warn level, according to a format specifier
@@ -805,14 +809,14 @@ func (logger *Logger) Warnf(format string, a ...any) {
805809
// for its operands and writes to log.Writer. Spaces are always added
806810
// between operands and a newline is appended.
807811
func (logger *Logger) Warnln(a ...any) {
808-
logger.echo(nil, level.Warn, formatStrLn, a...)
812+
logger.echo(nil, level.Warn, formatPrintln, a...)
809813
}
810814

811815
// Finfo creates message with Info level, using the default formats
812816
// for its operands and writes to w. Spaces are added between operands
813817
// when neither is a string.
814818
func (logger *Logger) Finfo(w io.Writer, a ...any) {
815-
logger.echo(w, level.Info, formatStr, a...)
819+
logger.echo(w, level.Info, formatPrint, a...)
816820
}
817821

818822
// Finfof creates message with Info level, according to a format
@@ -825,14 +829,14 @@ func (logger *Logger) Finfof(w io.Writer, format string, a ...any) {
825829
// for its operands and writes to w. Spaces are always added between
826830
// operands and a newline is appended.
827831
func (logger *Logger) Finfoln(w io.Writer, a ...any) {
828-
logger.echo(w, level.Info, formatStrLn, a...)
832+
logger.echo(w, level.Info, formatPrintln, a...)
829833
}
830834

831835
// Info creates message with Info level, using the default formats
832836
// for its operands and writes to log.Writer. Spaces are added between
833837
// operands when neither is a string.
834838
func (logger *Logger) Info(a ...any) {
835-
logger.echo(nil, level.Info, formatStr, a...)
839+
logger.echo(nil, level.Info, formatPrint, a...)
836840
}
837841

838842
// Infof creates message with Info level, according to a format specifier
@@ -845,14 +849,14 @@ func (logger *Logger) Infof(format string, a ...any) {
845849
// for its operands and writes to log.Writer. Spaces are always added
846850
// between operands and a newline is appended.
847851
func (logger *Logger) Infoln(a ...any) {
848-
logger.echo(nil, level.Info, formatStrLn, a...)
852+
logger.echo(nil, level.Info, formatPrintln, a...)
849853
}
850854

851855
// Fdebug creates message with Debug level, using the default formats
852856
// for its operands and writes to w. Spaces are added between operands
853857
// when neither is a string.
854858
func (logger *Logger) Fdebug(w io.Writer, a ...any) {
855-
logger.echo(w, level.Debug, formatStr, a...)
859+
logger.echo(w, level.Debug, formatPrint, a...)
856860
}
857861

858862
// Fdebugf creates message with Debug level, according to a format
@@ -865,14 +869,14 @@ func (logger *Logger) Fdebugf(w io.Writer, format string, a ...any) {
865869
// for its operands and writes to w. Spaces are always added between
866870
// operands and a newline is appended.
867871
func (logger *Logger) Fdebugln(w io.Writer, a ...any) {
868-
logger.echo(w, level.Debug, formatStrLn, a...)
872+
logger.echo(w, level.Debug, formatPrintln, a...)
869873
}
870874

871875
// Debug creates message with Debug level, using the default formats
872876
// for its operands and writes to log.Writer. Spaces are added between
873877
// operands when neither is a string.
874878
func (logger *Logger) Debug(a ...any) {
875-
logger.echo(nil, level.Debug, formatStr, a...)
879+
logger.echo(nil, level.Debug, formatPrint, a...)
876880
}
877881

878882
// Debugf creates message with Debug level, according to a format specifier
@@ -886,14 +890,14 @@ func (logger *Logger) Debugf(format string, a ...any) {
886890
// for its operands and writes to log.Writer. Spaces are always added
887891
// between operands and a newline is appended.
888892
func (logger *Logger) Debugln(a ...any) {
889-
logger.echo(nil, level.Debug, formatStrLn, a...)
893+
logger.echo(nil, level.Debug, formatPrintln, a...)
890894
}
891895

892896
// Ftrace creates message with Trace level, using the default formats
893897
// for its operands and writes to w. Spaces are added between operands
894898
// when neither is a string.
895899
func (logger *Logger) Ftrace(w io.Writer, a ...any) {
896-
logger.echo(w, level.Trace, formatStr, a...)
900+
logger.echo(w, level.Trace, formatPrint, a...)
897901
}
898902

899903
// Ftracef creates message with Trace level, according to a format
@@ -906,14 +910,14 @@ func (logger *Logger) Ftracef(w io.Writer, format string, a ...any) {
906910
// for its operands and writes to w. Spaces are always added between
907911
// operands and a newline is appended.
908912
func (logger *Logger) Ftraceln(w io.Writer, a ...any) {
909-
logger.echo(w, level.Trace, formatStrLn, a...)
913+
logger.echo(w, level.Trace, formatPrintln, a...)
910914
}
911915

912916
// Trace creates message with Trace level, using the default formats
913917
// for its operands and writes to log.Writer. Spaces are added between
914918
// operands when neither is a string.
915919
func (logger *Logger) Trace(a ...any) {
916-
logger.echo(nil, level.Trace, formatStr, a...)
920+
logger.echo(nil, level.Trace, formatPrint, a...)
917921
}
918922

919923
// Tracef creates message with Trace level, according to a format specifier
@@ -926,5 +930,5 @@ func (logger *Logger) Tracef(format string, a ...any) {
926930
// for its operands and writes to log.Writer. Spaces are always added
927931
// between operands and a newline is appended.
928932
func (logger *Logger) Traceln(a ...any) {
929-
logger.echo(nil, level.Trace, formatStrLn, a...)
933+
logger.echo(nil, level.Trace, formatPrintln, a...)
930934
}

logger_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func TestEchoWithTextFormatting(t *testing.T) {
108108
},
109109
{
110110
name: "System formatStr",
111-
format: formatStr,
111+
format: formatPrint,
112112
in: []interface{}{"hello", "world"},
113113
want: "helloworld", // used fmt.Print
114114
},
115115
{
116116
name: "System formatStrLn",
117-
format: formatStrLn,
117+
format: formatPrintln,
118118
in: []interface{}{"hello", "world"},
119119
want: " hello world\n", // used fmt.Println
120120
},
@@ -165,13 +165,13 @@ func TestEchoWithJSONFormatting(t *testing.T) {
165165
},
166166
{
167167
name: "System formatStr",
168-
format: formatStr,
168+
format: formatPrint,
169169
in: []interface{}{"hello", "world"},
170170
want: "helloworld", // used fmt.Print
171171
},
172172
{
173173
name: "System formatStrLn",
174-
format: formatStrLn,
174+
format: formatPrintln,
175175
in: []interface{}{"hello", "world"},
176176
want: "hello world", // used fmt.Println with Trim
177177
},

tools.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ func textMessage(
147147
switch {
148148
case f == "":
149149
fallthrough
150-
case f == formatStr:
150+
case f == formatPrint:
151151
// For messages that are output on the same line, the task of
152152
// separating the messages falls on the user. We don't need to
153153
// add extra characters to user messages.
154154
// msg = fmt.Sprintf("%s%s%s", sb.String(), fmt.Sprint(a...), o.Space)
155155
msg = fmt.Sprintf("%s%s", sb.String(), fmt.Sprint(a...))
156-
case f == formatStrLn:
156+
case f == formatPrintln:
157157
msg = fmt.Sprintf("%s%s", sb.String(), fmt.Sprintln(a...))
158158
default:
159159
msg = fmt.Sprintf("%s%s", sb.String(), fmt.Sprintf(f, a...))
@@ -223,9 +223,9 @@ func objectMessage(
223223
switch {
224224
case f == "":
225225
fallthrough
226-
case f == formatStr:
226+
case f == formatPrint:
227227
obj.Message = fmt.Sprint(a...)
228-
case f == formatStrLn:
228+
case f == formatPrintln:
229229
obj.Message = strings.TrimSuffix(fmt.Sprintln(a...), "\n")
230230
default:
231231
obj.Message = fmt.Sprintf(f, a...)
@@ -241,7 +241,7 @@ func objectMessage(
241241
switch {
242242
case f == "":
243243
fallthrough
244-
case f == formatStr:
244+
case f == formatPrint:
245245
msg = fmt.Sprintf("%s%s", data, o.Space)
246246
default: // for formatStrLn and others
247247
msg = fmt.Sprintf("%s\n", data)

0 commit comments

Comments
 (0)