hook

package module
v0.0.0-...-2fe1d6f Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2019 License: MIT Imports: 24 Imported by: 2

README

Logrus Hook for Aliyun SLS

Build Status

Simple yet powerful logrus hook for aliyun simple logging service.

Features

  • Batch sending logs asynchronously
  • Dump huge logs (exceeds sls service limit) to stdout
  • Fallback dumping logs to stdout when sls api not available

Getting Start

Simply add a hook to global logrus instance or any logrus instance.

slsLogrusHook, err := hook.NewSlsLogrusHook("<project>.<region>.log.aliyuncs.com", "access_key", "access_secret", "logstore", "topic")
logrus.AddHook(slsLogrusHook)

Ensure logs are flushed to sls before program exits

slsLogrusHook.Flush(5 * time.Second)

Performance Tuning

Disable processing logs for default output.

logrus.SetFormatter(&hook.NoopFormatter{})
logrus.SetOutput(ioutil.Discard)

Disable locks.

logrus.StandardLogger().SetNoLock()

Setting send interval if necessary.

slsLogrusHook.SetSendInterval(100 * time.Millisecond) // defaults to 300 * time.Millisecond

Contributing

This project welcomes contributions from the community. Contributions are accepted using GitHub pull requests. If you're not familiar with making GitHub pull requests, please refer to the GitHub documentation "Creating a pull request".

Documentation

Overview

Package sls is a generated protocol buffer package.

It is generated from these files:

log.proto

It has these top-level messages:

LogContent
Log
LogTag
LogGroup
SlsLogPackage
SlsLogPackageList
LogGroupList

Index

Constants

View Source
const (
	DefaultTimeout  = 5 * time.Second
	MaxLogItemSize  = 512 * 1024      // Safe value for maximum 1M log item.
	MaxLogGroupSize = 4 * 1024 * 1024 // Safe value for maximum 5M log group
	MaxLogBatchSize = 1024            // Safe value for batch send size
)

Default config for sls client

View Source
const (
	SlsVersion               = "0.6.0"
	SlsSignatureMethod       = "hmac-sha1"
	HeaderAuthorization      = "Authorization"
	HeaderContentType        = "Content-Type"
	HeaderContentLength      = "Content-Length"
	HeaderContentMd5         = "Content-MD5"
	HeaderDate               = "Date"
	HeaderHost               = "Host"
	HeaderLogVersion         = "x-log-apiversion"
	HeaderLogSignatureMethod = "x-log-signaturemethod"
	HeaderLogBodyRawSize     = "x-log-bodyrawsize"
)

Sls constants

View Source
const (
	BufferSize          = 4096
	DefaultSendInterval = 300 * time.Millisecond
	MaxBatchSize        = 300
)

Default config for sls logrus hooks

Variables

View Source
var (
	ErrInvalidLengthLog = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowLog   = fmt.Errorf("proto: integer overflow")
)

Functions

func APISign

func APISign(secret string, method string, headers map[string]string, resource string) string

APISign Create signature for sls api

Types

type Config

type Config struct {
	Endpoint     string
	AccessKey    string
	AccessSecret string
	LogStore     string
	Topic        string
	Timeout      time.Duration
}

type Log

type Log struct {
	Time             *uint32       `protobuf:"varint,1,req,name=Time" json:"Time,omitempty"`
	Contents         []*LogContent `protobuf:"bytes,2,rep,name=Contents" json:"Contents,omitempty"`
	XXX_unrecognized []byte        `json:"-"`
}

func (*Log) Descriptor

func (*Log) Descriptor() ([]byte, []int)

func (*Log) GetContents

func (m *Log) GetContents() []*LogContent

func (*Log) GetTime

func (m *Log) GetTime() uint32

func (*Log) Marshal

func (m *Log) Marshal() (dAtA []byte, err error)

func (*Log) MarshalTo

func (m *Log) MarshalTo(dAtA []byte) (int, error)

func (*Log) ProtoMessage

func (*Log) ProtoMessage()

func (*Log) Reset

func (m *Log) Reset()

func (*Log) Size

func (m *Log) Size() (n int)

func (*Log) String

func (m *Log) String() string

func (*Log) Unmarshal

func (m *Log) Unmarshal(dAtA []byte) error

type LogContent

type LogContent struct {
	Key              *string `protobuf:"bytes,1,req,name=Key" json:"Key,omitempty"`
	Value            *string `protobuf:"bytes,2,req,name=Value" json:"Value,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*LogContent) Descriptor

func (*LogContent) Descriptor() ([]byte, []int)

func (*LogContent) GetKey

func (m *LogContent) GetKey() string

func (*LogContent) GetValue

func (m *LogContent) GetValue() string

func (*LogContent) Marshal

func (m *LogContent) Marshal() (dAtA []byte, err error)

func (*LogContent) MarshalTo

func (m *LogContent) MarshalTo(dAtA []byte) (int, error)

func (*LogContent) ProtoMessage

func (*LogContent) ProtoMessage()

func (*LogContent) Reset

func (m *LogContent) Reset()

func (*LogContent) Size

func (m *LogContent) Size() (n int)

func (*LogContent) String

func (m *LogContent) String() string

func (*LogContent) Unmarshal

func (m *LogContent) Unmarshal(dAtA []byte) error

type LogGroup

type LogGroup struct {
	Logs             []*Log    `protobuf:"bytes,1,rep,name=Logs" json:"Logs,omitempty"`
	Category         *string   `protobuf:"bytes,2,opt,name=Category" json:"Category,omitempty"`
	Topic            *string   `protobuf:"bytes,3,opt,name=Topic" json:"Topic,omitempty"`
	Source           *string   `protobuf:"bytes,4,opt,name=Source" json:"Source,omitempty"`
	MachineUUID      *string   `protobuf:"bytes,5,opt,name=MachineUUID" json:"MachineUUID,omitempty"`
	LogTags          []*LogTag `protobuf:"bytes,6,rep,name=LogTags" json:"LogTags,omitempty"`
	XXX_unrecognized []byte    `json:"-"`
}

func (*LogGroup) Descriptor

func (*LogGroup) Descriptor() ([]byte, []int)

func (*LogGroup) GetCategory

func (m *LogGroup) GetCategory() string

func (*LogGroup) GetLogTags

func (m *LogGroup) GetLogTags() []*LogTag

func (*LogGroup) GetLogs

func (m *LogGroup) GetLogs() []*Log

func (*LogGroup) GetMachineUUID

func (m *LogGroup) GetMachineUUID() string

func (*LogGroup) GetSource

func (m *LogGroup) GetSource() string

func (*LogGroup) GetTopic

func (m *LogGroup) GetTopic() string

func (*LogGroup) Marshal

func (m *LogGroup) Marshal() (dAtA []byte, err error)

func (*LogGroup) MarshalTo

func (m *LogGroup) MarshalTo(dAtA []byte) (int, error)

func (*LogGroup) ProtoMessage

func (*LogGroup) ProtoMessage()

func (*LogGroup) Reset

func (m *LogGroup) Reset()

func (*LogGroup) Size

func (m *LogGroup) Size() (n int)

func (*LogGroup) String

func (m *LogGroup) String() string

func (*LogGroup) Unmarshal

func (m *LogGroup) Unmarshal(dAtA []byte) error

type LogGroupList

type LogGroupList struct {
	LogGroups        []*LogGroup `protobuf:"bytes,1,rep,name=LogGroups" json:"LogGroups,omitempty"`
	XXX_unrecognized []byte      `json:"-"`
}

func (*LogGroupList) Descriptor

func (*LogGroupList) Descriptor() ([]byte, []int)

func (*LogGroupList) GetLogGroups

func (m *LogGroupList) GetLogGroups() []*LogGroup

func (*LogGroupList) Marshal

func (m *LogGroupList) Marshal() (dAtA []byte, err error)

func (*LogGroupList) MarshalTo

func (m *LogGroupList) MarshalTo(dAtA []byte) (int, error)

func (*LogGroupList) ProtoMessage

func (*LogGroupList) ProtoMessage()

func (*LogGroupList) Reset

func (m *LogGroupList) Reset()

func (*LogGroupList) Size

func (m *LogGroupList) Size() (n int)

func (*LogGroupList) String

func (m *LogGroupList) String() string

func (*LogGroupList) Unmarshal

func (m *LogGroupList) Unmarshal(dAtA []byte) error

type LogTag

type LogTag struct {
	Key              *string `protobuf:"bytes,1,req,name=Key" json:"Key,omitempty"`
	Value            *string `protobuf:"bytes,2,req,name=Value" json:"Value,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*LogTag) Descriptor

func (*LogTag) Descriptor() ([]byte, []int)

func (*LogTag) GetKey

func (m *LogTag) GetKey() string

func (*LogTag) GetValue

func (m *LogTag) GetValue() string

func (*LogTag) Marshal

func (m *LogTag) Marshal() (dAtA []byte, err error)

func (*LogTag) MarshalTo

func (m *LogTag) MarshalTo(dAtA []byte) (int, error)

func (*LogTag) ProtoMessage

func (*LogTag) ProtoMessage()

func (*LogTag) Reset

func (m *LogTag) Reset()

func (*LogTag) Size

func (m *LogTag) Size() (n int)

func (*LogTag) String

func (m *LogTag) String() string

func (*LogTag) Unmarshal

func (m *LogTag) Unmarshal(dAtA []byte) error

type NoopFormatter

type NoopFormatter struct{}

NoopFormatter is a no-op logrus formatter

func (*NoopFormatter) Format

func (*NoopFormatter) Format(*logrus.Entry) ([]byte, error)

Format implements logrus formatter interface

type SlsClient

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

SlsClient the client struct for sls connection

func NewSlsClient

func NewSlsClient(config *Config) (*SlsClient, error)

NewSlsClient create a new sls client

func (*SlsClient) Ping

func (client *SlsClient) Ping() error

Ping sls api auth & connection

func (*SlsClient) SendLogs

func (client *SlsClient) SendLogs(logs []*Log) error

SendLogs using sls api & handle extreme cases

type SlsLogPackage

type SlsLogPackage struct {
	Data             []byte `protobuf:"bytes,1,req,name=data" json:"data,omitempty"`
	UncompressSize   *int32 `protobuf:"varint,2,opt,name=uncompress_size,json=uncompressSize" json:"uncompress_size,omitempty"`
	XXX_unrecognized []byte `json:"-"`
}

func (*SlsLogPackage) Descriptor

func (*SlsLogPackage) Descriptor() ([]byte, []int)

func (*SlsLogPackage) GetData

func (m *SlsLogPackage) GetData() []byte

func (*SlsLogPackage) GetUncompressSize

func (m *SlsLogPackage) GetUncompressSize() int32

func (*SlsLogPackage) Marshal

func (m *SlsLogPackage) Marshal() (dAtA []byte, err error)

func (*SlsLogPackage) MarshalTo

func (m *SlsLogPackage) MarshalTo(dAtA []byte) (int, error)

func (*SlsLogPackage) ProtoMessage

func (*SlsLogPackage) ProtoMessage()

func (*SlsLogPackage) Reset

func (m *SlsLogPackage) Reset()

func (*SlsLogPackage) Size

func (m *SlsLogPackage) Size() (n int)

func (*SlsLogPackage) String

func (m *SlsLogPackage) String() string

func (*SlsLogPackage) Unmarshal

func (m *SlsLogPackage) Unmarshal(dAtA []byte) error

type SlsLogPackageList

type SlsLogPackageList struct {
	Packages         []*SlsLogPackage `protobuf:"bytes,1,rep,name=packages" json:"packages,omitempty"`
	XXX_unrecognized []byte           `json:"-"`
}

func (*SlsLogPackageList) Descriptor

func (*SlsLogPackageList) Descriptor() ([]byte, []int)

func (*SlsLogPackageList) GetPackages

func (m *SlsLogPackageList) GetPackages() []*SlsLogPackage

func (*SlsLogPackageList) Marshal

func (m *SlsLogPackageList) Marshal() (dAtA []byte, err error)

func (*SlsLogPackageList) MarshalTo

func (m *SlsLogPackageList) MarshalTo(dAtA []byte) (int, error)

func (*SlsLogPackageList) ProtoMessage

func (*SlsLogPackageList) ProtoMessage()

func (*SlsLogPackageList) Reset

func (m *SlsLogPackageList) Reset()

func (*SlsLogPackageList) Size

func (m *SlsLogPackageList) Size() (n int)

func (*SlsLogPackageList) String

func (m *SlsLogPackageList) String() string

func (*SlsLogPackageList) Unmarshal

func (m *SlsLogPackageList) Unmarshal(dAtA []byte) error

type SlsLogrusHook

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

SlsLogrusHook logrus hook for sls

func New

func New(config *Config) (*SlsLogrusHook, error)

func NewSlsLogrusHook

func NewSlsLogrusHook(endpoint string, accessKey string, accessSecret string, logStore string, topic string) (*SlsLogrusHook, error)

NewSlsLogrusHook create logrus hook

func (*SlsLogrusHook) Fire

func (hook *SlsLogrusHook) Fire(entry *logrus.Entry) error

Fire implement logrus Hook interface

func (*SlsLogrusHook) Flush

func (hook *SlsLogrusHook) Flush(timeout time.Duration)

Flush ensure logs are flush through sls api

func (*SlsLogrusHook) Levels

func (hook *SlsLogrusHook) Levels() []logrus.Level

Levels implement logrus Hook interface

func (*SlsLogrusHook) SetSendInterval

func (hook *SlsLogrusHook) SetSendInterval(interval time.Duration)

SetSendInterval change batch send interval

Jump to

Keyboard shortcuts

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