logger with helper methods (#1216)

* support unix daemon socket

* refactor(logger): logger fields changed to map[string]interface{}

* improvement(logger): adding string to Level Parser

* improvement(logger): rename ParseLevel to GetLevel

* refactor(logger): adding basic logger

adding micro default logger, and refactor logger interface

* refactor(logger): moved basic logger to top level package

* refactor(logger): adding default logger
This commit is contained in:
Sumanth Chinthagunta
2020-02-20 23:57:59 -08:00
committed by GitHub
parent 88457b812e
commit 3fa7c26946
4 changed files with 196 additions and 58 deletions

View File

@@ -2,30 +2,48 @@ package logger
import (
"context"
"io"
)
// 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 {
// The Log Level
// The logging level the logger should log at. default is `InfoLevel`
Level Level
// Other opts
// fields to always be logged
Fields map[string]interface{}
// It's common to set this to a file, or leave it default which is `os.Stderr`
Out io.Writer
// Alternative options
Context context.Context
}
// WithLevel sets the log level
func WithLevel(l Level) Option {
return func(o *Options) {
o.Level = l
// WithFields set default fields for the logger
func WithFields(fields map[string]interface{}) Option {
return func(args *Options) {
args.Fields = fields
}
}
// WithLevel set default level for the logger
func WithLevel(level Level) Option {
return func(args *Options) {
args.Level = level
}
}
// WithOutput set default output writer for the logger
func WithOutput(out io.Writer) Option {
return func(args *Options) {
args.Out = out
}
}
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}