logx

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 25 Imported by: 8

Documentation

Overview

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Note: 这里debug,info,stat,slow采用直接调用output的方式,而warn,error,stack采用封装调用的方式是特意设计的。 提取封装函数再调用能简化代码,但都采用封装调用的方式,很有可能条件不满足,大量的fmt.Sprint函数做无用功。

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Copyright 2022 GoFast Author(http://chende.ren). All rights reserved. Use of this source code is governed by a MIT license

Index

Constants

View Source
const (
	LogLevelDebug int8 = iota // LogLevelDebug logs [everything]
	LogLevelInfo              // LogLevelInfo logs [info, warn, error, stack]
	LogLevelWarn              // LogLevelError includes [warn, error, stack]
	LogLevelError             // LogLevelError includes [error, stack]
	LogLevelStack             // LogLevelError includes [stack]
)

日志级别的设定,自动显示对应级别的日志

View Source
const (
	LogStyleCustom int8 = iota
	LogStyleSdx
	LogStyleSdxJson
	LogStyleELK
	LogStylePrometheus
)

日志样式类型

Variables

View Source
var (
	LogStatSysMonitor  = &LogStat{Type: 1, Fields: []string{"cpu", "mem", "gor", "gc"}}
	LogStatRouteReq    = &LogStat{Type: 2, Fields: []string{"accept", "timeout", "drop", "qps", "ave", "max"}}
	LogStatCpuUsage    = &LogStat{Type: 3, Fields: []string{"cpu", "total", "pass", "drop"}}
	LogStatBreakerOpen = &LogStat{Type: 4, Fields: []string{}}
)

我们把系统收集统计信息日志的一些常量维护在这里

View Source
var CustomOutputFunc func(logLevel string, data any) string

如果采用Custom模式,必须外部指定下面这两个方法

View Source
var CustomReqLogFunc func(p *ReqLogEntity, flag int8) string

Functions

func CloseFiles

func CloseFiles() error

func Debug

func Debug(v string)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

func DebugDirect

func DebugDirect(v string)

func DebugF

func DebugF(format string, v ...any)

func Debugs

func Debugs(v ...any)

func Disable

func Disable()

func Error

func Error(v string)

+++

func ErrorF

func ErrorF(format string, v ...any)

func ErrorFatal

func ErrorFatal(v ...any)

func ErrorFatalF

func ErrorFatalF(format string, v ...any)

func Errors

func Errors(v ...any)

func Info

func Info(v string)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

func InfoDirect

func InfoDirect(v string)

直接打印所给的数据

func InfoF

func InfoF(format string, v ...any)

func InfoKV

func InfoKV(v cst.KV)

func InfoReport

func InfoReport(kv cst.KV)

func Infos

func Infos(v ...any)

func MustSetup

func MustSetup(cnf *LogConfig)

必须准备好日志环境,否则启动失败自动退出

func NewLessWriter

func NewLessWriter(writer io.Writer, milliseconds int) *lessWriter

func RequestsLog

func RequestsLog(p *ReqLogEntity, flag int8)

打印请求日志,可以指定不同的输出样式

func ShowDebug

func ShowDebug() bool

func ShowError

func ShowError() bool

func ShowInfo

func ShowInfo() bool

func ShowStack

func ShowStack() bool

func ShowStat

func ShowStat() bool

func ShowWarn

func ShowWarn() bool

func Slow

func Slow(v string)

+++

func SlowF

func SlowF(format string, v ...any)

func Slows

func Slows(v ...any)

func Stack

func Stack(v string)

+++

func StackF

func StackF(format string, v ...any)

func Stacks

func Stacks(v ...any)

func Stat

func Stat(v string)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

func StatF

func StatF(format string, v ...any)

func StatKV

func StatKV(data cst.KV)

func Stats

func Stats(v ...any)

func Timer added in v0.4.3

func Timer(v string)

+++

func TimerError added in v0.4.3

func TimerError(v string)

func TimerF added in v0.4.3

func TimerF(format string, v ...any)

func TimerKV added in v0.4.3

func TimerKV(data cst.KV)

func Timers added in v0.4.3

func Timers(v ...any)

func Warn

func Warn(v string)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

func WarnF

func WarnF(format string, v ...any)

func Warns

func Warns(v ...any)

Types

type DailyRotateRule

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

func (*DailyRotateRule) ArchiveFileName

func (r *DailyRotateRule) ArchiveFileName() string

func (*DailyRotateRule) MarkRotated

func (r *DailyRotateRule) MarkRotated()

func (*DailyRotateRule) NeedRotate

func (r *DailyRotateRule) NeedRotate() bool

func (*DailyRotateRule) OutdatedFiles

func (r *DailyRotateRule) OutdatedFiles() []string

过期的文件列表

type LogConfig

type LogConfig struct {
	AppName   string `v:"def=AppName"`                                     // 应用名称
	ServerNo  string `v:"def=ServerNo"`                                    // 运行终端编号
	LogMedium string `v:"def=console,enum=console|file|volume"`            // 显示媒介
	LogLevel  string `v:"def=info,enum=debug|info|warn|error|stack"`       // 记录日志的级别
	LogStyle  string `v:"def=sdx,enum=custom|sdx|sdx-json|elk|prometheus"` // 日志样式
	LogStats  bool   `v:"def=true"`                                        // 是否打印统计信息

	FileFolder string `v:""`                    // 日志文件夹路径
	FilePrefix string `v:""`                    // 日志文件名统一前缀(默认是AppName)
	FileSplit  uint16 `v:"def=0,range=[0:255]"` // 日志拆分(比如32: info+stat; 64: info+timer; 160: info+stat+timer)

	FileKeepDays int  `v:"def=7"`     // 日志文件保留天数
	FileGzip     bool `v:"def=false"` // 是否Gzip压缩日志文件
	// contains filtered or unexported fields
}

type LogStat

type LogStat struct {
	Type   uint8
	Fields []string
}

type ReqLogEntity

type ReqLogEntity struct {
	RawReq     *http.Request
	TimeStamp  time.Duration
	Latency    time.Duration
	ClientIP   string
	StatusCode int
	Pms        cst.KV
	BodySize   int
	ResData    []byte
	CarryItems tips.CarryList
}

日志参数实体

type RotateLogger

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

func NewRotateLogger

func NewRotateLogger(filename string, rule RotateRule, compress bool) (*RotateLogger, error)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 自动归档日志系统

func (*RotateLogger) Close

func (rl *RotateLogger) Close() error

func (*RotateLogger) Write

func (rl *RotateLogger) Write(bytes []byte) (int, error)

func (*RotateLogger) Writeln

func (rl *RotateLogger) Writeln(str string) (err error)

每次调用都会在 str 后面自动判断并加上 \n

func (*RotateLogger) WritelnBuilder

func (rl *RotateLogger) WritelnBuilder(sb *strings.Builder) (err error)

func (*RotateLogger) WritelnBytes

func (rl *RotateLogger) WritelnBytes(bytes []byte) (err error)

type RotateRule

type RotateRule interface {
	ArchiveFileName() string
	OutdatedFiles() []string
	NeedRotate() bool
	MarkRotated()
}

func DefDailyRotateRule

func DefDailyRotateRule(filename, delimiter string, days int, gzip bool) RotateRule

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

type WriterCloser

type WriterCloser interface {
	io.Writer
	io.Closer
	Writeln(data string) (err error)
	WritelnBytes(data []byte) (err error)
	WritelnBuilder(sb *strings.Builder) (err error)
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Jump to

Keyboard shortcuts

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