-
-
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.
Merge branch 'master' of https://github.com/rusq/slackdump
- Loading branch information
Showing
7 changed files
with
154 additions
and
81 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 |
---|---|---|
|
@@ -54,3 +54,5 @@ dist/ | |
/tmp | ||
*.dot | ||
*.gz | ||
|
||
*.html |
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 | ||
} |
Oops, something went wrong.