mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
New warehouse
This commit is contained in:
59
utils/logger/http.go
Normal file
59
utils/logger/http.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LogRequest 记录HTTP请求日志 - 使用标准Apache Common Log Format
|
||||
// 格式: IP - - [timestamp] "METHOD path HTTP/1.1" status_code response_size
|
||||
// method: HTTP请求方法
|
||||
// path: 请求路径
|
||||
// clientIP: 客户端IP地址
|
||||
// statusCode: HTTP状态码
|
||||
// duration: 请求处理时长
|
||||
func (l *Logger) LogRequest(method, path, clientIP string, statusCode int, duration time.Duration) {
|
||||
l.LogRequestWithHeaders(method, path, clientIP, statusCode, duration, "-", "-")
|
||||
}
|
||||
|
||||
// LogRequestWithHeaders 记录HTTP请求日志 - 使用修改的Apache Log Format(移除Referer字段)
|
||||
// 直接输出标准格式,不通过logrus格式化器
|
||||
// method: HTTP请求方法
|
||||
// path: 请求路径
|
||||
// clientIP: 客户端IP地址
|
||||
// statusCode: HTTP状态码
|
||||
// duration: 请求处理时长
|
||||
// referer: 引用页面(已废弃,保留参数兼容性)
|
||||
// userAgent: 用户代理字符串
|
||||
func (l *Logger) LogRequestWithHeaders(method, path, clientIP string, statusCode int, duration time.Duration, referer, userAgent string) {
|
||||
// 格式化时间戳为Apache标准格式
|
||||
timestamp := time.Now().Format("02/Jan/2006:15:04:05 -0700")
|
||||
|
||||
// 处理空值
|
||||
if userAgent == "" {
|
||||
userAgent = "-"
|
||||
}
|
||||
|
||||
// 构建修改的HTTP Log格式(完全移除Referer字段)
|
||||
logLine := fmt.Sprintf(`%s - - [%s] "%s %s HTTP/1.1" %d - "%s" %dms`,
|
||||
clientIP,
|
||||
timestamp,
|
||||
method,
|
||||
path,
|
||||
statusCode,
|
||||
userAgent,
|
||||
duration.Milliseconds(),
|
||||
)
|
||||
|
||||
// 直接输出到标准输出和日志文件,不使用logrus格式化
|
||||
l.writeHTTPLog(logLine)
|
||||
}
|
||||
|
||||
// writeHTTPLog 直接输出HTTP日志到标准输出
|
||||
// 避免Logrus的任何格式化和转义,保持Apache日志格式的原始性
|
||||
// logLine: 格式化后的日志行
|
||||
func (l *Logger) writeHTTPLog(logLine string) {
|
||||
// 直接输出到标准输出,避免Logrus的转义处理
|
||||
fmt.Fprintln(os.Stdout, logLine)
|
||||
}
|
||||
112
utils/logger/logger.go
Normal file
112
utils/logger/logger.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Logger 日志工具结构体
|
||||
// 封装logrus.Logger,提供统一的日志接口
|
||||
type Logger struct {
|
||||
*log.Logger // 嵌入logrus.Logger,继承其所有方法
|
||||
}
|
||||
|
||||
// NewLogger 创建新的日志实例,使用全局logrus配置
|
||||
// 返回: 新的Logger实例
|
||||
func NewLogger() *Logger {
|
||||
// 使用全局logrus实例而不是创建新实例,确保配置一致性
|
||||
return &Logger{Logger: log.StandardLogger()}
|
||||
}
|
||||
|
||||
// InitLogger 初始化HTTP日志处理器
|
||||
// 创建专门用于HTTP请求日志的Logger实例,使用全局logrus配置
|
||||
// 返回: 初始化后的Logger实例
|
||||
func InitLogger() *Logger {
|
||||
logger := NewLogger()
|
||||
|
||||
// HTTP日志使用全局logrus的配置
|
||||
// 通过使用log.StandardLogger()确保与全局配置保持一致
|
||||
|
||||
// 更新全局日志实例
|
||||
SetGlobalLogger(logger)
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
// WithFields 添加字段到日志条目
|
||||
// fields: 要添加的字段映射
|
||||
// 返回: 包含字段的日志条目
|
||||
func (l *Logger) WithFields(fields log.Fields) *log.Entry {
|
||||
return l.Logger.WithFields(fields)
|
||||
}
|
||||
|
||||
// WithField 添加单个字段到日志条目
|
||||
// key: 字段名
|
||||
// value: 字段值
|
||||
// 返回: 包含字段的日志条目
|
||||
func (l *Logger) WithField(key string, value interface{}) *log.Entry {
|
||||
return l.Logger.WithField(key, value)
|
||||
}
|
||||
|
||||
// WithError 添加错误字段到日志条目
|
||||
// err: 要记录的错误
|
||||
// 返回: 包含错误信息的日志条目
|
||||
func (l *Logger) WithError(err error) *log.Entry {
|
||||
return l.Logger.WithError(err)
|
||||
}
|
||||
|
||||
// InfoWithFields 记录带字段的信息级别日志
|
||||
// msg: 日志消息
|
||||
// fields: 附加字段
|
||||
func (l *Logger) InfoWithFields(msg string, fields log.Fields) {
|
||||
l.WithFields(fields).Info(msg)
|
||||
}
|
||||
|
||||
// ErrorWithFields 记录带字段的错误级别日志
|
||||
// msg: 日志消息
|
||||
// fields: 附加字段
|
||||
func (l *Logger) ErrorWithFields(msg string, fields log.Fields) {
|
||||
l.WithFields(fields).Error(msg)
|
||||
}
|
||||
|
||||
// WarnWithFields 记录带字段的警告级别日志
|
||||
// msg: 日志消息
|
||||
// fields: 附加字段
|
||||
func (l *Logger) WarnWithFields(msg string, fields log.Fields) {
|
||||
l.WithFields(fields).Warn(msg)
|
||||
}
|
||||
|
||||
// DebugWithFields 记录带字段的调试级别日志
|
||||
// msg: 日志消息
|
||||
// fields: 附加字段
|
||||
func (l *Logger) DebugWithFields(msg string, fields log.Fields) {
|
||||
l.WithFields(fields).Debug(msg)
|
||||
}
|
||||
|
||||
// LogError 记录错误日志
|
||||
// err: 错误对象
|
||||
// msg: 日志消息
|
||||
func (l *Logger) LogError(err error, msg string) {
|
||||
l.WithError(err).Error(msg)
|
||||
}
|
||||
|
||||
// GlobalLogger 全局日志实例
|
||||
// 提供全局访问的日志记录器
|
||||
var GlobalLogger *Logger
|
||||
|
||||
// init 包初始化函数
|
||||
// 创建全局日志实例,使用全局logrus配置
|
||||
func init() {
|
||||
GlobalLogger = NewLogger()
|
||||
}
|
||||
|
||||
// GetLogger 获取全局日志实例
|
||||
// 返回: 全局Logger实例
|
||||
func GetLogger() *Logger {
|
||||
return GlobalLogger
|
||||
}
|
||||
|
||||
// SetGlobalLogger 设置全局日志实例
|
||||
// logger: 要设置的Logger实例
|
||||
func SetGlobalLogger(logger *Logger) {
|
||||
GlobalLogger = logger
|
||||
}
|
||||
26
utils/logger/server.go
Normal file
26
utils/logger/server.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// LogServerStart 记录服务器启动日志
|
||||
// host: 服务器监听地址
|
||||
// port: 服务器监听端口
|
||||
func (l *Logger) LogServerStart(host string, port int) {
|
||||
l.WithFields(log.Fields{
|
||||
"host": host,
|
||||
"port": port,
|
||||
}).Info("HTTP服务器启动")
|
||||
}
|
||||
|
||||
// LogServerStop 记录服务器停止日志
|
||||
func (l *Logger) LogServerStop() {
|
||||
l.Info("HTTP服务器停止")
|
||||
}
|
||||
|
||||
// LogConfigLoad 记录配置加载日志
|
||||
// configFile: 配置文件路径
|
||||
func (l *Logger) LogConfigLoad(configFile string) {
|
||||
l.WithField("config_file", configFile).Info("配置文件加载")
|
||||
}
|
||||
Reference in New Issue
Block a user