Documentation ¶
Index ¶
- Constants
- Variables
- func NewConverter(messageKey, levelKey string, levelMapping LevelMapping, ...) *converter
- func NewService(bufferSize int, interval time.Duration, flush func(...Message) error) *service
- func NewWriter(uri *url.URL, topic, source, accessKey string, accessSecret Secret, ...) *writer
- type AliyunError
- type Config
- type ContentModifier
- type ContentModifierFunc
- type Converter
- type Hook
- type LevelMapping
- type Message
- type Secret
- type Service
- type Writer
Examples ¶
Constants ¶
View Source
const ( DefaultBufferSize = 100 DefaultMessageKey = "message" DefaultLevelKey = "level" DefaultTimeout = 500 * time.Millisecond DefaultInterval = 3 * time.Second )
Variables ¶
View Source
var ( // levels >= info will be hooked DefaultVisibleLevels = []logrus.Level{ logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel, logrus.InfoLevel, } // Mapping to syslog level SyslogLevelMapping = func() LevelMapping { m := [7]int{0, 2, 3, 4, 6, 7, 8} return func(level logrus.Level) int { return m[level] } }() )
Functions ¶
func NewConverter ¶
func NewConverter(messageKey, levelKey string, levelMapping LevelMapping, extra map[string]string, modifier ContentModifier, ) *converter
func NewService ¶
Types ¶
type AliyunError ¶
type AliyunError struct { HTTPCode int32 `json:"-"` Code string `json:"errorCode"` Message string `json:"errorMessage"` RequestID string `json:"-"` }
func (AliyunError) Error ¶
func (a AliyunError) Error() string
type Config ¶
type Config struct { // 阿里云日志接入地址, 格式: "<region>.log.aliyuncs.com", // 例如: "cn-hangzhou-intranet.log.aliyuncs.com", // 更多接入点参考: https://help.aliyun.com/document_detail/29008.html?spm=a2c4g.11174283.6.1118.292a1caaVMpfPu Endpoint string AccessKey string // 密钥对: key AccessSecret string // 密钥对: secret Project string // 日志项目名称 Store string // 日志库名称 Topic string // 日志 __topic__ 字段 Source string // 日志 __source__ 字段, 可选, 默认为 hostname Extra map[string]string // 日志附加字段, 可选 BufferSize int // 本地缓存日志条数, 可选, 默认为 100 Timeout time.Duration // 写缓存最大等待时间, 可选, 默认为 500ms Interval time.Duration // 缓存刷新间隔, 可选, 默认为 3s MessageKey string // 日志 Message 字段映射, 可选, 默认为 "message" LevelKey string // 日志 Level 字段映射, 可选, 默认为 "level" LevelMapping LevelMapping // 日志 Level 内容映射, 可选, 默认按照 syslog 规则映射 VisibleLevels []logrus.Level // 日志推送 Level, 可选, 默认推送 level >= info 的日志 HttpClient *http.Client // HTTP 客户端, 可选, 默认为 DefaultClient ContentModifier ContentModifier // 在发送前编辑日志内容, 可选, 默认为空 // contains filtered or unexported fields }
日志配置
type ContentModifier ¶ added in v1.1.0
type ContentModifierFunc ¶ added in v1.1.0
func (ContentModifierFunc) Modify ¶ added in v1.1.0
func (f ContentModifierFunc) Modify(contents map[string]string)
type Hook ¶
type Hook struct {
// contains filtered or unexported fields
}
Example ¶
hook, err := New(Config{ Endpoint: os.Getenv("ENDPOINT"), AccessKey: os.Getenv("ACCESS_KEY"), AccessSecret: os.Getenv("ACCESS_SECRET"), Project: os.Getenv("PROJECT"), Store: os.Getenv("STORE"), Topic: "demo", Extra: map[string]string{"service": "demo"}, }) if err != nil { panic(err) } logrus.SetLevel(logrus.TraceLevel) logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true}) logrus.AddHook(hook) time.AfterFunc(5*time.Second, func() { _ = hook.Close() }) for i := 0; i < 5; i++ { logrus.WithField("n", i).Info("Hi!") time.Sleep(time.Duration(rand.Intn(3) * int(time.Second))) }
Output:
Click to show internal directories.
Click to hide internal directories.