Documentation
¶
Index ¶
- Constants
- func DetailedDebugLogger(storage Storage) gin.HandlerFunc
- func DisableMiddleware() gin.HandlerFunc
- func EnableMiddleware(storage Storage, config ...Config) gin.HandlerFunc
- func GenerateID() string
- func MiddlewareWithConfig(storage Storage, config Config) gin.HandlerFunc
- func RequestLogger(storage Storage) gin.HandlerFunc
- type Config
- type Controller
- type ControllerConfig
- type DatabaseStorage
- func (ds *DatabaseStorage) Cleanup(before time.Time) error
- func (ds *DatabaseStorage) Close() error
- func (ds *DatabaseStorage) CreateIndexes() error
- func (ds *DatabaseStorage) FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
- func (ds *DatabaseStorage) FindByID(id string) (*LogEntry, error)
- func (ds *DatabaseStorage) GetMethods() (map[string]int, error)
- func (ds *DatabaseStorage) GetStats() (map[string]interface{}, error)
- func (ds *DatabaseStorage) GetStatusCodes() (map[int]int, error)
- func (ds *DatabaseStorage) Save(entry *LogEntry) error
- func (ds *DatabaseStorage) Search(keyword string, page, pageSize int) ([]*LogEntry, int, error)
- type Debugger
- func New(config *Config) (*Debugger, error)
- func NewProductionDebugger(storagePath string) (*Debugger, error)
- func NewSimpleDebugger() (*Debugger, error)
- func NewWithCustomStorage(customStorage Storage) (*Debugger, error)
- func NewWithFileStorage(storagePath string, maxRecords int) (*Debugger, error)
- func NewWithMemoryStorage(maxRecords int) (*Debugger, error)
- func (d *Debugger) GetConfig() *Config
- func (d *Debugger) GetController() *Controller
- func (d *Debugger) GetLogger() LoggerInterface
- func (d *Debugger) GetLoggerWithFields(fields map[string]interface{}) LoggerInterface
- func (d *Debugger) GetStorage() Storage
- func (d *Debugger) Middleware() gin.HandlerFunc
- func (d *Debugger) RegisterRoutes(router *gin.Engine)
- func (d *Debugger) SetSessionData(c *gin.Context, data map[string]interface{})
- func (d *Debugger) WithController(router *gin.Engine, config *ControllerConfig) *Debugger
- type DefaultLogger
- func (l *DefaultLogger) ClearLogs()
- func (l *DefaultLogger) Debug(msg any, fields ...map[string]interface{})
- func (l *DefaultLogger) Error(msg any, fields ...map[string]interface{})
- func (l *DefaultLogger) GetLogs() []LoggerLog
- func (l *DefaultLogger) Info(msg any, fields ...map[string]interface{})
- func (l *DefaultLogger) Warn(msg any, fields ...map[string]interface{})
- func (l *DefaultLogger) WithFields(fields map[string]interface{}) LoggerInterface
- type DetailView
- type DetailViewer
- type FileStorage
- func (fs *FileStorage) Cleanup(before time.Time) error
- func (fs *FileStorage) Close() error
- func (fs *FileStorage) ExportAll(outputPath string) error
- func (fs *FileStorage) FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
- func (fs *FileStorage) FindByID(id string) (*LogEntry, error)
- func (fs *FileStorage) GetMethods() (map[string]int, error)
- func (fs *FileStorage) GetStats() (map[string]interface{}, error)
- func (fs *FileStorage) GetStatusCodes() (map[int]int, error)
- func (fs *FileStorage) ImportFromFile(filePath string) error
- func (fs *FileStorage) Save(entry *LogEntry) error
- func (fs *FileStorage) Search(keyword string, page, pageSize int) ([]*LogEntry, int, error)
- type LogEntry
- type LogEntryModel
- type LoggerInterface
- type LoggerLog
- type MemoryStorage
- func (ms *MemoryStorage) Cleanup(before time.Time) error
- func (ms *MemoryStorage) Clear() error
- func (ms *MemoryStorage) Close() error
- func (ms *MemoryStorage) ExportAll() ([]*LogEntry, error)
- func (ms *MemoryStorage) FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
- func (ms *MemoryStorage) FindByID(id string) (*LogEntry, error)
- func (ms *MemoryStorage) GetByTimeRange(startTime, endTime time.Time) ([]*LogEntry, error)
- func (ms *MemoryStorage) GetMethods() (map[string]int, error)
- func (ms *MemoryStorage) GetRecent(count int) ([]*LogEntry, error)
- func (ms *MemoryStorage) GetStats() (map[string]interface{}, error)
- func (ms *MemoryStorage) GetStatusCodes() (map[int]int, error)
- func (ms *MemoryStorage) ImportEntries(entries []*LogEntry) error
- func (ms *MemoryStorage) Save(entry *LogEntry) error
- func (ms *MemoryStorage) Search(keyword string, page, pageSize int) ([]*LogEntry, int, error)
- type Middleware
- type Pagination
- type QueryManager
- func (qm *QueryManager) Cleanup(before time.Time) error
- func (qm *QueryManager) Export(format string, options QueryOptions) ([]byte, error)
- func (qm *QueryManager) GetByID(id string) (*LogEntry, error)
- func (qm *QueryManager) GetByMethod(method string, page, pageSize int) (*QueryResult, error)
- func (qm *QueryManager) GetByStatusCode(statusCode int, page, pageSize int) (*QueryResult, error)
- func (qm *QueryManager) GetByTimeRange(startTime, endTime time.Time, page, pageSize int) (*QueryResult, error)
- func (qm *QueryManager) GetByURL(urlPattern string, page, pageSize int) (*QueryResult, error)
- func (qm *QueryManager) GetErrors(page, pageSize int) (*QueryResult, error)
- func (qm *QueryManager) GetFilterOptions() map[string]interface{}
- func (qm *QueryManager) GetRecent(limit int) ([]*LogEntry, error)
- func (qm *QueryManager) GetSlowRequests(threshold time.Duration, page, pageSize int) (*QueryResult, error)
- func (qm *QueryManager) GetStats() (map[string]interface{}, error)
- func (qm *QueryManager) Query(options QueryOptions) (*QueryResult, error)
- func (qm *QueryManager) Search(keyword string, page, pageSize int) (*QueryResult, error)
- type QueryOptions
- type QueryResult
- type Storage
- type TemplateData
Constants ¶
const ( LevelDebug = "debug" // 调试级别:记录所有详细信息 LevelInfo = "info" // 信息级别:只记录基本信息 LevelWarn = "warn" // 警告级别:记录警告信息 LevelError = "error" // 错误级别:记录错误信息 )
日志级别常量
Variables ¶
This section is empty.
Functions ¶
func DetailedDebugLogger ¶
func DetailedDebugLogger(storage Storage) gin.HandlerFunc
DetailedDebugLogger 详细的调试日志中间件 记录完整的请求和响应信息,适用于开发环境
func DisableMiddleware ¶
func DisableMiddleware() gin.HandlerFunc
DisableMiddleware 禁用中间件的快捷方法 创建一个空中间件,不进行任何调试记录
func EnableMiddleware ¶
func EnableMiddleware(storage Storage, config ...Config) gin.HandlerFunc
EnableMiddleware 启用中间件的快捷方法 创建一个已启用调试功能的中间件
func GenerateID ¶
func GenerateID() string
GenerateID 生成唯一请求标识 使用纳秒时间戳和进程ID确保唯一性,经过SM4加密后输出,密文结果不包含=字符
func MiddlewareWithConfig ¶
func MiddlewareWithConfig(storage Storage, config Config) gin.HandlerFunc
MiddlewareWithConfig 使用配置创建中间件 提供更灵活的配置选项
func RequestLogger ¶
func RequestLogger(storage Storage) gin.HandlerFunc
RequestLogger 简化的请求日志中间件 只记录基本的请求信息,适用于生产环境
Types ¶
type Config ¶
type Config struct {
Enabled bool `json:"enabled" default:"false"` // 是否启用调试器
MaxBodySize int64 `json:"max_body_size" default:"1024"` // 最大请求/响应体大小(KB),默认1MB
RetentionPeriod time.Duration `json:"retention_period" default:"168h"` // 日志保留期限,默认7天
Level string `json:"level" default:"debug"` // 日志级别:debug/info
MaxRecords int `json:"max_records" default:"150"` // 最大记录数量,默认150条
// 过滤配置
SkipPaths []string `json:"skip_paths" default:""` // 跳过的路径(如静态文件:"/static/,/favicon.ico")
SkipMethods []string `json:"skip_methods" default:"OPTIONS"` // 跳过的HTTP方法
// 采样配置
SampleRate float64 `json:"sample_rate" default:"1.0"` // 采样率(0-1之间),默认记录所有请求
// 核心组件配置 - 必须传入实例化的存储器
Storage Storage `json:"-"` // 存储实现(必须传入实例化的存储器)
Logger LoggerInterface `json:"-"` // 日志记录器(推荐直接传入实例化的日志记录器)
}
Config 调试器配置结构
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig 返回默认配置 使用helper.CheckAndSetDefault方法设置默认值,符合jcbaseGo规范
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
Controller 调试器控制器结构
func NewController ¶
func NewController(debugger *Debugger, router *gin.Engine, config *ControllerConfig) *Controller
NewController 创建新的调试器控制器
func (*Controller) RegisterRoutes ¶
func (c *Controller) RegisterRoutes(router *gin.Engine)
RegisterRoutes 手动注册路由到指定的Gin引擎 用于在初始化时没有传入路由组的情况
type ControllerConfig ¶
type ControllerConfig struct {
BasePath string `json:"base_path" default:"/jcbase/debug"` // 基础路径,默认为 "/jcbase/debug"
Title string `json:"title" default:"调试器"` // 页面标题,默认为 "调试器"
PageSize int `json:"page_size" default:"20"` // 页面大小,默认为 20
}
ControllerConfig 控制器配置
type DatabaseStorage ¶
type DatabaseStorage struct {
// contains filtered or unexported fields
}
DatabaseStorage 数据库存储器实现 使用GORM将调试日志保存到数据库中
func NewDatabaseStorage ¶
NewDatabaseStorage 创建新的数据库存储器 db: GORM数据库连接 maxSize: 最大存储条目数(0表示无限制) tableName: 数据表名(可选,默认为"debug_logs")
func (*DatabaseStorage) Cleanup ¶
func (ds *DatabaseStorage) Cleanup(before time.Time) error
Cleanup 清理过期日志
func (*DatabaseStorage) CreateIndexes ¶
func (ds *DatabaseStorage) CreateIndexes() error
CreateIndexes 创建索引(优化查询性能)
func (*DatabaseStorage) FindAll ¶
func (ds *DatabaseStorage) FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
FindAll 查找所有日志条目,支持分页和过滤
func (*DatabaseStorage) FindByID ¶
func (ds *DatabaseStorage) FindByID(id string) (*LogEntry, error)
FindByID 根据ID查找日志条目
func (*DatabaseStorage) GetMethods ¶
func (ds *DatabaseStorage) GetMethods() (map[string]int, error)
GetMethods 获取所有HTTP方法统计
func (*DatabaseStorage) GetStats ¶
func (ds *DatabaseStorage) GetStats() (map[string]interface{}, error)
GetStats 获取存储统计信息
func (*DatabaseStorage) GetStatusCodes ¶
func (ds *DatabaseStorage) GetStatusCodes() (map[int]int, error)
GetStatusCodes 获取所有状态码统计
func (*DatabaseStorage) Save ¶
func (ds *DatabaseStorage) Save(entry *LogEntry) error
Save 保存日志条目到数据库
type Debugger ¶
type Debugger struct {
// contains filtered or unexported fields
}
Debugger 调试器主结构
func NewProductionDebugger ¶
NewProductionDebugger 创建生产环境调试器实例 使用文件存储,适合生产环境
func NewSimpleDebugger ¶
NewSimpleDebugger 创建简单的调试器实例 使用默认配置,适合快速开始
func NewWithCustomStorage ¶
NewWithCustomStorage 创建使用自定义存储器的调试器 适用于需要自定义存储逻辑的场景
func NewWithFileStorage ¶
NewWithFileStorage 创建使用文件存储器的调试器 适用于生产环境,日志会持久化到文件系统
func NewWithMemoryStorage ¶
NewWithMemoryStorage 创建使用内存存储器的调试器 适用于开发和测试环境
func (*Debugger) GetController ¶
func (d *Debugger) GetController() *Controller
GetController 获取控制器实例
func (*Debugger) GetLogger ¶
func (d *Debugger) GetLogger() LoggerInterface
GetLogger 获取调试器的日志记录器 可以在控制器中通过此方法获取Logger实例来记录日志
func (*Debugger) GetLoggerWithFields ¶
func (d *Debugger) GetLoggerWithFields(fields map[string]interface{}) LoggerInterface
GetLoggerWithFields 获取带有指定字段的日志记录器
func (*Debugger) Middleware ¶
func (d *Debugger) Middleware() gin.HandlerFunc
Middleware 创建Gin中间件 用于拦截HTTP请求并记录调试信息
func (*Debugger) RegisterRoutes ¶
RegisterRoutes 手动注册路由到指定的Gin引擎 只支持 *gin.Engine 类型,使用更简洁
func (*Debugger) SetSessionData ¶
SetSessionData 设置会话数据(供外部调用)
func (*Debugger) WithController ¶
func (d *Debugger) WithController(router *gin.Engine, config *ControllerConfig) *Debugger
WithController 为调试器添加控制器支持 router: Gin引擎,用于注册调试器页面路由 config: 控制器配置(可选)
type DefaultLogger ¶
type DefaultLogger struct {
// contains filtered or unexported fields
}
DefaultLogger 调试器内置的日志记录器实现(默认实现)
func (*DefaultLogger) Debug ¶
func (l *DefaultLogger) Debug(msg any, fields ...map[string]interface{})
Debug 记录调试级别日志
func (*DefaultLogger) Error ¶
func (l *DefaultLogger) Error(msg any, fields ...map[string]interface{})
Error 记录错误级别日志
func (*DefaultLogger) Info ¶
func (l *DefaultLogger) Info(msg any, fields ...map[string]interface{})
Info 记录信息级别日志
func (*DefaultLogger) Warn ¶
func (l *DefaultLogger) Warn(msg any, fields ...map[string]interface{})
Warn 记录警告级别日志
func (*DefaultLogger) WithFields ¶
func (l *DefaultLogger) WithFields(fields map[string]interface{}) LoggerInterface
WithFields 创建带有字段的日志记录器
type DetailView ¶
type DetailView struct {
LogEntry *LogEntry `json:"log_entry"` // 原始日志条目
// 格式化后的内容
FormattedRequestHeaders string `json:"formatted_request_headers"` // 格式化请求头
FormattedResponseHeaders string `json:"formatted_response_headers"` // 格式化响应头
FormattedRequestBody string `json:"formatted_request_body"` // 格式化请求体
FormattedResponseBody string `json:"formatted_response_body"` // 格式化响应体
FormattedSessionData string `json:"formatted_session_data"` // 格式化会话数据
FormattedQueryParams string `json:"formatted_query_params"` // 格式化查询参数
// 统计信息
RequestSize int `json:"request_size"` // 请求大小(字节)
ResponseSize int `json:"response_size"` // 响应大小(字节)
// 时间信息
FormattedTimestamp string `json:"formatted_timestamp"` // 格式化时间戳
FormattedDuration string `json:"formatted_duration"` // 格式化持续时间
// 相关日志
RelatedEntries []*LogEntry `json:"related_entries"` // 相关日志条目
}
DetailView 详情视图 包含日志条目的完整信息和格式化后的内容
type DetailViewer ¶
type DetailViewer struct {
// contains filtered or unexported fields
}
DetailViewer 日志详情查看器 提供丰富的日志详情展示功能,支持格式化显示请求和响应信息
func NewDetailViewer ¶
func NewDetailViewer(queryManager *QueryManager) *DetailViewer
NewDetailViewer 创建新的详情查看器 queryManager: 查询管理器实例
func (*DetailViewer) CompareEntries ¶
func (dv *DetailViewer) CompareEntries(id1, id2 string) (map[string]interface{}, error)
CompareEntries 比较两个日志条目
func (*DetailViewer) GetDetail ¶
func (dv *DetailViewer) GetDetail(id string) (*DetailView, error)
GetDetail 获取日志详情 id: 日志条目ID
func (*DetailViewer) GetDetailHTML ¶
func (dv *DetailViewer) GetDetailHTML(id string) (string, error)
GetDetailHTML 获取HTML格式的详情
func (*DetailViewer) GetDetailJSON ¶
func (dv *DetailViewer) GetDetailJSON(id string) ([]byte, error)
GetDetailJSON 获取JSON格式的详情
type FileStorage ¶
type FileStorage struct {
// contains filtered or unexported fields
}
FileStorage 文件存储器实现 将调试日志保存到文件系统中
func NewFileStorage ¶
func NewFileStorage(basePath string, maxSize int) (*FileStorage, error)
NewFileStorage 创建新的文件存储器 basePath: 文件存储的基础路径 maxSize: 最大存储条目数(0表示无限制)
func (*FileStorage) ExportAll ¶
func (fs *FileStorage) ExportAll(outputPath string) error
ExportAll 导出所有日志到单个文件
func (*FileStorage) FindAll ¶
func (fs *FileStorage) FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
FindAll 查找所有日志条目,支持分页和过滤
func (*FileStorage) FindByID ¶
func (fs *FileStorage) FindByID(id string) (*LogEntry, error)
FindByID 根据ID查找日志条目
func (*FileStorage) GetMethods ¶
func (fs *FileStorage) GetMethods() (map[string]int, error)
GetMethods 获取HTTP方法统计
func (*FileStorage) GetStats ¶
func (fs *FileStorage) GetStats() (map[string]interface{}, error)
GetStats 获取存储统计信息
func (*FileStorage) GetStatusCodes ¶
func (fs *FileStorage) GetStatusCodes() (map[int]int, error)
GetStatusCodes 获取状态码统计
func (*FileStorage) ImportFromFile ¶
func (fs *FileStorage) ImportFromFile(filePath string) error
ImportFromFile 从文件导入日志
func (*FileStorage) Save ¶
func (fs *FileStorage) Save(entry *LogEntry) error
Save 保存日志条目到文件 每个日志条目保存为一个独立的JSON文件
type LogEntry ¶
type LogEntry struct {
ID string `json:"id"` // 日志唯一标识
Timestamp time.Time `json:"timestamp"` // 请求时间戳
Method string `json:"method"` // HTTP方法
URL string `json:"url"` // 请求URL
StatusCode int `json:"status_code"` // HTTP状态码
Duration time.Duration `json:"duration"` // 处理耗时
ClientIP string `json:"client_ip"` // 客户端IP
UserAgent string `json:"user_agent"` // 用户代理
RequestID string `json:"request_id"` // 请求ID(用于追踪)
// 请求信息
RequestHeaders map[string]string `json:"request_headers"` // 请求头
QueryParams map[string]string `json:"query_params"` // 查询参数
RequestBody string `json:"request_body"` // 请求体内容
// 响应信息
ResponseHeaders map[string]string `json:"response_headers"` // 响应头
ResponseBody string `json:"response_body"` // 响应体内容
// 会话数据(可选)
SessionData map[string]interface{} `json:"session_data,omitempty"` // 会话数据
// 错误信息
Error string `json:"error,omitempty"` // 错误信息
// Logger日志信息(新增)
LoggerLogs []LoggerLog `json:"logger_logs,omitempty"` // 通过logger记录的日志
// 存储大小(计算字段,不持久化到存储)
StorageSize string `json:"storage_size,omitempty"` // 存储大小(格式化显示)
}
LogEntry 调试日志条目结构 用于记录单个HTTP请求的完整调试信息
func (*LogEntry) CalculateStorageSize ¶
CalculateStorageSize 计算日志条目的存储大小并格式化显示 计算内容包括:基本字段、请求体、响应体、请求头、响应头、查询参数、会话数据等 与总存储计算逻辑保持一致
type LogEntryModel ¶
type LogEntryModel struct {
ID string `gorm:"column:id;type:VARCHAR(64);primaryKey" json:"id"`
Timestamp time.Time `gorm:"column:timestamp;type:DATETIME;index" json:"timestamp"`
Method string `gorm:"column:method;type:VARCHAR(10);index" json:"method"`
URL string `gorm:"column:url;type:TEXT" json:"url"`
StatusCode int `gorm:"column:status_code;type:INT;index" json:"status_code"`
Duration int64 `gorm:"column:duration;type:BIGINT" json:"duration"` // 存储纳秒数
ClientIP string `gorm:"column:client_ip;type:VARCHAR(45)" json:"client_ip"`
UserAgent string `gorm:"column:user_agent;type:TEXT" json:"user_agent"`
RequestID string `gorm:"column:request_id;type:VARCHAR(64);index" json:"request_id"`
// JSON格式存储的字段
RequestHeaders string `gorm:"column:request_headers;type:JSON" json:"request_headers"`
QueryParams string `gorm:"column:query_params;type:JSON" json:"query_params"`
RequestBody string `gorm:"column:request_body;type:LONGTEXT" json:"request_body"`
ResponseHeaders string `gorm:"column:response_headers;type:JSON" json:"response_headers"`
ResponseBody string `gorm:"column:response_body;type:LONGTEXT" json:"response_body"`
SessionData string `gorm:"column:session_data;type:JSON" json:"session_data"`
Error string `gorm:"column:error;type:TEXT" json:"error"`
// 索引字段
URLHash string `gorm:"column:url_hash;type:VARCHAR(32);index" json:"url_hash"` // URL的MD5哈希,用于快速搜索
}
LogEntryModel 数据库模型结构 对应数据库中的日志条目表
type LoggerInterface ¶
type LoggerInterface interface {
// Debug 记录调试级别日志
Debug(msg any, fields ...map[string]interface{})
// Info 记录信息级别日志
Info(msg any, fields ...map[string]interface{})
// Warn 记录警告级别日志
Warn(msg any, fields ...map[string]interface{})
// Error 记录错误级别日志
Error(msg any, fields ...map[string]interface{})
// WithFields 创建带有字段的日志记录器
WithFields(fields map[string]interface{}) LoggerInterface
}
LoggerInterface 日志记录器接口 支持不同级别的日志记录,可以在控制器中直接使用
func GetLoggerFromContext ¶
func GetLoggerFromContext(c *gin.Context) LoggerInterface
GetLoggerFromContext 从Gin上下文中获取Logger实例 控制器可以通过此函数获取Logger来记录日志
type LoggerLog ¶
type LoggerLog struct {
Timestamp time.Time `json:"timestamp"` // 日志时间戳
Level string `json:"level"` // 日志级别:debug/info/warn/error
Message string `json:"message"` // 日志消息
Fields map[string]interface{} `json:"fields"` // 日志附加字段
}
LoggerLog 记录通过logger打印的日志信息
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
MemoryStorage 内存存储器实现 将调试日志保存在内存中,适合开发环境使用
func NewMemoryStorage ¶
func NewMemoryStorage(maxSize ...int) (*MemoryStorage, error)
NewMemoryStorage 创建新的内存存储器 maxSize: 最大存储条目数(0表示无限制)
func (*MemoryStorage) Cleanup ¶
func (ms *MemoryStorage) Cleanup(before time.Time) error
Cleanup 清理过期日志
func (*MemoryStorage) ExportAll ¶
func (ms *MemoryStorage) ExportAll() ([]*LogEntry, error)
ExportAll 导出所有日志到内存(返回副本)
func (*MemoryStorage) FindAll ¶
func (ms *MemoryStorage) FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
FindAll 查找所有日志条目,支持分页和过滤
func (*MemoryStorage) FindByID ¶
func (ms *MemoryStorage) FindByID(id string) (*LogEntry, error)
FindByID 根据ID查找日志条目
func (*MemoryStorage) GetByTimeRange ¶
func (ms *MemoryStorage) GetByTimeRange(startTime, endTime time.Time) ([]*LogEntry, error)
GetByTimeRange 根据时间范围获取日志条目
func (*MemoryStorage) GetMethods ¶
func (ms *MemoryStorage) GetMethods() (map[string]int, error)
GetMethods 获取所有HTTP方法统计
func (*MemoryStorage) GetRecent ¶
func (ms *MemoryStorage) GetRecent(count int) ([]*LogEntry, error)
GetRecent 获取最近的日志条目
func (*MemoryStorage) GetStats ¶
func (ms *MemoryStorage) GetStats() (map[string]interface{}, error)
GetStats 获取存储统计信息
func (*MemoryStorage) GetStatusCodes ¶
func (ms *MemoryStorage) GetStatusCodes() (map[int]int, error)
GetStatusCodes 获取所有状态码统计
func (*MemoryStorage) ImportEntries ¶
func (ms *MemoryStorage) ImportEntries(entries []*LogEntry) error
ImportEntries 导入日志条目
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware Gin中间件,用于拦截请求并记录调试信息 该中间件会捕获请求和响应的所有关键信息,包括请求头、请求体、响应头、响应体等
func NewMiddleware ¶
func NewMiddleware(debugger *Debugger) *Middleware
NewMiddleware 创建新的中间件实例 debugger: 调试器实例,用于存储日志
func (*Middleware) Handler ¶
func (m *Middleware) Handler() gin.HandlerFunc
Handler 中间件处理函数 实现gin.HandlerFunc接口,用于拦截HTTP请求
type Pagination ¶
type Pagination struct {
Page int // 当前页码
PageSize int // 每页大小
Total int // 总记录数
TotalPages int // 总页数
HasPrev bool // 是否有上一页
HasNext bool // 是否有下一页
PrevPage int // 上一页页码
NextPage int // 下一页页码
}
Pagination 分页信息
type QueryManager ¶
type QueryManager struct {
// contains filtered or unexported fields
}
QueryManager 日志查询管理器 提供丰富的查询和过滤功能,支持多种查询条件组合
func NewQueryManager ¶
func NewQueryManager(storage Storage) *QueryManager
NewQueryManager 创建新的查询管理器 storage: 存储接口实例
func (*QueryManager) Cleanup ¶
func (qm *QueryManager) Cleanup(before time.Time) error
Cleanup 清理过期日志 before: 清理此时间之前的日志
func (*QueryManager) Export ¶
func (qm *QueryManager) Export(format string, options QueryOptions) ([]byte, error)
Export 导出日志数据 format: 导出格式(json, csv等)
func (*QueryManager) GetByID ¶
func (qm *QueryManager) GetByID(id string) (*LogEntry, error)
GetByID 根据ID获取日志条目
func (*QueryManager) GetByMethod ¶
func (qm *QueryManager) GetByMethod(method string, page, pageSize int) (*QueryResult, error)
GetByMethod 根据HTTP方法查询日志
func (*QueryManager) GetByStatusCode ¶
func (qm *QueryManager) GetByStatusCode(statusCode int, page, pageSize int) (*QueryResult, error)
GetByStatusCode 根据状态码查询日志
func (*QueryManager) GetByTimeRange ¶
func (qm *QueryManager) GetByTimeRange(startTime, endTime time.Time, page, pageSize int) (*QueryResult, error)
GetByTimeRange 根据时间范围查询日志 startTime: 开始时间 endTime: 结束时间
func (*QueryManager) GetByURL ¶
func (qm *QueryManager) GetByURL(urlPattern string, page, pageSize int) (*QueryResult, error)
GetByURL 根据URL查询日志(模糊匹配)
func (*QueryManager) GetErrors ¶
func (qm *QueryManager) GetErrors(page, pageSize int) (*QueryResult, error)
GetErrors 获取包含错误的日志
func (*QueryManager) GetFilterOptions ¶
func (qm *QueryManager) GetFilterOptions() map[string]interface{}
GetFilterOptions 获取可用的过滤选项
func (*QueryManager) GetRecent ¶
func (qm *QueryManager) GetRecent(limit int) ([]*LogEntry, error)
GetRecent 获取最近的日志条目 limit: 限制返回的条目数量
func (*QueryManager) GetSlowRequests ¶
func (qm *QueryManager) GetSlowRequests(threshold time.Duration, page, pageSize int) (*QueryResult, error)
GetSlowRequests 获取慢请求日志 threshold: 慢请求阈值
func (*QueryManager) GetStats ¶
func (qm *QueryManager) GetStats() (map[string]interface{}, error)
GetStats 获取统计信息
func (*QueryManager) Query ¶
func (qm *QueryManager) Query(options QueryOptions) (*QueryResult, error)
Query 执行查询 根据查询选项获取日志条目
func (*QueryManager) Search ¶
func (qm *QueryManager) Search(keyword string, page, pageSize int) (*QueryResult, error)
Search 执行搜索 根据关键词搜索日志内容
type QueryOptions ¶
type QueryOptions struct {
Page int `json:"page"` // 页码,从1开始
PageSize int `json:"page_size"` // 每页大小
Filters map[string]interface{} `json:"filters"` // 过滤条件
SortBy string `json:"sort_by"` // 排序字段
SortOrder string `json:"sort_order"` // 排序方向:asc/desc
}
QueryOptions 查询选项 定义查询的各种过滤条件和分页参数
type QueryResult ¶
type QueryResult struct {
Entries []*LogEntry `json:"entries"` // 日志条目列表
Total int `json:"total"` // 总记录数
Page int `json:"page"` // 当前页码
PageSize int `json:"page_size"` // 每页大小
TotalPages int `json:"total_pages"` // 总页数
}
QueryResult 查询结果 包含查询到的日志条目和分页信息
type Storage ¶
type Storage interface {
// Save 保存日志条目
Save(entry *LogEntry) error
// FindByID 根据ID查找日志条目
FindByID(id string) (*LogEntry, error)
// FindAll 查找所有日志条目,支持分页和过滤
FindAll(page, pageSize int, filters map[string]interface{}) ([]*LogEntry, int, error)
// Search 搜索日志内容
Search(keyword string, page, pageSize int) ([]*LogEntry, int, error)
// Cleanup 清理过期日志
Cleanup(before time.Time) error
// GetStats 获取统计信息
GetStats() (map[string]interface{}, error)
// GetMethods 获取HTTP方法统计
GetMethods() (map[string]int, error)
// GetStatusCodes 获取状态码统计
GetStatusCodes() (map[int]int, error)
// Close 关闭存储
Close() error
}
Storage 存储接口定义 支持多种存储方式:文件、内存、数据库