logx

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelInfo    = "info"
	LevelWarn    = "warn"
	LevelError   = "error"
	MessageType  = "message_type"        //日志类型
	XtraceKey    = "trace_id"            //外部链路ID
	RequestIdKey = "request_id"          //链路ID
	TimeFormat   = "2006-01-02 15:04:05" //默认时间
)

Variables

View Source
var (
	HeaderAppName    = "app-name"
	KeyPath          = "path"
	KeyTitle         = "title"
	KeyOriginAppName = "origin_app_name"

	DefaultAppName               = "app"               // 默认应用名称
	DefaultAppMode               = "dev"               // 默认应用环境
	DefaultRunMode               = "debug"             // 默认运行环境
	DefaultAppVersion            = "1.0.0"             // 默认版本
	DefaultLogType               = "file"              // 默认日志类型
	DefaultLogPath               = "/home/logs/app/"   // 默认文件目录
	DefaultChildPath             = "/file-%s.log"      // 默认子目录
	DefaultRotationSize          = 32 * 1024 * 1024    // 默认大小为32M
	DefaultRotationCount         = 0                   // 默认不限制
	DefaultRotationTime          = 24 * time.Hour      // 默认每天轮转一次
	DefaultNoBuffWrite           = false               // 不不开启无缓冲写入
	DefaultMaxAge                = 90 * 24 * time.Hour // 默认保留90天
	DefaultTraceType             = "zipkin"            //记录类型 zipkin 和 jaeger
	DefaultTraceAddr             = ""
	DefaultTraceMod      float64 = 0
)
View Source
var (
	Sugar *zap.Logger

	DefaultConfig = &Config{
		AppName:       DefaultAppName,
		AppMode:       DefaultAppMode,
		LogType:       DefaultLogType,
		LogPath:       DefaultLogPath,
		ChildPath:     DefaultChildPath,
		RotationSize:  int64(DefaultRotationSize),
		RotationCount: uint(DefaultRotationCount),
		NoBuffWrite:   DefaultNoBuffWrite,
		RotationTime:  DefaultRotationTime,
		MaxAge:        DefaultMaxAge,
	}
)

Functions

func Default

func Default(level logger.LogLevel) logger.Interface

func Error

func Error(template string, args ...interface{})

func ErrorF

func ErrorF(c *gin.Context, template string, args ...interface{})

func ErrorLogId

func ErrorLogId(logId, template string, args ...interface{})

func GetRequestIdKey

func GetRequestIdKey(c *gin.Context) (requestId string)

GetRequestIdKey 获取链路ID

func GetWriteSyncer

func GetWriteSyncer(file string, lc *Config) zapcore.WriteSyncer

GetWriteSyncer 按天切割按大小切割 filename 文件名 RotationSize 每个文件的大小 MaxAge 文件最大保留天数 RotationCount 最大保留文件个数 RotationTime 设置文件分割时间 RotationCount 设置保留的最大文件数量

func Info

func Info(template string, args ...interface{})

func InfoApi

func InfoApi(c *gin.Context, template string, args ...interface{})

func InfoDB

func InfoDB(c *gin.Context, template string, args ...interface{})

func InfoF

func InfoF(c *gin.Context, template string, args ...interface{})

func InfoLogId

func InfoLogId(logId, template string, args ...interface{})

func InfoSdk

func InfoSdk(c *gin.Context, template string, args ...interface{})

func InitLog

func InitLog(options *Config)

InitLog 初始化日志文件 logPath= /home/logs/app/appName/childPath

func String

func String(key string, value interface{}) zap.Field

func Warn

func Warn(template string, args ...interface{})

func WarnF

func WarnF(c *gin.Context, title string, template string, args ...interface{})

func WarnLogId

func WarnLogId(logId, template string, args ...interface{})

Types

type Config

type Config struct {
	AppName       string        `json:"app_name"`       // 应用名
	AppMode       string        `json:"app_mode"`       // 应用环境
	LogType       string        `json:"log_type"`       // 日志类型
	LogPath       string        `json:"log_path"`       // 日志主路径
	ChildPath     string        `json:"child_path"`     // 日志子路径+文件名
	RotationSize  int64         `json:"rotation_size"`  // 单个文件大小
	RotationCount uint          `json:"rotation_count"` // 可以保留的文件个数
	NoBuffWrite   bool          `json:"no_buff_write"`  // 设置无缓冲日志写入
	RotationTime  time.Duration `json:"rotation_time"`  // 日志分割的时间
	MaxAge        time.Duration `json:"max_age"`        // 日志最大保留的天数
}

type Field

type Field = zap.Field

type LogContext

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

LogContext logger

func NewLogContext

func NewLogContext(logId string) *LogContext

NewLogContext new obj

func (*LogContext) ErrorF

func (ctx *LogContext) ErrorF(template string, args ...interface{})

func (*LogContext) Field

func (ctx *LogContext) Field(key string, value interface{}) Field

func (*LogContext) GetClientIP

func (ctx *LogContext) GetClientIP() string

GetClientIP 得到clientIP

func (*LogContext) GetLogId

func (ctx *LogContext) GetLogId() string

GetLogId 得到LogId

func (*LogContext) InfoF

func (ctx *LogContext) InfoF(template string, args ...interface{})

func (*LogContext) WarnF

func (ctx *LogContext) WarnF(template string, args ...interface{})

type LogOptionFunc

type LogOptionFunc func(*Config)

func SetLogAppMode

func SetLogAppMode(appMode string) LogOptionFunc

设置环境变量,标识当前应用运行的环境,默认值dev

func SetLogAppName

func SetLogAppName(appName string) LogOptionFunc

设置应用名称,默认值default-app

func SetLogChildPath

func SetLogChildPath(childPath string) LogOptionFunc

设置子目录—+文件名,保证一个类型的文件在同一个文件夹下面便于区分、默认值glogs/%Y-%m-%d.log

func SetLogMaxAge

func SetLogMaxAge(maxAge time.Duration) LogOptionFunc

SetLogMaxAge 设置文件最大保留时间、默认值7天

func SetLogMaxSize

func SetLogMaxSize(size int64) LogOptionFunc

设置单个文件最大值byte,默认值32M

func SetLogPath

func SetLogPath(logPath string) LogOptionFunc

设置日志目录,这个是主目录,程序会给此目录拼接上项目名,子目录以及文件,默认值/home/logs/app

func SetLogType

func SetLogType(logType string) LogOptionFunc

设置日志类型,日志类型目前分为2种,console和file,默认值file

func SetNoBuffWriter

func SetNoBuffWriter() LogOptionFunc

SetNoBuffWriter 设置无缓冲写入日志,比较消耗性能

func SetRotationCount

func SetRotationCount(n uint) LogOptionFunc

SetRotationCount 设置保留的最大文件数量、没有默认值(表示不限制)

func SetRotationTime

func SetRotationTime(rotationTime time.Duration) LogOptionFunc

SetRotationTime 设置文件分割时间、默认值24*time.Hour(按天分割)

type LoggerContext

type LoggerContext interface {
	InfoF(template string, args ...interface{})
	WarnF(template string, args ...interface{})
	ErrorF(template string, args ...interface{})
	Field(key string, value interface{}) Field
}

LoggerContext 日志

Jump to

Keyboard shortcuts

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