1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

[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:
Shu xian 2020-02-07 05:35:46 +08:00 committed by GitHub
parent dbeb7cfe9c
commit fdfb2bc4c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 123 additions and 0 deletions

43
logger/field.go Normal file
View 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
View 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
View 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
View 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
}