glog

package
v1.2.16 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

* @Author: morehao morehao@qq.com * @Date: 2025-04-26 09:55:22 * @LastEditors: morehao morehao@qq.com * @LastEditTime: 2025-04-26 16:50:59 * @FilePath: /golib/glog/config.go * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE

* @Author: morehao morehao@qq.com * @Date: 2025-04-26 19:13:30 * @LastEditors: morehao morehao@qq.com * @LastEditTime: 2025-04-27 15:20:49 * @FilePath: /golib/glog/logger.go * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE

Index

Constants

View Source
const (
	KeyLogger = "logger"

	KeyRequestId  = "requestId"
	KeyTraceId    = "traceId"
	KeyTraceFlags = "traceFlags"
	KeySpanId     = "spanId"

	MsgFlagNotice = "notice"

	KeySkipLog          = "skip"
	KeyService          = "service"
	KeyHost             = "host"
	KeyClientIp         = "clientIp"
	KeyHandle           = "handle"
	KeyProto            = "proto"
	KeyRefer            = "refer"
	KeyUserAgent        = "userAgent"
	KeyHeader           = "header"
	KeyCookie           = "cookie"
	KeyUri              = "uri"
	KeyMethod           = "method"
	KeyHttpStatusCode   = "httpStatusCode"
	KeyRequestQuery     = "requestQuery"
	KeyRequestBody      = "requestBody"
	KeyRequestBodySize  = "requestBodySize"
	KeyResponseCode     = "responseCode"
	KeyResponseBody     = "responseBody"
	KeyResponseBodySize = "responseBodySize"
	KeyRequestStartTime = "start"
	KeyRequestEndTime   = "end"
	KeyCost             = "cost"
	KeyRequestErr       = "requestErr"
	KeyErrorCode        = "errorCode"
	KeyErrorMsg         = "errorMsg"
	KeyAffectedRows     = "affectedRows"
	KeyAddr             = "addr"
	KeyDatabase         = "database"
	KeySql              = "sql"
	KeyCmd              = "cmd"
	KeyCmdContent       = "cmdContent"
	KeyRalCode          = "ralCode"
	KeyFile             = "file"
	KeyDsl              = "dsl"
	KeyDslMethod        = "dslMethod"
	KeyDslPath          = "dslPath"

	ValueProtoHttp  = "gresty"
	ValueProtoMysql = "mysql"
	ValueProtoRedis = "redis"
	ValueProtoES    = "es"
)

Variables

This section is empty.

Functions

func Close

func Close()

Close 关闭所有logger

func Debug

func Debug(ctx context.Context, args ...any)

func Debugf

func Debugf(ctx context.Context, format string, kvs ...any)

func Debugw

func Debugw(ctx context.Context, msg string, kvs ...any)

func Error

func Error(ctx context.Context, args ...any)

func Errorf

func Errorf(ctx context.Context, format string, kvs ...any)

func Errorw

func Errorw(ctx context.Context, msg string, kvs ...any)

func Fatal

func Fatal(ctx context.Context, args ...any)

func Fatalf

func Fatalf(ctx context.Context, format string, kvs ...any)

func Fatalw

func Fatalw(ctx context.Context, msg string, kvs ...any)

func FormatRequestTime

func FormatRequestTime(time time.Time) string

func GenRequestID

func GenRequestID() string

GenRequestID 生成requestId

func GetRequestCost

func GetRequestCost(start, end time.Time) float64

func Info

func Info(ctx context.Context, args ...any)

func Infof

func Infof(ctx context.Context, format string, kvs ...any)

func Infow

func Infow(ctx context.Context, msg string, kvs ...any)

func InitLogger

func InitLogger(cfg *LogConfig, opts ...Option) error

InitLogger 初始化日志系统

func Panic

func Panic(ctx context.Context, args ...any)

func Panicf

func Panicf(ctx context.Context, format string, kvs ...any)

func Panicw

func Panicw(ctx context.Context, msg string, kvs ...any)

func ToJsonString

func ToJsonString(v any) string

func Warn

func Warn(ctx context.Context, args ...any)

func Warnf

func Warnf(ctx context.Context, format string, kvs ...any)

func Warnw

func Warnw(ctx context.Context, msg string, kvs ...any)

Types

type Field

type Field struct {
	Key   string
	Value any
}

func KV

func KV(key string, value any) Field

type FieldHookFunc

type FieldHookFunc func(fields []Field)

FieldHookFunc 字段钩子函数类型

type Level

type Level string
const (
	DebugLevel Level = "debug"
	InfoLevel  Level = "info"
	WarnLevel  Level = "warn"
	ErrorLevel Level = "error"
	PanicLevel Level = "panic"
	FatalLevel Level = "fatal"
)

type LogConfig

type LogConfig struct {
	// Service 服务名
	Service string
	// Module 模块名称,如 "es", "gorm", "redis" 等
	Module string
	// Level 日志级别
	Level Level `json:"level" yaml:"level"`
	// Writer 日志输出类型
	Writer WriterType `json:"writer" yaml:"writer"`
	// RotateInterval 日志切割周期,单位为天
	RotateInterval RotateIntervalType `json:"rotate_interval" yaml:"rotate_interval"`
	// Dir 日志文件目录
	Dir string `json:"dir" yaml:"dir"`
	// ExtraKeys 需要从上下文中提取的额外字段
	ExtraKeys []string `json:"extra_keys" yaml:"extra_keys"`
	// RotateUnit 日志切割的时间单位
	RotateUnit RotateUnit `json:"rotate_unit" yaml:"rotate_unit"`
}

LogConfig 模块级别的日志配置

func GetDefaultLogConfig

func GetDefaultLogConfig() *LogConfig

func GetLoggerConfig

func GetLoggerConfig() *LogConfig

type Logger

type Logger interface {
	Debug(ctx context.Context, args ...any)
	Debugf(ctx context.Context, format string, kvs ...any)
	Debugw(ctx context.Context, msg string, kvs ...any)
	Info(ctx context.Context, args ...any)
	Infof(ctx context.Context, format string, kvs ...any)
	Infow(ctx context.Context, msg string, kvs ...any)
	Warn(ctx context.Context, args ...any)
	Warnf(ctx context.Context, format string, kvs ...any)
	Warnw(ctx context.Context, msg string, kvs ...any)
	Error(ctx context.Context, args ...any)
	Errorf(ctx context.Context, format string, kvs ...any)
	Errorw(ctx context.Context, msg string, kvs ...any)
	Panic(ctx context.Context, args ...any)
	Panicf(ctx context.Context, format string, kvs ...any)
	Panicw(ctx context.Context, msg string, kvs ...any)
	Fatal(ctx context.Context, args ...any)
	Fatalf(ctx context.Context, format string, kvs ...any)
	Fatalw(ctx context.Context, msg string, kvs ...any)

	Close()
	// contains filtered or unexported methods
}

func GetDefaultLogger

func GetDefaultLogger() Logger

func GetLogger

func GetLogger(cfg *LogConfig, opts ...Option) (Logger, error)

type LoggerType

type LoggerType uint8
const (
	LoggerTypeZap LoggerType = iota + 1
)

type MessageHookFunc

type MessageHookFunc func(message string) string

MessageHookFunc 消息钩子函数类型

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option 日志选项

func WithCallerSkip

func WithCallerSkip(skip int) Option

WithCallerSkip 设置调用者跳过的层数

func WithFieldHookFunc

func WithFieldHookFunc(fn FieldHookFunc) Option

WithFieldHookFunc 设置字段钩子函数

func WithMessageHookFunc

func WithMessageHookFunc(fn MessageHookFunc) Option

WithMessageHookFunc 设置消息钩子函数

type RotateIntervalType

type RotateIntervalType string
const (
	RotateIntervalTypeHour RotateIntervalType = "hour"
	RotateIntervalTypeDay  RotateIntervalType = "day"
)

type RotateUnit

type RotateUnit string

RotateUnit 日志切割的时间单位

const (
	RotateUnitDay  RotateUnit = "day"
	RotateUnitHour RotateUnit = "hour"
)

type WriterType

type WriterType string
const (
	WriterConsole WriterType = "console"
	WriterFile    WriterType = "file"
)

Jump to

Keyboard shortcuts

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