logger

package
v0.0.0-...-07920c1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEBUG = iota
	INFO
	WARNING
	ERROR
	FATAL
)

日志级别常量

View Source
const (
	// MaxLogSize 单个日志文件的最大大小(MB)
	MaxLogSize = 128
	// MaxLogBackups 保留的旧日志文件的最大数量
	MaxLogBackups = 3
	// MaxLogAge 保留的旧日志文件的最大天数
	MaxLogAge = 28
)

日志配置常量

Variables

View Source
var (
	MethodColor = color.New(color.FgGreen, color.Bold).SprintfFunc()
	PathColor   = color.CyanString
	StatusColor = map[int]func(format string, a ...interface{}) string{

		200: color.New(color.FgGreen).SprintfFunc(),
		201: color.New(color.FgGreen).SprintfFunc(),

		301: color.New(color.FgYellow).SprintfFunc(),
		302: color.New(color.FgYellow).SprintfFunc(),

		400: color.New(color.FgMagenta).SprintfFunc(),
		401: color.New(color.FgMagenta).SprintfFunc(),
		403: color.New(color.FgMagenta).SprintfFunc(),
		404: color.New(color.FgMagenta).SprintfFunc(),

		500: color.New(color.FgRed, color.Bold).SprintfFunc(),
		501: color.New(color.FgRed, color.Bold).SprintfFunc(),
		502: color.New(color.FgRed, color.Bold).SprintfFunc(),
		503: color.New(color.FgRed, color.Bold).SprintfFunc(),
	}
	TimeColor     = color.New(color.FgWhite).SprintfFunc()
	ClientIPColor = color.New(color.FgWhite).SprintfFunc()
	BytesColor    = color.New(color.FgMagenta).SprintfFunc()
)

访问日志的颜色

Functions

func AccessLog

func AccessLog(method, path string, statusCode, bytes int, clientIP string, duration time.Duration)

默认实例的访问日志方法

func Debug

func Debug(format string, args ...interface{})

提供默认实例的方便方法

func DefaultGinLogger

func DefaultGinLogger() gin.HandlerFunc

DefaultGinLogger 使用默认日志记录器的Gin中间件

func Error

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

func Fatal

func Fatal(format string, args ...interface{})

func FormatAccessLog

func FormatAccessLog(method, path string, statusCode, bytes int, clientIP string, duration time.Duration) (string, string)

FormatAccessLog 格式化HTTP访问日志

func GinLogger

func GinLogger(logger *Logger) gin.HandlerFunc

GinLogger 返回一个Gin中间件,用于记录HTTP请求的访问日志

func Info

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

func Warning

func Warning(format string, args ...interface{})

Types

type LogConfig

type LogConfig struct {
	// 日志级别
	Level int
	// 是否启用文件日志
	EnableFileLog bool
	// 日志文件名(不包含路径)
	Filename string
}

LogConfig 日志配置

type Logger

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

Logger 结构表示一个日志记录器

var Default *Logger

默认日志实例

func New

func New(config LogConfig) (*Logger, error)

创建一个新的Logger实例

func (*Logger) AccessLog

func (l *Logger) AccessLog(method, path string, statusCode, bytes int, clientIP string, duration time.Duration)

AccessLog 记录HTTP访问日志

func (*Logger) Debug

func (l *Logger) Debug(format string, args ...interface{})

Debug 记录DEBUG级别日志

func (*Logger) Error

func (l *Logger) Error(format string, args ...interface{})

Error 记录ERROR级别日志

func (*Logger) Fatal

func (l *Logger) Fatal(format string, args ...interface{})

Fatal 记录FATAL级别日志并退出程序

func (*Logger) GetLevel

func (l *Logger) GetLevel() int

GetLevel 获取当前日志级别

func (*Logger) Info

func (l *Logger) Info(format string, args ...interface{})

Info 记录INFO级别日志

func (*Logger) SetLevel

func (l *Logger) SetLevel(level int)

SetLevel 设置日志级别

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput 设置控制台日志输出目标

func (*Logger) Warning

func (l *Logger) Warning(format string, args ...interface{})

Warning 记录WARNING级别日志

Jump to

Keyboard shortcuts

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