accessLog

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

Hertz-accessLog

Hertz middleware to get access log.

Usage

Start using it

Download hertz-accessLog by using:

go get github.com/FlameMida/accessLog@latest

Import following in your code:

import "github.com/FlameMida/accessLog" // hertz-accessLog middleware 
Quick start
Run a default logger print in terminal
func main() {
    h := server.Default()
    h.Use(accessLog.Logger())
    h.Spin()
}

Logger uses the low-allocation path by default. It emits ANSI colors automatically when the output stream looks like a color-capable terminal or IDE console. Use accessLog.ForceConsoleColor() / accessLog.DisableConsoleColor() to override the auto-detection.

Custom formatter

Custom formatters now write directly to an io.Writer and receive zero-copy request views:

h.Use(accessLog.LoggerWithFormatter(func(out io.Writer, param *accessLog.LogFormatterParams) {
    _, _ = out.Write(param.Method)
    _, _ = out.Write([]byte(" "))
    _, _ = out.Write(param.AppendRequestURI(nil))
}))

Method, Path, Query, Host, Request, and Keys are only valid during the formatter call and must not be retained after it returns.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWriter io.Writer = os.Stdout

Functions

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() app.HandlerFunc

Logger instances a low-allocation logger middleware that writes directly to DefaultWriter.

func LoggerWithConfig

func LoggerWithConfig(conf LoggerConfig) app.HandlerFunc

LoggerWithConfig instances a low-allocation logger middleware with config.

func LoggerWithFormatter

func LoggerWithFormatter(f LogFormatter) app.HandlerFunc

LoggerWithFormatter instances a low-allocation logger middleware with the specified log formatter.

func LoggerWithWriter

func LoggerWithWriter(out io.Writer, notLogged ...string) app.HandlerFunc

LoggerWithWriter instances a low-allocation logger middleware with the specified writer buffer.

Types

type LogFormatter

type LogFormatter func(w io.Writer, params *LogFormatterParams)

LogFormatter writes log output directly to w.

Implementations must not retain params or any byte slices, map entries, or request pointers referenced by params after the function returns.

type LogFormatterParams

type LogFormatterParams struct {
	Request *protocol.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
	// Method is the HTTP method given to the request.
	Method []byte
	// Path is the raw path requested by the client.
	Path []byte
	// Query is the raw query string without the leading '?'.
	Query []byte
	// Host is the Host header requested by the client.
	Host []byte

	// Keys are the keys set on the request's context. Treat as read-only.
	Keys map[string]any
	// contains filtered or unexported fields
}

LogFormatterParams contains zero-copy views into the current request.

Request, Method, Path, Query, Host, and Keys are owned by the active RequestContext and are only valid during the formatter call.

func (*LogFormatterParams) AppendErrorMessage

func (p *LogFormatterParams) AppendErrorMessage(dst []byte) []byte

AppendErrorMessage appends the current private error message without extra string allocations.

func (*LogFormatterParams) AppendRequestURI

func (p *LogFormatterParams) AppendRequestURI(dst []byte) []byte

AppendRequestURI appends the current request URI to dst without extra allocations.

func (*LogFormatterParams) BodySize

func (p *LogFormatterParams) BodySize() int

BodySize returns the response Content-Length when known. It is resolved lazily.

func (*LogFormatterParams) ClientIP

func (p *LogFormatterParams) ClientIP() string

ClientIP returns the client IP. It is resolved lazily.

func (*LogFormatterParams) ErrorMessage

func (p *LogFormatterParams) ErrorMessage() string

ErrorMessage returns private errors for the request. It is resolved lazily.

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 defaultLogFormatter.
	Formatter LogFormatter

	// Output is a writer where logs are written.
	// Optional. Default value is os.Stdout.
	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