Documentation
¶
Overview ¶
Package glogs is a small structured logging facade for Go services.
It provides named channels, per-channel rolling files, ANSI colored console output, structured text/JSON output and request context fields.
Index ¶
Constants ¶
const DefaultMask = "***"
DefaultMask replaces a redacted value.
const Version = "0.0.0-dev"
Version is the semantic version of this library.
Variables ¶
var DefaultChannels = []Channel{ ChannelAccess, ChannelDefault, ChannelDepend, ChannelApp, ChannelError, ChannelEvent, }
DefaultChannels is the default channel set.
var DefaultSensitiveKeys = []string{
"password", "passwd", "pwd", "secret", "token", "accesstoken", "refreshtoken",
"verifytoken", "authorization", "cookie", "set-cookie", "credential",
"privatekey", "apikey", "api-key", "sessionid", "session", "salt",
"idcard", "idnumber", "bankcard", "cvv",
"密码", "令牌", "密钥",
}
DefaultSensitiveKeys is the deny list applied by key name, case-insensitively. It is intentionally coarse: a false positive costs a masked debug field, a false negative writes a credential to disk.
var ErrWriterClosed = errors.New("glogs: writer is closed")
ErrWriterClosed means a write arrived after Close.
Functions ¶
Types ¶
type AsyncStats ¶
AsyncStats reports what an async writer did. Dropping is lossy by design: a log line must never block a request goroutine, and the queue is bounded so memory cannot grow without limit. The counter is how that trade-off stays visible.
type ChannelConfig ¶
type ChannelConfig struct {
Enabled bool
Level slog.Level
Path string
Console bool
MaxSize int64 // bytes per file, default 50 MiB
MaxAge int // days, default 7
TotalCap int64 // bytes, default 500 MiB
// Async moves the file write off the caller's goroutine. The queue is
// bounded by AsyncBuffer; when it is full the line is dropped and counted
// rather than blocking the request. Off by default so an existing deployment
// keeps its current durability.
Async bool
// AsyncBuffer is the queue depth when Async is set. Default 1024.
AsyncBuffer int
}
ChannelConfig configures one channel's file output and rotation.
type Config ¶
type Config struct {
AppName string
Dir string // base directory, default "./logs"
Root ConsoleConfig
Level slog.Level
Channels map[Channel]ChannelConfig
// Redactor masks sensitive attributes and bodies. Nil disables redaction,
// which is only appropriate for trusted local debugging: a logged credential
// lives in the file forever.
Redactor Redactor
// OnError is told about every file write, rotation and cleanup failure.
// slog discards handler errors, so this is the only way a broken disk becomes
// visible instead of silently dropping log lines.
OnError func(channel Channel, err error)
}
Config is the top-level logging configuration.
func DefaultConfig ¶
DefaultConfig returns a configuration matching the platform defaults.
type ConsoleConfig ¶
ConsoleConfig configures console output.
type FileStats ¶
FileStats reports the failures a rolling writer has seen. A non-zero Errors with an empty log file is the signature of a broken disk, not of a quiet service.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger holds the root console logger and the named channel loggers.
func (*Logger) AsyncStats ¶
func (l *Logger) AsyncStats(channel Channel) (AsyncStats, error)
AsyncStats reports drop and error counters for one channel. It returns an error when that channel does not use an async writer.
func (*Logger) Channel ¶
Channel returns the logger for a named channel. Unknown channels fall back to the root logger.
func (*Logger) Close ¶
Close flushes and closes all file writers. Close drains async writers, then closes the files. The drain is bounded: a disk that stopped accepting writes must not hang shutdown, and unsent lines are reported through AsyncStats instead of disappearing silently.
type MaskRedactor ¶
type MaskRedactor struct {
// Keys extends DefaultSensitiveKeys.
Keys []string
// Mask is the replacement text; DefaultMask when empty.
Mask string
// MaxBodyBytes bounds how much of a body is walked or returned.
MaxBodyBytes int
}
MaskRedactor is the default Redactor: substring match on key names plus a JSON-field walker for bodies.
func NewMaskRedactor ¶
func NewMaskRedactor(mask string, extraKeys ...string) *MaskRedactor
NewMaskRedactor builds a redactor with the default deny list plus extra keys.
func (*MaskRedactor) RedactBody ¶
func (r *MaskRedactor) RedactBody(body []byte, maskAll bool) string
RedactBody implements Redactor.
A body that cannot be parsed as JSON is masked entirely when maskAll is set (a non-JSON body might be anything, so keeping it would defeat the purpose), and returned untouched otherwise. The size bound keeps a large upload from being copied into the log path.
func (*MaskRedactor) RedactValue ¶
func (r *MaskRedactor) RedactValue(key string, value any) any
RedactValue implements Redactor.
func (*MaskRedactor) Sensitive ¶
func (r *MaskRedactor) Sensitive(key string) bool
Sensitive implements Redactor.
type Redactor ¶
type Redactor interface {
// RedactValue masks a value whose key matched (or returns it unchanged).
RedactValue(key string, value any) any
// Sensitive reports whether an attribute key is sensitive.
Sensitive(key string) bool
// RedactBody masks the sensitive fields of a JSON request/response body and
// returns the rewritten body. Bodies that are not JSON are returned as is
// unless maskAll is set.
RedactBody(body []byte, maskAll bool) string
}
Redactor decides what must not reach a log file, and what a masked value looks like. The default implementation covers attribute keys and JSON bodies; a deployment can replace it.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package access provides HTTP access logging middleware backed by glogs.
|
Package access provides HTTP access logging middleware backed by glogs. |
|
Package bridge captures the output of loggers that are not slog-based.
|
Package bridge captures the output of loggers that are not slog-based. |
|
examples
|
|
|
basic
command
|