Documentation
¶
Overview ¶
Package logger provides colorized console logging and file-based access/error logging.
Index ¶
- func AccessClose()
- func AccessEnabled() bool
- func AccessReopenFiles() error
- func ClientIP(r *http.Request) string
- func Close()
- func LogAccess(routeID string, entry AccessEntry)
- func NewServerErrorLog() *log.Logger
- func ParseLevel(s string) slog.Level
- func ReopenFiles() error
- func Setup(cfg Config) error
- func SetupAccess(globalPath string, routePaths map[string]string) error
- type AccessEntry
- type Config
- type ConsoleHandler
- type FileWriter
- type ResponseCapture
- func (rc *ResponseCapture) BytesOut() int64
- func (rc *ResponseCapture) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (rc *ResponseCapture) Reset(w http.ResponseWriter)
- func (rc *ResponseCapture) Status() int
- func (rc *ResponseCapture) Unwrap() http.ResponseWriter
- func (rc *ResponseCapture) Write(b []byte) (int, error)
- func (rc *ResponseCapture) WriteHeader(code int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessEnabled ¶
func AccessEnabled() bool
AccessEnabled reports whether any access log output is active (file or console). Used by the handler to skip building AccessEntry when nothing would be emitted.
func AccessReopenFiles ¶
func AccessReopenFiles() error
AccessReopenFiles reopens all access log files (called during SIGHUP rotation).
func ClientIP ¶
ClientIP extracts the client IP address from a request, checking X-Forwarded-For, X-Real-Ip, and RemoteAddr in order.
func LogAccess ¶
func LogAccess(routeID string, entry AccessEntry)
LogAccess writes an access log entry to the appropriate file and console.
Console output uses slog with level based on status code:
2xx/3xx → info, 4xx → warn, 5xx → error
File output is JSON lines (one object per line).
func NewServerErrorLog ¶
NewServerErrorLog returns a *log.Logger that routes all messages through slog at DEBUG level. Set this as the ErrorLog field on http.Server.
func ParseLevel ¶
ParseLevel converts a level string to slog.Level.
func ReopenFiles ¶
func ReopenFiles() error
ReopenFiles closes and re-opens all log files. Designed for SIGHUP-triggered rotation with external tools like logrotate.
func Setup ¶
Setup configures the global slog default with a colorized console handler and an optional error log file handler.
func SetupAccess ¶
SetupAccess configures access log file destinations. globalPath sets the default access log file. routePaths maps route IDs to per-route log files. The special value "off" disables all access logging (both file and console) for that route. Routes without an explicit path fall back to the global file.
Types ¶
type AccessEntry ¶
type AccessEntry struct {
Timestamp time.Time `json:"ts"`
Service string `json:"service"`
Method string `json:"method"`
Path string `json:"path"`
Status int `json:"status"`
Duration float64 `json:"duration_ms"`
BytesOut int64 `json:"bytes_out"`
ClientIP string `json:"client_ip"`
UserAgent string `json:"user_agent,omitempty"`
}
AccessEntry represents a single access log record.
type Config ¶
type Config struct {
Level string // "debug", "info", "warn", "error"
ErrorLog string // file path for error-level log (empty = disabled)
}
Config holds logger initialization parameters.
type ConsoleHandler ¶
type ConsoleHandler struct {
// contains filtered or unexported fields
}
ConsoleHandler is a slog.Handler that writes colorized, compact log lines to a writer.
Output format:
15:04:05 INF message key=value key2=value2
func NewConsoleHandler ¶
func NewConsoleHandler(w io.Writer, opts *slog.HandlerOptions) *ConsoleHandler
NewConsoleHandler creates a handler that writes compact, human-friendly log lines. Color output is auto-detected based on terminal capabilities.
type FileWriter ¶
type FileWriter struct {
// contains filtered or unexported fields
}
FileWriter is a thread-safe file writer that supports reopen for log rotation. Designed to work with external rotation tools (e.g. logrotate) via SIGHUP.
func NewFileWriter ¶
func NewFileWriter(path string) (*FileWriter, error)
NewFileWriter opens a file in append mode for writing log entries.
func (*FileWriter) Close ¶
func (w *FileWriter) Close() error
Close flushes and closes the underlying file.
func (*FileWriter) Reopen ¶
func (w *FileWriter) Reopen() error
Reopen closes and re-opens the file. This allows external log rotation tools to rename the file and have the writer create a fresh one.
type ResponseCapture ¶
type ResponseCapture struct {
http.ResponseWriter
// contains filtered or unexported fields
}
ResponseCapture wraps http.ResponseWriter to record the status code and bytes written. It is used by the access log middleware. When the connection is hijacked (WebSocket), Status() returns 101 to reflect the protocol switch.
func NewResponseCapture ¶
func NewResponseCapture(w http.ResponseWriter) *ResponseCapture
NewResponseCapture wraps a ResponseWriter for access log capturing.
func (*ResponseCapture) BytesOut ¶
func (rc *ResponseCapture) BytesOut() int64
BytesOut returns the total bytes written to the response body.
func (*ResponseCapture) Hijack ¶
func (rc *ResponseCapture) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack implements http.Hijacker. It marks the connection as hijacked so Status() reports 101 (Switching Protocols) in the access log.
func (*ResponseCapture) Reset ¶
func (rc *ResponseCapture) Reset(w http.ResponseWriter)
Reset reinitializes a ResponseCapture for reuse from a pool.
func (*ResponseCapture) Status ¶
func (rc *ResponseCapture) Status() int
Status returns the recorded HTTP status code. For hijacked connections (WebSocket), returns 101 Switching Protocols.
func (*ResponseCapture) Unwrap ¶
func (rc *ResponseCapture) Unwrap() http.ResponseWriter
Unwrap returns the original ResponseWriter. This allows http.ResponseController to discover interfaces like http.Flusher and http.Hijacker on the inner writer.
func (*ResponseCapture) WriteHeader ¶
func (rc *ResponseCapture) WriteHeader(code int)