xlog

package module
v0.0.0-...-4d8cce5 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 13 Imported by: 0

README

Combined Logging

based on both ginzap and gin's default logging - this is a combo logger.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomRecoveryWithZap

func CustomRecoveryWithZap(logger *zap.Logger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc

CustomRecoveryWithZap returns a gin.HandlerFunc (middleware) with a custom recovery handler that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

func DisableConsoleColor

func DisableConsoleColor()

DisableConsoleColor disables color output in the console.

func ForceConsoleColor

func ForceConsoleColor()

ForceConsoleColor force color output in the console.

func Logger

func Logger(logger *zap.Logger) gin.HandlerFunc

Logger instances a Logger middleware that will write the logs to gin.DefaultWriter. By default, gin.DefaultWriter = os.Stdout.

func LoggerWithFormatter

func LoggerWithFormatter(logger *zap.Logger, f LogFormatter, timeFormat string, utc bool) gin.HandlerFunc

LoggerWithFormatter instance a Logger middleware with the specified log format function.

func RecoveryWithZap

func RecoveryWithZap(logger *zap.Logger, stack bool) gin.HandlerFunc

RecoveryWithZap returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

func XLog

func XLog(logger *zap.Logger, timeFormat string, utc bool) gin.HandlerFunc

XLog returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

It receives:

  1. A time package format string (e.g. time.RFC3339).
  2. A boolean stating whether to use UTC time zone or local.

func XLogWithConfig

func XLogWithConfig(logger *zap.Logger, conf *Config) gin.HandlerFunc

XLogWithConfig returns a gin.HandlerFunc using configs

Types

type Config

type Config struct {
	TimeFormat string
	UTC        bool
	SkipPaths  []string
	Context    Fn
	Formatter  LogFormatter
	Output     io.Writer
}

Config is config setting for XLog

type Fn

type Fn func(c *gin.Context) []zapcore.Field

type LogFormatter

type LogFormatter func(params LogFormatterParams) string

LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter

type LogFormatterParams

type LogFormatterParams struct {
	Request *http.Request

	// TimeStamp shows the time after the server returns a response.
	TimeStamp time.Time
	// StatusCode is HTTP response code.
	StatusCode int
	// Latency is how much time the server cost to process a certain request.
	Latency time.Duration
	// ClientIP equals Context's ClientIP method.
	ClientIP string
	// Method is the HTTP method given to the request.
	Method string
	// Path is a path the client requests.
	Path string
	// ErrorMessage is set if error has occurred in processing the request.
	ErrorMessage string

	// BodySize is the size of the Response Body
	BodySize int
	// Keys are the keys set on the request's context.
	Keys map[string]any
	// contains filtered or unexported fields
}

LogFormatterParams is the structure any formatter will be handed when time to log comes

func (*LogFormatterParams) IsOutputColor

func (p *LogFormatterParams) IsOutputColor() bool

IsOutputColor indicates whether can colors be outputted to the log.

func (*LogFormatterParams) MethodColor

func (p *LogFormatterParams) MethodColor() string

MethodColor is the ANSI color for appropriately logging http method to a terminal.

func (*LogFormatterParams) ResetColor

func (p *LogFormatterParams) ResetColor() string

ResetColor resets all escape attributes.

func (*LogFormatterParams) StatusCodeColor

func (p *LogFormatterParams) StatusCodeColor() string

StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.

type LoggerConfig

type LoggerConfig struct {
	// Optional. Default value is gin.defaultLogFormatter
	Formatter LogFormatter

	// Output is a writer where logs are written.
	// Optional. Default value is gin.DefaultWriter.
	Output io.Writer

	// SkipPaths is an url path array which logs are not written.
	// Optional.
	SkipPaths []string
}

LoggerConfig defines the config for Logger middleware.

Jump to

Keyboard shortcuts

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