logger

package
v1.0.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModuleHTTP     = "HTTP"
	ModuleRPC      = "RPC"
	ModuleMySQL    = "MySQL"
	ModuleRedis    = "Redis"
	ModuleRabbitMQ = "RabbitMQ"
	ModuleCron     = "Cron"
)
View Source
const (
	LogID      = "log_id"
	TraceID    = "trace_id"
	Header     = "header"
	Method     = "method"
	Request    = "request"
	Response   = "response"
	Code       = "code"
	ClientIP   = "client_ip"
	ClientPort = "client_port"
	ServerIP   = "server_ip"
	ServerPort = "server_port"
	API        = "api"
	Cost       = "cost"
	Module     = "module"
	SericeName = "service_name"
	Timeout    = "timeout"
	Trace      = "trace"
)
View Source
const (
	LogHeader = "Log-Id"
)

Variables

This section is empty.

Functions

func RotateWriter added in v1.0.22

func RotateWriter(infoFile, errFile string) (infoWriter io.Writer, errWriter io.Writer, err error)

func ValueLogID

func ValueLogID(ctx context.Context) string

ValueLogID extract log id from context

func ValueTraceID

func ValueTraceID(ctx context.Context) string

ValueTraceID extract trace id from context

func WithHTTPFields

func WithHTTPFields(ctx context.Context, fields Fields) context.Context

WithHTTPFields inject common http log fields to context

func WithLogID

func WithLogID(ctx context.Context, val interface{}) context.Context

WithLogID inject log id to context

func WithTraceID

func WithTraceID(ctx context.Context, val interface{}) context.Context

WithTraceID inject trace_id id to context

Types

type Config

type Config struct {
	InfoFile  string
	ErrorFile string
	Level     string
}

Config is used to parse configuration file logger should be controlled with Options

type Fields

type Fields struct {
	LogID      string      `json:"log_id"`
	TraceID    string      `json:"trace_id"`
	Header     http.Header `json:"header"`
	Method     string      `json:"method"`
	Request    interface{} `json:"request"`
	Response   interface{} `json:"response"`
	Code       int         `json:"code"`
	ClientIP   string      `json:"client_ip"`
	ClientPort int         `json:"client_port"`
	ServerIP   string      `json:"server_ip"`
	ServerPort int         `json:"server_port"`
	API        string      `json:"api"`
	Cost       int64       `json:"cost"`
}

func ValueHTTPFields

func ValueHTTPFields(ctx context.Context) Fields

ValueHTTPFields extrect common http log fields from context

type Logger

type Logger struct {
	*zap.Logger
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(cfg *Config, options ...Option) (l *Logger, err error)

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, fields ...zap.Field)

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string, fields ...zap.Field)

func (*Logger) Fatal

func (l *Logger) Fatal(ctx context.Context, msg string, fields ...zap.Field)

func (*Logger) GetLevel

func (l *Logger) GetLevel() zapcore.Level

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, fields ...zap.Field)

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, fields ...zap.Field)

type ObjectId

type ObjectId [12]byte

LogId is a unique ID identifying a log record. It must be exactly 12 bytes long.

Reference:http://www.mongodb.org/display/DOCS/Object+IDs

func NewObjectId

func NewObjectId() ObjectId

NewObjectId returns a new unique ObjectId.

func NewObjectIdWithHexString

func NewObjectIdWithHexString(s string) (o ObjectId, err error)

func NewObjectIdWithTime

func NewObjectIdWithTime(t time.Time) ObjectId

NewObjectIdWithTime returns a dummy ObjectId with the timestamp part filled with the provided number of seconds from epoch UTC, and all other parts filled with zeroes. It's not safe to insert a document with an id generated by this method, it is useful only for queries to find documents with ids generated before or after the specified timestamp.

func StrToObjectId

func StrToObjectId(str string) (ObjectId, error)

StrToObjectId string id to object

func (ObjectId) Counter

func (id ObjectId) Counter() int32

Counter returns the incrementing value part of the id. It's a runtime error to call this method with an invalid id.

func (ObjectId) Hex

func (id ObjectId) Hex() string

Hex returns a hex representation of the ObjectId.

func (ObjectId) Machine

func (id ObjectId) Machine() []byte

Machine returns the 3-byte machine id part of the id. It's a runtime error to call this method with an invalid id.

func (ObjectId) Pid

func (id ObjectId) Pid() uint16

Pid returns the process id part of the id. It's a runtime error to call this method with an invalid id.

func (ObjectId) Time

func (id ObjectId) Time() time.Time

Time returns the timestamp part of the id. It's a runtime error to call this method with an invalid id.

type Option

type Option func(l *Options)

func WithCallerSkip

func WithCallerSkip(skip int) Option

func WithErrorWriter added in v1.0.22

func WithErrorWriter(w io.Writer) Option

func WithInfoWriter added in v1.0.22

func WithInfoWriter(w io.Writer) Option

func WithLevel added in v1.0.22

func WithLevel(l string) Option

func WithModule

func WithModule(module string) Option

func WithServiceName

func WithServiceName(serviceName string) Option

type Options added in v1.0.22

type Options struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL