-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chris Koch <[email protected]>
- Loading branch information
Showing
5 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2024 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package llog_test | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/u-root/uio/llog" | ||
) | ||
|
||
func ExampleDefault_withtime() { | ||
l := llog.Default() | ||
l.Infof("An INFO level string") | ||
l.Debugf("A DEBUG level that does not appear") | ||
|
||
l.Level = llog.Level(slog.LevelDebug) | ||
l.Debugf("A DEBUG level that appears") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package llog_test | ||
|
||
import ( | ||
"log" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/u-root/uio/llog" | ||
) | ||
|
||
func ExampleDefault() { | ||
// Example is reproducible without date/time displayed. | ||
log.SetFlags(0) | ||
// Examples only go out to stdout. | ||
log.SetOutput(os.Stdout) | ||
|
||
l := llog.Default() | ||
l.Infof("An INFO level string") | ||
l.Debugf("A DEBUG level that does not appear") | ||
|
||
l.Level = llog.Level(slog.LevelDebug) | ||
l.Debugf("A DEBUG level that appears") | ||
|
||
l.Warnf("I'm warning you") | ||
l.Errorf("This is going to error") | ||
|
||
// Output: | ||
// INFO An INFO level string | ||
// DEBUG A DEBUG level that appears | ||
// WARN I'm warning you | ||
// ERROR This is going to error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package llog_test | ||
|
||
import ( | ||
"flag" | ||
"log/slog" | ||
|
||
"github.com/u-root/uio/llog" | ||
) | ||
|
||
func someFunc(v llog.Printf) { | ||
v("logs at the given level %s", "foo") | ||
} | ||
|
||
func Example() { | ||
l := llog.Default() | ||
// If -v is set, l.Level becomes slog.LevelDebug. | ||
l.Level.RegisterDebugFlag(flag.CommandLine, "v") | ||
flag.Parse() | ||
|
||
someFunc(l.Debugf) | ||
someFunc(l.Printf(slog.LevelWarn)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright 2024 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package llog | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLevelFlag(t *testing.T) { | ||
for _, tt := range []struct { | ||
args []string | ||
want Level | ||
}{ | ||
{ | ||
args: []string{"-level=4"}, | ||
want: Level(slog.LevelWarn), | ||
}, | ||
{ | ||
args: []string{}, | ||
want: Level(slog.LevelInfo), | ||
}, | ||
} { | ||
f := flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
var v Level | ||
v.RegisterLevelFlag(f, "level") | ||
f.Parse(tt.args) | ||
|
||
if v != tt.want { | ||
t.Errorf("Parse(%#v) = %v, want %v", tt.args, v, tt.want) | ||
} | ||
} | ||
|
||
for _, tt := range []struct { | ||
args []string | ||
want Level | ||
err error | ||
}{ | ||
{ | ||
args: []string{"-v"}, | ||
want: Level(slog.LevelWarn), | ||
}, | ||
{ | ||
args: []string{}, | ||
want: Level(slog.LevelInfo), | ||
}, | ||
{ | ||
args: []string{"-v=true"}, | ||
want: Level(slog.LevelWarn), | ||
}, | ||
{ | ||
args: []string{"-v=true", "-v=false"}, | ||
want: Level(slog.LevelWarn), | ||
}, | ||
{ | ||
args: []string{"-v=foobar"}, | ||
want: Level(slog.LevelInfo), | ||
err: strconv.ErrSyntax, | ||
}, | ||
} { | ||
f := flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
var v Level | ||
v.RegisterVerboseFlag(f, "v", slog.LevelWarn) | ||
// Parse doesn't use %w. | ||
if err := f.Parse(tt.args); err != tt.err && err != nil && !strings.Contains(err.Error(), tt.err.Error()) { | ||
t.Errorf("Parse(%#v) = %v, want %v", tt.args, err, tt.err) | ||
} | ||
if v != tt.want { | ||
t.Errorf("Parse(%#v) = %v, want %v", tt.args, v, tt.want) | ||
} | ||
} | ||
|
||
for _, tt := range []struct { | ||
args []string | ||
want Level | ||
err error | ||
}{ | ||
{ | ||
args: []string{"-v"}, | ||
want: Level(slog.LevelDebug), | ||
}, | ||
{ | ||
args: []string{}, | ||
want: Level(slog.LevelInfo), | ||
}, | ||
{ | ||
args: []string{"-v=true"}, | ||
want: Level(slog.LevelDebug), | ||
}, | ||
{ | ||
args: []string{"-v=true", "-v=false"}, | ||
want: Level(slog.LevelDebug), | ||
}, | ||
{ | ||
args: []string{"-v=foobar"}, | ||
want: Level(slog.LevelInfo), | ||
err: strconv.ErrSyntax, | ||
}, | ||
} { | ||
f := flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
var v Level | ||
v.RegisterDebugFlag(f, "v") | ||
// Parse doesn't use %w. | ||
if err := f.Parse(tt.args); err != tt.err && err != nil && !strings.Contains(err.Error(), tt.err.Error()) { | ||
t.Errorf("Parse(%#v) = %v, want %v", tt.args, err, tt.err) | ||
} | ||
if v != tt.want { | ||
t.Errorf("Parse(%#v) = %v, want %v", tt.args, v, tt.want) | ||
} | ||
} | ||
} | ||
|
||
func TestNilLogger(t *testing.T) { | ||
for _, l := range []*Logger{nil, &Logger{}} { | ||
// Test that none of this panics. | ||
l.Printf(slog.LevelDebug)("nothing") | ||
l.Debugf("nothing") | ||
l.Infof("nothing") | ||
l.Warnf("nothing") | ||
l.Errorf("nothing") | ||
l.Logf(slog.LevelDebug, "nothing") | ||
} | ||
} | ||
|
||
func TestLog(t *testing.T) { | ||
var s strings.Builder | ||
l := New(Level(slog.LevelDebug), func(format string, args ...any) { | ||
fmt.Fprintf(&s, format+"\n", args...) | ||
}) | ||
|
||
l.Printf(slog.LevelDebug)("nothing") | ||
l.Debugf("nothing") | ||
l.Infof("nothing") | ||
l.Warnf("nothing") | ||
l.Errorf("nothing") | ||
l.Logf(slog.LevelDebug, "nothing") | ||
|
||
want := `DEBUG nothing | ||
DEBUG nothing | ||
INFO nothing | ||
WARN nothing | ||
ERROR nothing | ||
DEBUG nothing | ||
` | ||
if got := s.String(); got != want { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestDefaults(t *testing.T) { | ||
var s strings.Builder | ||
log.SetOutput(&s) | ||
log.SetFlags(0) | ||
|
||
l := Debug() | ||
l.Debugf("foobar") | ||
want := "DEBUG foobar\n" | ||
if got := s.String(); got != want { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
|
||
l = Test(t) | ||
l.Debugf("more foobar") | ||
} |