log

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT, Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogFormatter = func(param FormatterParams) string {
	var statusColor, methodColor, resetColor string
	if param.IsOutputColor() {
		statusColor = param.StatusCodeColor()
		methodColor = param.MethodColor()
		resetColor = param.ResetColor()
	}

	if param.Latency > time.Minute {

		param.Latency = param.Latency - param.Latency%time.Second
	}

	return fmt.Sprintf("%v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
		param.TimeStamp.Format("2006/01/02-15:04:05"),
		statusColor, param.StatusCode, resetColor,
		param.Latency,
		param.ClientIP,
		methodColor, param.Method, resetColor,
		param.Path,
		param.ErrorMessage,
	)
}

defaultLogFormatter is the default log format function Logger middleware uses.

Functions

func DisableConsoleColor

func DisableConsoleColor()

DisableConsoleColor disables color output in the console.

func ForceConsoleColor

func ForceConsoleColor()

ForceConsoleColor force color output in the console.

Types

type Formatter added in v0.3.3

type Formatter func(params FormatterParams) string

Formatter gives the signature of the formatter function passed to LoggerWithFormatter

type FormatterParams added in v0.3.3

type FormatterParams 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]interface{}
	// contains filtered or unexported fields
}

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

func (*FormatterParams) IsOutputColor added in v0.3.3

func (p *FormatterParams) IsOutputColor() bool

IsOutputColor indicates whether can colors be outputted to the log.

func (*FormatterParams) MethodColor added in v0.3.3

func (p *FormatterParams) MethodColor() string

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

func (*FormatterParams) ResetColor added in v0.3.3

func (p *FormatterParams) ResetColor() string

ResetColor resets all escape attributes.

func (*FormatterParams) StatusCodeColor added in v0.3.3

func (p *FormatterParams) StatusCodeColor() string

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

type LoggerConfig

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

	Logger *log.Logger

	ErrHandle bool
	// SkipPaths is a 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