[WIP] logger first (#1161)
* logger first * log->logger * update comment * add context in Options * add Fields * remove logfi * add field encode * add common Field Types * update logger field
This commit is contained in:
parent
dbeb7cfe9c
commit
fdfb2bc4c1
43
logger/field.go
Normal file
43
logger/field.go
Normal file
@ -0,0 +1,43 @@
|
||||
package logger
|
||||
|
||||
type FieldType uint8
|
||||
|
||||
type Encode func(*Field) string
|
||||
|
||||
type Field struct {
|
||||
Key string
|
||||
Type FieldType
|
||||
Value interface{}
|
||||
Encode Encode
|
||||
}
|
||||
|
||||
func (f *Field) GetValue() interface{} {
|
||||
if f.Encode != nil {
|
||||
return f.Encode(f)
|
||||
}
|
||||
|
||||
return f.Value
|
||||
}
|
||||
|
||||
// preset common types for choosing encoder faster
|
||||
const (
|
||||
UnknownType FieldType = iota
|
||||
BoolType
|
||||
DurationType
|
||||
Float64Type
|
||||
Float32Type
|
||||
Int64Type
|
||||
Int32Type
|
||||
Int16Type
|
||||
Int8Type
|
||||
Uint64Type
|
||||
Uint32Type
|
||||
Uint16Type
|
||||
Uint8Type
|
||||
StringType
|
||||
TimeType
|
||||
)
|
||||
|
||||
func Bool(key string, val bool) Field {
|
||||
return Field{Key: key, Type: BoolType, Value: val}
|
||||
}
|
13
logger/level.go
Normal file
13
logger/level.go
Normal file
@ -0,0 +1,13 @@
|
||||
package logger
|
||||
|
||||
type Level int8
|
||||
|
||||
const (
|
||||
TraceLevel Level = iota - 1
|
||||
DebugLevel
|
||||
InfoLevel
|
||||
WarnLevel
|
||||
ErrorLevel
|
||||
PanicLevel
|
||||
FatalLevel
|
||||
)
|
48
logger/logger.go
Normal file
48
logger/logger.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Package log provides a log interface
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Logger is a generic logging interface
|
||||
type Logger interface {
|
||||
Init(options ...Option) error
|
||||
// String returns the name of logger
|
||||
String() string
|
||||
// SetLevel updates the logging level.
|
||||
SetLevel(Level)
|
||||
// Level returns the logging level
|
||||
Level() Level
|
||||
// Log inserts a log entry. Arguments may be handled in the manner
|
||||
// of fmt.Print, but the underlying logger may also decide to handle
|
||||
// them differently.
|
||||
Log(level Level, v ...interface{})
|
||||
// Logf insets a log entry. Arguments are handled in the manner of
|
||||
// fmt.Printf.
|
||||
Logf(level Level, format string, v ...interface{})
|
||||
// Fields set fields to always be logged
|
||||
Fields(fields ...Field) Logger
|
||||
}
|
||||
|
||||
var (
|
||||
mux sync.Mutex
|
||||
loggerMap = map[string]Logger{}
|
||||
)
|
||||
|
||||
func Register(logger Logger) {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
|
||||
loggerMap[logger.String()] = logger
|
||||
}
|
||||
|
||||
func GetLogger(name string) (Logger, error) {
|
||||
l := loggerMap[name]
|
||||
if l == nil {
|
||||
return nil, fmt.Errorf("no such name logger found %s", name)
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
19
logger/options.go
Normal file
19
logger/options.go
Normal file
@ -0,0 +1,19 @@
|
||||
package logger
|
||||
|
||||
import "context"
|
||||
|
||||
// Option for load profiles maybe
|
||||
// eg. yml
|
||||
// micro:
|
||||
// logger:
|
||||
// name:
|
||||
// dialect: zap/default/logrus
|
||||
// zap:
|
||||
// xxx:
|
||||
// logrus:
|
||||
// xxx:
|
||||
type Option func(*Options)
|
||||
|
||||
type Options struct {
|
||||
Context context.Context
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user