forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
naming discovery sdk and it's dependency
- Loading branch information
1 parent
cc57564
commit 7d6f233
Showing
12 changed files
with
2,007 additions
and
1 deletion.
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,56 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
|
||
pkgerr "github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
_log = "log" | ||
) | ||
|
||
// Handler is used to handle log events, outputting them to | ||
// stdio or sending them to remote services. See the "handlers" | ||
// directory for implementations. | ||
// | ||
// It is left up to Handlers to implement thread-safety. | ||
type Handler interface { | ||
// Log handle log | ||
// variadic D is k-v struct represent log content | ||
Log(context.Context, Level, ...D) | ||
|
||
// SetFormat set render format on log output | ||
// see StdoutHandler.SetFormat for detail | ||
SetFormat(string) | ||
|
||
// Close handler | ||
Close() error | ||
} | ||
|
||
// Handlers . | ||
type Handlers []Handler | ||
|
||
// Log handlers logging. | ||
func (hs Handlers) Log(c context.Context, lv Level, d ...D) { | ||
for _, h := range hs { | ||
h.Log(c, lv, d...) | ||
} | ||
} | ||
|
||
// Close close resource. | ||
func (hs Handlers) Close() (err error) { | ||
for _, h := range hs { | ||
if e := h.Close(); e != nil { | ||
err = pkgerr.WithStack(e) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// SetFormat . | ||
func (hs Handlers) SetFormat(format string) { | ||
for _, h := range hs { | ||
h.SetFormat(format) | ||
} | ||
} |
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,29 @@ | ||
package log | ||
|
||
// Level of severity. | ||
type Level int | ||
|
||
// Verbose is a boolean type that implements Info, Infov (like Printf) etc. | ||
type Verbose bool | ||
|
||
// common log level. | ||
const ( | ||
_debugLevel Level = iota | ||
_infoLevel | ||
_warnLevel | ||
_errorLevel | ||
_fatalLevel | ||
) | ||
|
||
var levelNames = [...]string{ | ||
_debugLevel: "DEBUG", | ||
_infoLevel: "INFO", | ||
_warnLevel: "WARN", | ||
_errorLevel: "ERROR", | ||
_fatalLevel: "FATAL", | ||
} | ||
|
||
// String implementation. | ||
func (l Level) String() string { | ||
return levelNames[l] | ||
} |
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,88 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// Config log config. | ||
type Config struct { | ||
Family string | ||
Host string | ||
|
||
// stdout | ||
Stdout bool | ||
|
||
// file | ||
Dir string | ||
// buffer size | ||
FileBufferSize int64 | ||
// MaxLogFile | ||
MaxLogFile int | ||
// RotateSize | ||
RotateSize int64 | ||
|
||
// V Enable V-leveled logging at the specified level. | ||
V int32 | ||
// Module="" | ||
// The syntax of the argument is a map of pattern=N, | ||
// where pattern is a literal file name (minus the ".go" suffix) or | ||
// "glob" pattern and N is a V level. For instance: | ||
// [module] | ||
// "service" = 1 | ||
// "dao*" = 2 | ||
// sets the V level to 2 in all Go files whose names begin "dao". | ||
Module map[string]int32 | ||
// Filter tell log handler which field are sensitive message, use * instead. | ||
Filter []string | ||
} | ||
|
||
var ( | ||
h Handler | ||
c *Config | ||
) | ||
|
||
// D represents a map of entry level data used for structured logging. | ||
// type D map[string]interface{} | ||
type D struct { | ||
Key string | ||
Value interface{} | ||
} | ||
|
||
// KV return a log kv for logging field. | ||
func KV(key string, value interface{}) D { | ||
return D{ | ||
Key: key, | ||
Value: value, | ||
} | ||
} | ||
|
||
// Info logs a message at the info log level. | ||
func Info(format string, args ...interface{}) { | ||
h.Log(context.Background(), _infoLevel, KV(_log, fmt.Sprintf(format, args...))) | ||
} | ||
|
||
// Warn logs a message at the warning log level. | ||
func Warn(format string, args ...interface{}) { | ||
h.Log(context.Background(), _warnLevel, KV(_log, fmt.Sprintf(format, args...))) | ||
} | ||
|
||
// Error logs a message at the error log level. | ||
func Error(format string, args ...interface{}) { | ||
h.Log(context.Background(), _errorLevel, KV(_log, fmt.Sprintf(format, args...))) | ||
} | ||
|
||
func logw(args []interface{}) []D { | ||
if len(args)%2 != 0 { | ||
Warn("log: the variadic must be plural, the last one will ignored") | ||
} | ||
ds := make([]D, 0, len(args)/2) | ||
for i := 0; i < len(args)-1; i = i + 2 { | ||
if key, ok := args[i].(string); ok { | ||
ds = append(ds, KV(key, args[i+1])) | ||
} else { | ||
Warn("log: key must be string, get %T, ignored", args[i]) | ||
} | ||
} | ||
return ds | ||
} |
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,83 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// V reports whether verbosity at the call site is at least the requested level. | ||
// The returned value is a boolean of type Verbose, which implements Info, Infov etc. | ||
// These methods will write to the Info log if called. | ||
// Thus, one may write either | ||
// if log.V(2) { log.Info("log this") } | ||
// or | ||
// log.V(2).Info("log this") | ||
// The second form is shorter but the first is cheaper if logging is off because it does | ||
// not evaluate its arguments. | ||
// | ||
// Whether an individual call to V generates a log record depends on the setting of | ||
// the Config.VLevel and Config.Module flags; both are off by default. If the level in the call to | ||
// V is at least the value of Config.VLevel, or of Config.Module for the source file containing the | ||
// call, the V call will log. | ||
// v must be more than 0. | ||
func V(v int32) Verbose { | ||
var ( | ||
file string | ||
) | ||
if v < 0 { | ||
return Verbose(false) | ||
} else if c.V >= v { | ||
return Verbose(true) | ||
} | ||
if pc, _, _, ok := runtime.Caller(1); ok { | ||
file, _ = runtime.FuncForPC(pc).FileLine(pc) | ||
} | ||
if strings.HasSuffix(file, ".go") { | ||
file = file[:len(file)-3] | ||
} | ||
if slash := strings.LastIndex(file, "/"); slash >= 0 { | ||
file = file[slash+1:] | ||
} | ||
for filter, lvl := range c.Module { | ||
var match bool | ||
if match = filter == file; !match { | ||
match, _ = filepath.Match(filter, file) | ||
} | ||
if match { | ||
return Verbose(lvl >= v) | ||
} | ||
} | ||
return Verbose(false) | ||
} | ||
|
||
// Info logs a message at the info log level. | ||
func (v Verbose) Info(format string, args ...interface{}) { | ||
if v { | ||
h.Log(context.Background(), _infoLevel, KV(_log, fmt.Sprintf(format, args...))) | ||
} | ||
} | ||
|
||
// Infov logs a message at the info log level. | ||
func (v Verbose) Infov(ctx context.Context, args ...D) { | ||
if v { | ||
h.Log(ctx, _infoLevel, args...) | ||
} | ||
} | ||
|
||
// Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With. | ||
func (v Verbose) Infow(ctx context.Context, args ...interface{}) { | ||
if v { | ||
h.Log(ctx, _infoLevel, logw(args)...) | ||
} | ||
} | ||
|
||
// Close close resource. | ||
func (v Verbose) Close() (err error) { | ||
if h == nil { | ||
return | ||
} | ||
return h.Close() | ||
} |
Oops, something went wrong.