accesslog

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultBufferSize = 64 * 1024 // 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 ParseLogTime

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

Types

type AccessLogIO

type AccessLogIO interface {
	io.ReadWriteCloser
	io.ReadWriteSeeker
	io.ReaderAt
	sync.Locker
	Name() string // file name or path
	Truncate(size int64) error
}

type AccessLogger

type AccessLogger struct {
	Formatter
	// contains filtered or unexported fields
}

func NewAccessLogger

func NewAccessLogger(parent task.Parent, io AccessLogIO, cfg *Config) *AccessLogger

func NewFileAccessLogger

func NewFileAccessLogger(parent task.Parent, cfg *Config) (*AccessLogger, error)

func (*AccessLogger) Config

func (l *AccessLogger) Config() *Config

func (*AccessLogger) Flush

func (l *AccessLogger) Flush() error

func (*AccessLogger) Log

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

func (*AccessLogger) LogError

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

func (*AccessLogger) Rotate

func (l *AccessLogger) Rotate() 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 AccessLogIO, 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) 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{ types.CIDR }

func (CIDR) Fulfill

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

type CombinedFormatter

type CombinedFormatter struct{ CommonFormatter }

func (*CombinedFormatter) Format

func (f *CombinedFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response)

type CommonFormatter

type CommonFormatter struct {
	GetTimeNow func() time.Time // for testing purposes only
	// contains filtered or unexported fields
}

func (*CommonFormatter) Format

func (f *CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response)

func (*CommonFormatter) SetGetTimeNow

func (f *CommonFormatter) SetGetTimeNow(getTimeNow func() time.Time)

debug only.

type Config

type Config struct {
	BufferSize int        `json:"buffer_size"`
	Format     Format     `json:"format" validate:"oneof=common combined json"`
	Path       string     `json:"path" validate:"required"`
	Filters    Filters    `json:"filters"`
	Fields     Fields     `json:"fields"`
	Retention  *Retention `json:"retention"`
}

func DefaultConfig

func DefaultConfig() *Config

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) ProcessCookies

func (cfg *FieldConfig) ProcessCookies(cookies []*http.Cookie) map[string]string

func (*FieldConfig) ProcessHeaders

func (cfg *FieldConfig) ProcessHeaders(headers http.Header) http.Header

func (*FieldConfig) ProcessQuery

func (cfg *FieldConfig) ProcessQuery(q url.Values) url.Values

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
	sync.Mutex
	// 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"
)

type Formatter

type Formatter interface {
	// Format writes a log line to line without a trailing newline
	Format(line *bytes.Buffer, req *http.Request, res *http.Response)
	SetGetTimeNow(getTimeNow func() time.Time)
}

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) Format

func (f *JSONFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response)

type JSONLogEntry

type JSONLogEntry struct {
	Time        string              `json:"time"`
	IP          string              `json:"ip"`
	Method      string              `json:"method"`
	Scheme      string              `json:"scheme"`
	Host        string              `json:"host"`
	URI         string              `json:"uri"`
	Protocol    string              `json:"protocol"`
	Status      int                 `json:"status"`
	Error       string              `json:"error,omitempty"`
	ContentType string              `json:"type"`
	Size        int64               `json:"size"`
	Referer     string              `json:"referer"`
	UserAgent   string              `json:"useragent"`
	Query       map[string][]string `json:"query,omitempty"`
	Headers     map[string][]string `json:"headers,omitempty"`
	Cookies     map[string]string   `json:"cookies,omitempty"`
}

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 {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*MockFile) Close

func (m *MockFile) Close() error

func (*MockFile) Content

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

func (*MockFile) Len

func (m *MockFile) Len() int64

func (*MockFile) LineCount

func (m *MockFile) LineCount() int

func (*MockFile) Name

func (m *MockFile) Name() string

func (*MockFile) Read

func (m *MockFile) Read(p []byte) (n int, err error)

func (*MockFile) ReadAt

func (m *MockFile) ReadAt(p []byte, off int64) (n int, err error)

func (*MockFile) Seek

func (m *MockFile) Seek(offset int64, whence int) (int64, error)

func (*MockFile) Truncate

func (m *MockFile) Truncate(size int64) error

func (*MockFile) Write

func (m *MockFile) Write(p []byte) (n int, err error)

type Retention

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

func (*Retention) Parse

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

Syntax:

<N> days|weeks|months

last <N>

Parse implements strutils.Parser.

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

Jump to

Keyboard shortcuts

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