accesslog

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinBufferSize = 4 * kilobyte
	MaxBufferSize = 1 * megabyte
)
View Source
const DefaultBufferSize = 64 * kilobyte // 64KB
View Source
const LogTimeFormat = "02/Jan/2006:15:04:05 -0700"

Variables

View Source
var (
	ErrInvalidSyntax = gperr.New("invalid syntax")
	ErrZeroValue     = gperr.New("zero value")
)
View Source
var ErrInvalidHTTPHeaderFilter = gperr.New("invalid http header filter")
View Source
var ErrInvalidStatusCodeRange = gperr.New("invalid status code range")

Functions

func ExtractTime

func ExtractTime(line []byte) []byte

ExtractTime extracts the time from the log line. It returns the time if the time is found, otherwise it returns nil.

The returned time is not validated.

func ParseLogTime

func ParseLogTime(line []byte) (t time.Time)

ParseLogTime parses the time from the log line. It returns the time if the time is found and valid in the log line, otherwise it returns zero time.

Types

type ACLFormatter

type ACLFormatter interface {
	// AppendACLLog appends a log line to line with or without a trailing newline
	AppendACLLog(line []byte, info *acl.IPInfo, blocked bool) []byte
}

type ACLLogFormatter

type ACLLogFormatter struct{}

func (ACLLogFormatter) AppendACLLog

func (f ACLLogFormatter) AppendACLLog(line []byte, info *acl.IPInfo, blocked bool) []byte

type ACLLoggerConfig

type ACLLoggerConfig struct {
	ConfigBase
	LogAllowed bool `json:"log_allowed"`
}

func DefaultACLLoggerConfig

func DefaultACLLoggerConfig() *ACLLoggerConfig

func (*ACLLoggerConfig) ToConfig

func (cfg *ACLLoggerConfig) ToConfig() *Config

type AccessLogger

type AccessLogger struct {
	RequestFormatter
	ACLFormatter
	// contains filtered or unexported fields
}

func NewAccessLogger

func NewAccessLogger(parent task.Parent, cfg AnyConfig) (*AccessLogger, error)

func NewAccessLoggerWithIO

func NewAccessLoggerWithIO(parent task.Parent, writer WriterWithName, anyCfg AnyConfig) *AccessLogger

func NewMockAccessLogger

func NewMockAccessLogger(parent task.Parent, cfg *RequestLoggerConfig) *AccessLogger

func (*AccessLogger) Close

func (l *AccessLogger) Close() error

func (*AccessLogger) Config

func (l *AccessLogger) Config() *Config

func (*AccessLogger) Flush

func (l *AccessLogger) Flush()

func (*AccessLogger) Log

func (l *AccessLogger) Log(req *http.Request, res *http.Response)

func (*AccessLogger) LogACL

func (l *AccessLogger) LogACL(info *acl.IPInfo, blocked bool)

func (*AccessLogger) LogError

func (l *AccessLogger) LogError(req *http.Request, err error)

func (*AccessLogger) Rotate

func (l *AccessLogger) Rotate() (result *RotateResult, err error)

func (*AccessLogger) ShouldRotate

func (l *AccessLogger) ShouldRotate() bool

type AnyConfig

type AnyConfig interface {
	ToConfig() *Config
	IO() (WriterWithName, error)
}

type BackScanner

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

BackScanner provides an interface to read a file backward line by line.

func NewBackScanner

func NewBackScanner(file ReaderAtSeeker, chunkSize int) *BackScanner

NewBackScanner creates a new Scanner to read the file backward. chunkSize determines the size of each read chunk from the end of the file.

func (*BackScanner) Bytes

func (s *BackScanner) Bytes() []byte

Bytes returns the most recent line generated by a call to Scan.

func (*BackScanner) Err

func (s *BackScanner) Err() error

Err returns the first non-EOF error encountered by the scanner.

func (*BackScanner) FileSize

func (s *BackScanner) FileSize() int64

FileSize returns the size of the file.

func (*BackScanner) Reset

func (s *BackScanner) Reset() error

func (*BackScanner) Scan

func (s *BackScanner) Scan() bool

Scan advances the scanner to the previous line, which will then be available via the Bytes method. It returns false when there are no more lines.

type CIDR

type CIDR struct {
	gpnet.CIDR
}

func (*CIDR) Fulfill

func (cidr *CIDR) Fulfill(req *http.Request, res *http.Response) bool

type CombinedFormatter

type CombinedFormatter struct{ CommonFormatter }

func (*CombinedFormatter) AppendRequestLog

func (f *CombinedFormatter) AppendRequestLog(line []byte, req *http.Request, res *http.Response) []byte

type CommonFormatter

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

func (*CommonFormatter) AppendRequestLog

func (f *CommonFormatter) AppendRequestLog(line []byte, req *http.Request, res *http.Response) []byte

type Config

type Config struct {
	*ConfigBase
	// contains filtered or unexported fields
}

type ConfigBase

type ConfigBase struct {
	BufferSize int        `json:"buffer_size"`
	Path       string     `json:"path"`
	Stdout     bool       `json:"stdout"`
	Retention  *Retention `json:"retention" aliases:"keep"`
}

func (*ConfigBase) IO

func (cfg *ConfigBase) IO() (WriterWithName, error)

func (*ConfigBase) Validate

func (cfg *ConfigBase) Validate() gperr.Error

type FieldConfig

type FieldConfig struct {
	Default FieldMode            `json:"default" validate:"oneof=keep drop redact"`
	Config  map[string]FieldMode `json:"config" validate:"dive,oneof=keep drop redact"`
}

func (*FieldConfig) IterCookies

func (cfg *FieldConfig) IterCookies(cookies []*http.Cookie) iter.Seq2[string, string]

func (*FieldConfig) IterHeaders

func (cfg *FieldConfig) IterHeaders(headers http.Header) iter.Seq2[string, []string]

func (*FieldConfig) IterQuery

func (cfg *FieldConfig) IterQuery(q url.Values) iter.Seq2[string, []string]

func (*FieldConfig) ZerologCookies

func (cfg *FieldConfig) ZerologCookies(cookies []*http.Cookie) zerolog.LogObjectMarshaler

func (*FieldConfig) ZerologHeaders

func (cfg *FieldConfig) ZerologHeaders(headers http.Header) zerolog.LogObjectMarshaler

func (*FieldConfig) ZerologQuery

func (cfg *FieldConfig) ZerologQuery(q url.Values) zerolog.LogObjectMarshaler

type FieldMode

type FieldMode string
const (
	FieldModeKeep   FieldMode = "keep"
	FieldModeDrop   FieldMode = "drop"
	FieldModeRedact FieldMode = "redact"

	RedactedValue = "REDACTED"
)

type Fields

type Fields struct {
	Headers FieldConfig `json:"headers"`
	Query   FieldConfig `json:"query"`
	Cookies FieldConfig `json:"cookies"`
}

type File

type File struct {
	*os.File
	// contains filtered or unexported fields
}

func (*File) Close

func (f *File) Close() error

type Filterable

type Filterable interface {
	comparable
	Fulfill(req *http.Request, res *http.Response) bool
}

type Filters

type Filters struct {
	StatusCodes LogFilter[*StatusCodeRange] `json:"status_codes"`
	Method      LogFilter[HTTPMethod]       `json:"method"`
	Host        LogFilter[Host]             `json:"host"`
	Headers     LogFilter[*HTTPHeader]      `json:"headers"` // header exists or header == value
	CIDR        LogFilter[*CIDR]            `json:"cidr"`
}

type Format

type Format string
var (
	FormatCommon   Format = "common"
	FormatCombined Format = "combined"
	FormatJSON     Format = "json"

	ReqLoggerFormats = []Format{FormatCommon, FormatCombined, FormatJSON}
)

type HTTPHeader

type HTTPHeader struct {
	Key, Value string
}

func (*HTTPHeader) Fulfill

func (k *HTTPHeader) Fulfill(req *http.Request, res *http.Response) bool

func (*HTTPHeader) Parse

func (k *HTTPHeader) Parse(v string) error

Parse implements strutils.Parser.

type HTTPMethod

type HTTPMethod string

func (HTTPMethod) Fulfill

func (method HTTPMethod) Fulfill(req *http.Request, res *http.Response) bool

type Host

type Host string

func (Host) Fulfill

func (h Host) Fulfill(req *http.Request, res *http.Response) bool

type JSONFormatter

type JSONFormatter struct{ CommonFormatter }

func (*JSONFormatter) AppendRequestLog

func (f *JSONFormatter) AppendRequestLog(line []byte, req *http.Request, res *http.Response) []byte

type LogFilter

type LogFilter[T Filterable] struct {
	Negative bool
	Values   []T
}

func (*LogFilter[T]) CheckKeep

func (f *LogFilter[T]) CheckKeep(req *http.Request, res *http.Response) bool

type MockFile

type MockFile struct {
	afero.File
	// contains filtered or unexported fields
}

func NewMockFile

func NewMockFile() *MockFile

func (*MockFile) Content

func (m *MockFile) Content() []byte

func (*MockFile) Len

func (m *MockFile) Len() int64

func (MockFile) Lock

func (MockFile) Lock()

func (*MockFile) NumLines

func (m *MockFile) NumLines() int

func (MockFile) Unlock

func (MockFile) Unlock()

type MultiWriter

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

func (*MultiWriter) Name

func (w *MultiWriter) Name() string

func (*MultiWriter) Unwrap

func (w *MultiWriter) Unwrap() []io.Writer

func (*MultiWriter) Write

func (w *MultiWriter) Write(p []byte) (n int, err error)

type MultiWriterInterface

type MultiWriterInterface interface {
	Unwrap() []io.Writer
}

type ReaderAtSeeker

type ReaderAtSeeker interface {
	io.ReaderAt
	io.Seeker
}

type RequestFormatter

type RequestFormatter interface {
	// AppendRequestLog appends a log line to line with or without a trailing newline
	AppendRequestLog(line []byte, req *http.Request, res *http.Response) []byte
}

type RequestLoggerConfig

type RequestLoggerConfig struct {
	ConfigBase
	Format  Format  `json:"format" validate:"oneof=common combined json"`
	Filters Filters `json:"filters"`
	Fields  Fields  `json:"fields"`
}

func DefaultRequestLoggerConfig

func DefaultRequestLoggerConfig() *RequestLoggerConfig

func (*RequestLoggerConfig) ToConfig

func (cfg *RequestLoggerConfig) ToConfig() *Config

type Retention

type Retention struct {
	Days     uint64 `json:"days"`
	Last     uint64 `json:"last"`
	KeepSize uint64 `json:"keep_size"`
}

func (*Retention) IsValid

func (r *Retention) IsValid() bool

func (*Retention) Parse

func (r *Retention) Parse(v string) (err error)

Syntax:

<N> days|weeks|months

last <N>

<N> KB|MB|GB|kb|mb|gb

Parse implements strutils.Parser.

func (*Retention) String

func (r *Retention) String() string

type RotateResult

type RotateResult struct {
	Filename        string
	OriginalSize    int64 // original size of the file
	NumBytesRead    int64 // number of bytes read from the file
	NumBytesKeep    int64 // number of bytes to keep
	NumLinesRead    int   // number of lines read from the file
	NumLinesKeep    int   // number of lines to keep
	NumLinesInvalid int   // number of invalid lines
}

func (*RotateResult) Add

func (r *RotateResult) Add(other *RotateResult)

func (*RotateResult) Print

func (r *RotateResult) Print(logger *zerolog.Logger)

type StatusCodeRange

type StatusCodeRange struct {
	Start int
	End   int
}

func (*StatusCodeRange) Fulfill

func (r *StatusCodeRange) Fulfill(req *http.Request, res *http.Response) bool

func (*StatusCodeRange) Includes

func (r *StatusCodeRange) Includes(code int) bool

func (*StatusCodeRange) Parse

func (r *StatusCodeRange) Parse(v string) error

Parse implements strutils.Parser.

func (*StatusCodeRange) String

func (r *StatusCodeRange) String() string

type StdoutLogger

type StdoutLogger struct {
	io.Writer
}

func (*StdoutLogger) Name

func (l *StdoutLogger) Name() string

type WriterWithName

type WriterWithName interface {
	io.Writer
	Name() string // file name or path
}

func NewMultiWriter

func NewMultiWriter(writers ...WriterWithName) WriterWithName

Jump to

Keyboard shortcuts

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