logging

package
v1.0.54 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package logging provides file-based structured logging for diagnostics. Logs are written as JSON lines to ~/.dws/logs/dws.log with automatic size-based rotation. Sensitive values (tokens, secrets) are never logged.

Index

Constants

View Source
const AuthDebugEnv = "DWS_DEBUG_AUTH"

AuthDebugEnv enables detailed authentication diagnostics when set to "1".

Variables

This section is empty.

Functions

func AuthDebug added in v1.0.54

func AuthDebug(message string, args ...any)

AuthDebug writes detailed authentication diagnostics only when explicitly enabled.

func IsSensitiveKey

func IsSensitiveKey(key string) bool

IsSensitiveKey returns true if the key (case-insensitive) refers to a credential or secret that must not appear in log files.

func LogCommandEnd added in v1.0.5

func LogCommandEnd(logger *slog.Logger, executionId, product, tool string, success bool, duration time.Duration, errCategory, errReason string)

LogCommandEnd logs the end of a command execution.

func LogCommandStart added in v1.0.5

func LogCommandStart(logger *slog.Logger, executionId, product, tool, endpoint, version string, authPresent bool, timeoutSec int)

LogCommandStart logs the beginning of a command execution.

func LogErrorClassified added in v1.0.5

func LogErrorClassified(logger *slog.Logger, method, executionId, category, reason string, httpStatus, rpcCode int, retryable bool, traceID string)

LogErrorClassified logs the final error classification at Warn level.

func LogRequest

func LogRequest(logger *slog.Logger, method, endpoint, executionId string, bodySize int)

LogRequest logs a JSON-RPC request at Debug level.

func LogRequestBody added in v1.0.5

func LogRequestBody(logger *slog.Logger, method, executionId string, toolName string, arguments map[string]any)

LogRequestBody logs a truncated, redacted request body for tools/call.

func LogResponse

func LogResponse(logger *slog.Logger, method, endpoint, executionId string, statusCode int, respSize int, duration time.Duration, err error)

LogResponse logs a JSON-RPC response at Debug level (Warn on error).

func LogResponseBody added in v1.0.5

func LogResponseBody(logger *slog.Logger, method, executionId string, statusCode int, body []byte, traceID string)

LogResponseBody logs a truncated response body on error paths.

func LogRetryAttempt added in v1.0.5

func LogRetryAttempt(logger *slog.Logger, method, executionId string, attempt, maxRetries int, statusCode int, delay time.Duration, lastErr error)

LogRetryAttempt logs a retry attempt at Warn level.

func NewRotatingFile added in v1.0.42

func NewRotatingFile(path string) (io.WriteCloser, error)

NewRotatingFile opens path as a size-rotating log file (same rotation policy as the diagnostic logger: maxLogSize per file, maxBackups kept) and returns an io.WriteCloser. The caller owns the returned writer and must Close it. Used by the connect daemon to archive a long-running connector's stdout/stderr without reimplementing rotation. Returns an error if the file cannot be opened.

func RedactHeaders added in v1.0.5

func RedactHeaders(headers http.Header) []slog.Attr

RedactHeaders returns slog attributes for HTTP headers with sensitive values redacted.

func RedactValue

func RedactValue(value string) string

RedactValue replaces a sensitive value with a safe placeholder. It preserves the first 4 characters for identification if the value is long enough, otherwise fully redacts.

func SanitizeArguments added in v1.0.5

func SanitizeArguments(args map[string]any, maxBytes int) string

SanitizeArguments returns a JSON string of the arguments map with sensitive-looking values replaced by "***". Truncates to maxBytes.

func TruncateBody added in v1.0.5

func TruncateBody(body []byte, maxBytes int) string

TruncateBody returns the body truncated to maxBytes with a UTF-8 safe boundary. If truncated, appends a marker showing the original size.

Types

type FileLogger

type FileLogger struct {
	Logger *slog.Logger
	// contains filtered or unexported fields
}

FileLogger wraps a rotatingWriter and provides a structured slog.Logger that writes JSON lines to disk.

func Setup

func Setup(configDir string) *FileLogger

Setup creates the log directory and returns a FileLogger. If directory creation fails, it returns a no-op logger that discards all output.

func (*FileLogger) Close

func (fl *FileLogger) Close() error

Close flushes and permanently closes the underlying log file. Later writes fail with os.ErrClosed instead of reopening the file.

func (*FileLogger) Writer

func (fl *FileLogger) Writer() io.Writer

Writer returns the underlying io.Writer for use with slog handlers. Returns io.Discard if the logger is not initialized.

type MultiHandler

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

MultiHandler fans out slog records to multiple handlers. Each handler applies its own level filter independently.

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

NewMultiHandler creates a handler that writes to all provided handlers.

func (*MultiHandler) Enabled

func (h *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*MultiHandler) Handle

func (h *MultiHandler) Handle(ctx context.Context, record slog.Record) error

func (*MultiHandler) WithAttrs

func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*MultiHandler) WithGroup

func (h *MultiHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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