-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
77 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
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
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 |
---|---|---|
@@ -1,23 +1,57 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/rusq/dlog" | ||
) | ||
|
||
// Interface is the interface for a logger. | ||
type Interface interface { | ||
Debug(...any) | ||
Debugf(fmt string, a ...any) | ||
Debugln(...any) | ||
Print(...any) | ||
Printf(fmt string, a ...any) | ||
Println(...any) | ||
IsDebug() bool | ||
} | ||
|
||
// Default is the default logger. It logs to stderr and debug logging can be | ||
// enabled by setting the DEBUG environment variable to 1. For example: | ||
// | ||
// DEBUG=1 slackdump | ||
var Default = dlog.New(log.Default().Writer(), "", log.LstdFlags, os.Getenv("DEBUG") == "1") | ||
|
||
// note: previously ioutil.Discard which is not deprecated in favord of io.Discard | ||
// so this is valid only from go1.16 | ||
var Silent = dlog.New(io.Discard, "", log.LstdFlags, false) | ||
// Silent is a logger that does not log anything. | ||
var Silent = silent{} | ||
|
||
// Silent is a logger that does not log anything. | ||
type silent struct{} | ||
|
||
func (s silent) Debug(...any) {} | ||
func (s silent) Debugf(fmt string, a ...any) {} | ||
func (s silent) Debugln(...any) {} | ||
func (s silent) Print(...any) {} | ||
func (s silent) Printf(fmt string, a ...any) {} | ||
func (s silent) Println(...any) {} | ||
func (s silent) IsDebug() bool { return false } | ||
|
||
type logCtx uint8 | ||
|
||
const ( | ||
logCtxKey logCtx = iota | ||
) | ||
|
||
func NewContext(ctx context.Context, l Interface) context.Context { | ||
return context.WithValue(ctx, logCtxKey, l) | ||
} | ||
|
||
func FromContext(ctx context.Context) Interface { | ||
if l, ok := ctx.Value(logCtxKey).(Interface); ok { | ||
return l | ||
} | ||
return Default | ||
} |
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,16 @@ | ||
package logger | ||
|
||
import "testing" | ||
|
||
func BenchmarkSlientPrintf(b *testing.B) { | ||
var l = Silent | ||
for i := 0; i < b.N; i++ { | ||
l.Printf("hello world, %s, %d", "foo", i) | ||
} | ||
// This benchmark compares the performance of the Silent logger when | ||
// using io.Discard, and when using a no-op function. | ||
// io.Discard: BenchmarkSlientPrintf-16 93075956 12.92 ns/op 8 B/op 0 allocs/op | ||
// no-op func: BenchmarkSlientPrintf-16 1000000000 0.2364 ns/op 0 B/op 0 allocs/op | ||
// | ||
// Oh, look! We have an WINNER. The no-op function wins, no surprises. | ||
} |
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