zap_custom_logger

package module
v0.0.0-...-4d26816 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2020 License: MIT Imports: 12 Imported by: 0

README

ZAP custom logger

Installation

Run command on you [$GOPATH/src] path:

go get -u github.com/alex60217101990/zap_custom_logger

If you use: bash go mod the package will load automatically with other dependencies.

Usage

The package provides two basic models of the logbook, according to which indexes are created in Elasticsearch:

type EndpointLog struct {
	Level            string        `json:"level"`
	Msg              *string       `json:"msg,omitempty"`
	Timestamp        time.Time     `json:"timestamp,omitempty"`
	Error            interface{}   `json:"error,omitempty"`
	ServiceName      string        `json:"service_name"`
	InstancePublicIP string        `json:"instance_public_ip"`
	Version          string        `json:"version"`
	Namespace        *string       `json:"namespace"`
	StackTrace       *string       `json:"stacktrace,omitempty"`
	Url              *string       `json:"url"`
	TraceID          interface{}   `json:"trace_id"`
	RequestBody      interface{}   `json:"request_body"`
	ResponseBody     interface{}   `json:"response_body"`
	Duration         time.Duration `json:"duration"`
}

and

type ServiceLog struct {
	Level            string      `json:"level"`
	Msg              *string     `json:"msg,omitempty"`
	Timestamp        time.Time   `json:"timestamp,omitempty"`
	Error            interface{} `json:"error,omitempty"`
	ServiceName      string      `json:"service_name"`
	InstancePublicIP string      `json:"instance_public_ip"`
	StackTrace       *string     `json:"stacktrace,omitempty"`
	Version          string      `json:"version"`
	Namespace        *string     `json:"namespace"`
}

An example of creating a logger with some configuration parameters (for correct closure, it is desirable to convey a global context):

	ctx, cancel := context.WithCancel(context.Background())
	log := zap_custom_logger.NewZapLogger(
		zap_custom_logger.SetConfigs(&zap_custom_logger.Configs{
			App: zap_custom_logger.App{
                PublicIP: "<you local or global IP>",
                Version: "<some version for example: 0.0.1>",
                Namespace: "<some namespac: default - "default">",
                ServiceName: "<some service name>",
			},
			Storage: zap_custom_logger.Storage{
				Hosts:         []string{"<some host>"},
				LoggerStorage: zap_custom_logger.Elastic,
			},
			Encoder: zap_custom_logger.Console,
		}),
		zap_custom_logger.SetContext(context.Background()),
	)
	log.Connect()

Correct close forexample:

    defer func() {
		log.Close()
		cancel()
	}()

License

MIT

Documentation

Index

Constants

View Source
const (
	ServiceLogType errorType = iota + 1
	EndpointLogType
)

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

func BoolPtr

func BoolPtr(v *bool) bool

func ConvertErrTypeFromFloat64

func ConvertErrTypeFromFloat64(data float64) *errorType

func GetEndpointsLogsIndexName

func GetEndpointsLogsIndexName(serviceName *string) *string

func GetServiceLogsIndexName

func GetServiceLogsIndexName(serviceName *string) *string

func Int

func Int(v int) *int

func Int32

func Int32(v int32) *int32

func Int32Ptr

func Int32Ptr(v *int32) int32

func IntPtr

func IntPtr(v *int) int

func SetConfigs

func SetConfigs(conf *Configs) func(*ZapLogger) error

func SetContext

func SetContext(ctx context.Context) func(*ZapLogger) error

func String

func String(v string) *string

func StringPtr

func StringPtr(v *string) string

func TimePtrToTime

func TimePtrToTime(t *time.Time) (emptyTime time.Time)

func TimeToTimePtr

func TimeToTimePtr(t time.Time) *time.Time

Types

type App

type App struct {
	PublicIP    string
	Version     string
	ServiceName string
	Namespace   string
}

type Configs

type Configs struct {
	App     App
	Encoder EncoderType
	Storage Storage
}

type ElasticPlugin

type ElasticPlugin struct {
	Client *elastic.Client
	// contains filtered or unexported fields
}

func (*ElasticPlugin) Close

func (e *ElasticPlugin) Close()

func (*ElasticPlugin) Connect

func (e *ElasticPlugin) Connect(logger Logger) (err error)

func (*ElasticPlugin) InsertEndpointLogObjectString

func (e *ElasticPlugin) InsertEndpointLogObjectString(log []byte) error

func (*ElasticPlugin) InsertEndpointLogObjectStruct

func (e *ElasticPlugin) InsertEndpointLogObjectStruct(log interface{}) error

func (*ElasticPlugin) InsertServiceLogObjectString

func (e *ElasticPlugin) InsertServiceLogObjectString(log []byte) error

func (*ElasticPlugin) InsertServiceLogObjectStruct

func (e *ElasticPlugin) InsertServiceLogObjectStruct(log interface{}) error

func (*ElasticPlugin) Ping

func (e *ElasticPlugin) Ping(ctx context.Context) bool

Ping the ElasticSearch server

func (*ElasticPlugin) RemoveIndexByName

func (e *ElasticPlugin) RemoveIndexByName(name *string) error

func (*ElasticPlugin) SetEndpointsLogsMap

func (e *ElasticPlugin) SetEndpointsLogsMap() error

func (*ElasticPlugin) SetMapping

func (e *ElasticPlugin) SetMapping() (err error)

func (*ElasticPlugin) SetServiceLogsMap

func (e *ElasticPlugin) SetServiceLogsMap() error

type EncoderType

type EncoderType int
const (
	Console EncoderType = iota
	Json
)

func EncoderFromStrConvert

func EncoderFromStrConvert(t string) *EncoderType

func (EncoderType) String

func (t EncoderType) String() string

func (EncoderType) Val

func (t EncoderType) Val() int

type EndpointLog

type EndpointLog struct {
	Level            string        `json:"level"`
	Msg              *string       `json:"msg,omitempty"`
	Timestamp        time.Time     `json:"timestamp,omitempty"`
	Error            interface{}   `json:"error,omitempty"`
	ServiceName      string        `json:"service_name"`
	InstancePublicIP string        `json:"instance_public_ip"`
	Version          string        `json:"version"`
	Namespace        *string       `json:"namespace"`
	StackTrace       *string       `json:"stacktrace,omitempty"`
	Url              *string       `json:"url"`
	TraceID          interface{}   `json:"trace_id"`
	RequestBody      interface{}   `json:"request_body"`
	ResponseBody     interface{}   `json:"response_body"`
	Duration         time.Duration `json:"duration"`
	// contains filtered or unexported fields
}

func (EndpointLog) MarshalJSON

func (e EndpointLog) MarshalJSON() ([]byte, error)

func (*EndpointLog) UnmarshalJSON

func (e *EndpointLog) UnmarshalJSON(data []byte) (err error)

type IndexPlugin

type IndexPlugin interface {
	Connect(Logger) (err error)
	Close()
	RemoveIndexByName(name *string) error
	SetMapping() (err error)
	Ping(context.Context) bool
	// indexing data...
	InsertServiceLogObjectStruct(interface{}) error
	InsertServiceLogObjectString([]byte) error
	InsertEndpointLogObjectStruct(interface{}) error
	InsertEndpointLogObjectString([]byte) error
}

type LogStorageType

type LogStorageType int
const (
	Default LogStorageType = iota
	Elastic
	Loki
)

func LogStorageFromStrConvert

func LogStorageFromStrConvert(t string) *LogStorageType

func (LogStorageType) String

func (t LogStorageType) String() string

func (LogStorageType) Val

func (t LogStorageType) Val() int

type Logger

type Logger interface {
	GetConfigs() *Configs
	Ping(context.Context) bool
	Connect()
	Close()
	// methods
	PanicEndpoint(log *EndpointLog)
	PanicService(log *ServiceLog)
	InfoEndpoint(log *EndpointLog)
	InfoService(log *ServiceLog)
	WarnEndpoint(log *EndpointLog)
	WarnService(log *ServiceLog)
	ErrorEndpoint(log *EndpointLog)
	ErrorService(log *ServiceLog)
	FatalEndpoint(log *EndpointLog)
	FatalService(log *ServiceLog)
	GetZapNativeLogger() *zap.Logger
}

type ServiceLog

type ServiceLog struct {
	Level            string      `json:"level"`
	Msg              *string     `json:"msg,omitempty"`
	Timestamp        time.Time   `json:"timestamp,omitempty"`
	Error            interface{} `json:"error,omitempty"`
	ServiceName      string      `json:"service_name"`
	InstancePublicIP string      `json:"instance_public_ip"`
	StackTrace       *string     `json:"stacktrace,omitempty"`
	Version          string      `json:"version"`
	Namespace        *string     `json:"namespace"`
	// contains filtered or unexported fields
}

func (ServiceLog) MarshalJSON

func (e ServiceLog) MarshalJSON() ([]byte, error)

func (*ServiceLog) UnmarshalJSON

func (e *ServiceLog) UnmarshalJSON(data []byte) (err error)

type Storage

type Storage struct {
	LoggerStorage LogStorageType
	Hosts         []string
}

type SyncLogsService

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

func NewSyncLogsService

func NewSyncLogsService(ctx context.Context, logger Logger, readers ...*io.PipeReader) *SyncLogsService

func (*SyncLogsService) Close

func (s *SyncLogsService) Close()

func (*SyncLogsService) Ping

func (s *SyncLogsService) Ping(ctx context.Context) bool

func (*SyncLogsService) RunLogsLoops

func (s *SyncLogsService) RunLogsLoops()

type SyncService

type SyncService interface {
	Close()
	RunLogsLoops()
	Ping(context.Context) bool
}

type ZapLogger

type ZapLogger struct {
	Configs *Configs
	// contains filtered or unexported fields
}

func NewZapLogger

func NewZapLogger(options ...func(*ZapLogger) error) *ZapLogger

func (*ZapLogger) Close

func (l *ZapLogger) Close()

func (*ZapLogger) Connect

func (l *ZapLogger) Connect()

func (*ZapLogger) ErrorEndpoint

func (l *ZapLogger) ErrorEndpoint(log *EndpointLog)

func (*ZapLogger) ErrorService

func (l *ZapLogger) ErrorService(log *ServiceLog)

func (*ZapLogger) FatalEndpoint

func (l *ZapLogger) FatalEndpoint(log *EndpointLog)

func (*ZapLogger) FatalService

func (l *ZapLogger) FatalService(log *ServiceLog)

func (*ZapLogger) GetConfigs

func (l *ZapLogger) GetConfigs() *Configs

func (*ZapLogger) GetZapNativeLogger

func (l *ZapLogger) GetZapNativeLogger() *zap.Logger

func (*ZapLogger) InfoEndpoint

func (l *ZapLogger) InfoEndpoint(log *EndpointLog)

func (*ZapLogger) InfoService

func (l *ZapLogger) InfoService(log *ServiceLog)

func (*ZapLogger) PanicEndpoint

func (l *ZapLogger) PanicEndpoint(log *EndpointLog)

func (*ZapLogger) PanicService

func (l *ZapLogger) PanicService(log *ServiceLog)

func (*ZapLogger) Ping

func (l *ZapLogger) Ping(ctx context.Context) bool

func (*ZapLogger) WarnEndpoint

func (l *ZapLogger) WarnEndpoint(log *EndpointLog)

func (*ZapLogger) WarnService

func (l *ZapLogger) WarnService(log *ServiceLog)

Jump to

Keyboard shortcuts

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