logger

package
v0.0.0-...-0409f2c Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Lg *zap.Logger
)

Functions

func Debug

func Debug(msg string, fields ...zap.Field)

Debug 通用 debug 日志方法

func Error

func Error(msg string, fields ...zap.Field)

Error 通用 error 日志方法

func Fatal

func Fatal(msg string, fields ...zap.Field)

Fatal 通用 fatal 日志方法

func GetDailyLogFilename

func GetDailyLogFilename(baseFilename string) string

GetDailyLogFilename 获取按日期分割的日志文件名

func Info

func Info(msg string, fields ...zap.Field)

Info 通用 info 日志方法

func Init

func Init(cfg *LogConfig, mode string) (err error)

Init 初始化lg(兼容旧版本)

func InitWithAlert

func InitWithAlert(cfg *LogConfig, mode string, notifiers []NotificationService) (err error)

InitWithAlert 初始化logger和预警功能

func IsAlertEnabled

func IsAlertEnabled() bool

IsAlertEnabled 检查预警功能是否启用

func LogAccess

func LogAccess(method, path, clientIP string, statusCode int, latency int64)

LogAccess 记录 HTTP 请求访问日志

func LogConfigLoaded

func LogConfigLoaded(configPath string)

LogConfigLoaded 记录配置加载完成日志

func LogDatabaseConnected

func LogDatabaseConnected(driver, dsn string)

LogDatabaseConnected 记录数据库连接成功日志

func LogError

func LogError(msg string, fields ...zap.Field)

LogError 记录错误日志(自动包含调用堆栈)

func LogServerConfig

func LogServerConfig(addr, dbDriver, dsn, mode, logLevel, logFilename string, logMaxSize, logMaxAge, logMaxBackups int)

LogServerConfig 记录服务器配置日志

func LogStartupSuccess

func LogStartupSuccess(addr string)

LogStartupSuccess 记录服务启动成功日志

func LogTaskStarted

func LogTaskStarted(taskName string)

LogTaskStarted 记录定时任务启动日志

func Panic

func Panic(msg string, fields ...zap.Field)

Panic 通用 panic 日志方法

func ResetAlertStats

func ResetAlertStats()

ResetAlertStats 重置预警统计

func Sync

func Sync()

Sync 刷新缓冲区

func Warn

func Warn(msg string, fields ...zap.Field)

Warn 通用 warn 日志方法

Types

type AlertConfig

type AlertConfig struct {
	Enabled           bool          `mapstructure:"enabled"`            // 是否启用预警
	ErrorThreshold    int           `mapstructure:"error_threshold"`    // 错误日志阈值
	WarningThreshold  int           `mapstructure:"warning_threshold"`  // 警告日志阈值
	TimeWindow        time.Duration `mapstructure:"time_window"`        // 时间窗口
	CooldownPeriod    time.Duration `mapstructure:"cooldown_period"`    // 冷却期
	AdminEmails       []string      `mapstructure:"admin_emails"`       // 管理员邮箱列表
	AdminPhones       []string      `mapstructure:"admin_phones"`       // 管理员手机号列表
	AdminUserIDs      []uint        `mapstructure:"admin_user_ids"`     // 管理员用户ID列表
	NotificationTypes []string      `mapstructure:"notification_types"` // 通知类型: email, sms, internal
}

AlertConfig 预警配置

type AlertInfo

type AlertInfo struct {
	Type       string            `json:"type"`        // 预警类型: error, warning
	Count      int               `json:"count"`       // 日志数量
	Threshold  int               `json:"threshold"`   // 阈值
	TimeWindow time.Duration     `json:"time_window"` // 时间窗口
	StartTime  time.Time         `json:"start_time"`  // 开始时间
	EndTime    time.Time         `json:"end_time"`    // 结束时间
	SampleLogs []LogEntry        `json:"sample_logs"` // 示例日志
	Metadata   map[string]string `json:"metadata"`    // 额外元数据
}

AlertInfo 预警信息

type AlertManager

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

AlertManager 预警管理器

func NewAlertManager

func NewAlertManager(config *AlertConfig, notifiers ...NotificationService) *AlertManager

NewAlertManager 创建预警管理器

func (*AlertManager) CheckAndAlert

func (am *AlertManager) CheckAndAlert(level zapcore.Level, entry zapcore.Entry, fields []zap.Field)

CheckAndAlert 检查并发送预警

func (*AlertManager) GetStats

func (am *AlertManager) GetStats() *AlertStats

GetStats 获取预警统计

func (*AlertManager) ResetStats

func (am *AlertManager) ResetStats()

ResetStats 重置统计

type AlertStats

type AlertStats struct {
	ErrorCount   int       `json:"error_count"`
	WarningCount int       `json:"warning_count"`
	LastAlert    time.Time `json:"last_alert"`
	WindowStart  time.Time `json:"window_start"`
}

AlertStats 预警统计

func GetAlertStats

func GetAlertStats() *AlertStats

GetAlertStats 获取预警统计信息

type LogConfig

type LogConfig struct {
	Level      string       `mapstructure:"level"`
	Filename   string       `mapstructure:"filename"`
	MaxSize    int          `mapstructure:"max_size"`
	MaxAge     int          `mapstructure:"max_age"`
	MaxBackups int          `mapstructure:"max_backups"`
	Daily      bool         `mapstructure:"daily"`
	Alert      *AlertConfig `mapstructure:"alert"`
}

type LogEntry

type LogEntry struct {
	Level     string                 `json:"level"`
	Message   string                 `json:"message"`
	Timestamp time.Time              `json:"timestamp"`
	Caller    string                 `json:"caller"`
	Fields    map[string]interface{} `json:"fields"`
}

LogEntry 日志条目

type NotificationService

type NotificationService interface {
	SendAlert(ctx context.Context, alert *AlertInfo) error
}

NotificationService 通知服务接口

Jump to

Keyboard shortcuts

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