Documentation
¶
Index ¶
- Constants
- Variables
- func AsStrings[T any](input []T) []string
- func IsInterrupted(err error) bool
- func RealIPMiddleware(opts ...realIPOption) func(http.Handler) http.Handler
- func WithDate(format string) option
- func WithHostAsPeer() middlewareOption
- func WithLevel(level Level) option
- func WithTarget(out io.Writer) option
- func WithTrustedHeaders(headers ...string) realIPOption
- func WithTrustedProxies(proxies ...string) realIPOption
- type Level
- type Plain
- func (p *Plain) Confirm(prompt string, defaultYes bool) (bool, error)
- func (p *Plain) ConfirmWithEcho(prompt string, defaultYes bool, prefix string) (bool, error)
- func (p *Plain) Debug(a ...any)
- func (p *Plain) Debugf(format string, a ...any)
- func (p *Plain) Debugln(a ...any)
- func (p *Plain) Error(a ...any)
- func (p *Plain) Errorf(format string, a ...any)
- func (p *Plain) Errorln(a ...any)
- func (p *Plain) Info(a ...any)
- func (p *Plain) Infof(format string, a ...any)
- func (p *Plain) Infoln(a ...any)
- func (p *Plain) LogRequest(request *http.Request, metrics *httpsnoop.Metrics)
- func (p *Plain) Middleware(opts ...middlewareOption) func(http.Handler) http.Handler
- func (p *Plain) MustExit(err error)
- func (p *Plain) MustFail(err error)
- func (p *Plain) OnSignal(sig os.Signal, handler func())
- func (p *Plain) OnSignalContext(ctx context.Context, sig os.Signal, handler func())
- func (p *Plain) Print(a ...any)
- func (p *Plain) Printf(format string, a ...any)
- func (p *Plain) Println(a ...any)
- func (p *Plain) Read(prompt string, max int) (string, error)
- func (p *Plain) ReadHidden(prompt string) (string, error)
- func (p *Plain) ReadMask(prompt string, mask rune) (string, error)
- func (p *Plain) ReadOne(prompt string, echo bool) (rune, error)
- func (p *Plain) Select(prompt string, options []string) (int, error)
- func (p *Plain) SelectWithDescription(prompt string, options []SelectOption) (int, error)
- func (p *Plain) SetDate(format string)
- func (p *Plain) SetLevel(level Level)
- func (p *Plain) SetTarget(out io.Writer)
- func (p *Plain) Theme(c themeColor) string
- func (p *Plain) WaitForInterrupt()
- func (p *Plain) Warn(a ...any)
- func (p *Plain) Warnf(format string, a ...any)
- func (p *Plain) Warnln(a ...any)
- func (p *Plain) Write(code, msg string, reset, newline, noHeader bool)
- func (p *Plain) Writeln(code, msg string, reset, noHeader bool)
- type SelectOption
- type Theme
Constants ¶
const ( MaskStar = '*' MaskDot = '-' MaskHash = '#' )
Mask constants for ReadMask. Pass any rune to ReadMask; these are convenience values for common masking characters. MaskNone disables on-screen echoing entirely (behaviour equivalent to ReadHidden, but with backspace handling).
const ( // HeaderXRealIP is the X-Real-IP header. HeaderXRealIP = "X-Real-IP" // HeaderXForwardedFor is the X-Forwarded-For header. HeaderXForwardedFor = "X-Forwarded-For" )
const ( Dimmed themeColor = iota Success Highlight Input Warn Error Reset )
const AnsiReset = ansi.AnsiReset
const RFC3339Local = "2006-01-02T15:04:05"
RFC3339Local is an RFC3339-like time format without timezone information
Variables ¶
var ( // ErrInterrupted indicates that an input operation was cancelled. ErrInterrupted = internal.ErrInterrupted // ErrNoOptions indicates that a selector was opened without any options. ErrNoOptions = errors.New("no options") )
Functions ¶
func IsInterrupted ¶ added in v1.2.1
IsInterrupted checks if the given err is ErrInterrupted
func RealIPMiddleware ¶ added in v1.6.4
RealIPMiddleware replaces the request's remote address with the first valid IP found in the trusted headers. It defaults to X-Real-IP and X-Forwarded-For for requests originating from 127.0.0.1/32.
func WithDate ¶
func WithDate(format string) option
WithDate sets the timestamp format used in log headers (empty disables timestamps)
func WithHostAsPeer ¶ added in v1.6.5
func WithHostAsPeer() middlewareOption
WithHostAsPeer logs request.Host instead of the remote address, using the same port stripping.
func WithLevel ¶ added in v1.5.0
func WithLevel(level Level) option
WithLevel sets the minimum log level. Messages below this level are discarded.
func WithTarget ¶
WithTarget sets the output writer used by the logger
func WithTrustedHeaders ¶ added in v1.6.4
func WithTrustedHeaders(headers ...string) realIPOption
WithTrustedHeaders sets the headers used to determine the client's IP address.
func WithTrustedProxies ¶ added in v1.6.4
func WithTrustedProxies(proxies ...string) realIPOption
WithTrustedProxies restricts trusted headers to requests from the given IP addresses or CIDR ranges. Invalid proxies are ignored.
Types ¶
type Plain ¶
type Plain struct {
// contains filtered or unexported fields
}
Plain is a small, allocation-conscious logger with optional ANSI color output
func New ¶
func New(opts ...option) *Plain
New creates a Plain logger configured by the provided options
func (*Plain) Confirm ¶
Confirm displays a prompt aligned with the logger's format, reads y/n confirmation from stdin and prints a newline
func (*Plain) ConfirmWithEcho ¶ added in v1.1.8
ConfirmWithEcho displays a prompt aligned with the logger's format, reads y/n confirmation from stdin and echoes the chosen input (optionally prefixed) before printing a newline.
func (*Plain) Debug ¶
Debug formats using the default formats for its operands and writes to the target output as a debug log.
func (*Plain) Debugf ¶
Debugf formats according to a format specifier and writes to the target output as a debug log.
func (*Plain) Debugln ¶
Debugln formats using the default formats for its operands and writes to the target output as a debug log with a trailing newline.
func (*Plain) Error ¶
Error formats using the default formats for its operands and writes to the target output as an error.
func (*Plain) Errorf ¶
Errorf formats according to a format specifier and writes to the target output as an error.
func (*Plain) Errorln ¶
Errorln formats using the default formats for its operands and writes to the target output as an error with a trailing newline.
func (*Plain) LogRequest ¶
LogRequest writes a single formatted access log line for request using the provided metrics
func (*Plain) Middleware ¶
Middleware returns an http middleware that logs each request after it has been handled
func (*Plain) MustExit ¶ added in v1.1.5
MustExit logs the error and exits with code 1 if the error is not nil.
func (*Plain) OnSignal ¶ added in v1.5.0
OnSignal registers a non-blocking handler that executes the provided callback every time the specified signal is received.
func (*Plain) OnSignalContext ¶ added in v1.6.0
OnSignalContext registers a non-blocking handler until ctx is cancelled.
func (*Plain) Print ¶
Print formats using the default formats for its operands and writes to the target output.
func (*Plain) Printf ¶
Printf formats according to a format specifier and writes to the target output.
func (*Plain) Println ¶
Println formats using the default formats for its operands and writes to the target output with a trailing newline.
func (*Plain) Read ¶
Read displays a prompt aligned with the logger's format and reads max bytes from stdin.
func (*Plain) ReadHidden ¶ added in v1.4.4
ReadHidden displays a prompt aligned with the logger's format, reads a line from stdin without echoing input and prints a newline.
func (*Plain) ReadMask ¶ added in v1.4.4
ReadMask displays a prompt aligned with the logger's format, reads a line from stdin masking each typed byte on screen, and prints a newline.
func (*Plain) ReadOne ¶
ReadOne displays a prompt aligned with the logger's format and reads 1 byte from stdin.
func (*Plain) SelectWithDescription ¶ added in v1.5.1
func (p *Plain) SelectWithDescription(prompt string, options []SelectOption) (int, error)
SelectWithDescription displays a cyclic selector with the selected option's description on the line below it.
func (*Plain) SetDate ¶ added in v1.1.9
SetDate updates the timestamp format used in log headers (empty disables timestamps)
func (*Plain) WaitForInterrupt ¶ added in v1.1.6
func (p *Plain) WaitForInterrupt()
WaitForInterrupt blocks until SIGINT or SIGTERM is received
func (*Plain) Warn ¶
Warn formats using the default formats for its operands and writes to the target output as a warning.
func (*Plain) Warnf ¶
Warnf formats according to a format specifier and writes to the target output as a warning.
func (*Plain) Warnln ¶
Warnln formats using the default formats for its operands and writes to the target output as a warning with a trailing newline.
type SelectOption ¶ added in v1.5.1
SelectOption provides the label and description for SelectWithDescription.