Documentation
¶
Overview ¶
Package logging provides logging utilities for the application.
Index ¶
- Variables
- func ForTestsOnlyResetLogger()
- func GetLogger() *slog.Logger
- func HydrateFromFile(path string) error
- func Init(level slog.Level, output io.Writer, logFilePath string, format ...string)
- func SetLevel(level slog.Level)
- func ToSlogLevel(level configv1.GlobalSettings_LogLevel) slog.Level
- type AuditHandler
- func (h *AuditHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *AuditHandler) Export(ctx context.Context, r slog.Record) error
- func (h *AuditHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *AuditHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *AuditHandler) WithGroup(name string) slog.Handler
- type BroadcastHandler
- type Broadcaster
- func (b *Broadcaster) Broadcast(msg any)
- func (b *Broadcaster) ClearHistory()
- func (b *Broadcaster) GetHistory() []any
- func (b *Broadcaster) Hydrate(messages []any)
- func (b *Broadcaster) Reset()
- func (b *Broadcaster) Subscribe() chan any
- func (b *Broadcaster) SubscribeBuffered(size int) chan any
- func (b *Broadcaster) SubscribeWithHistory() (chan any, []any)
- func (b *Broadcaster) SubscribeWithHistoryBuffered(size int) (chan any, []any)
- func (b *Broadcaster) Unsubscribe(ch chan any)
- type LogEntry
- type RedactingWriter
- type TeeHandler
Constants ¶
This section is empty.
Variables ¶
var ( // GlobalBroadcaster is the shared broadcaster instance for logs. // Summary: Defines GlobalBroadcaster. GlobalBroadcaster = NewBroadcaster() )
Functions ¶
func ForTestsOnlyResetLogger ¶
func ForTestsOnlyResetLogger()
ForTestsOnlyResetLogger is for use in tests to reset the `sync.Once` mechanism. This allows the global logger to be re-initialized in different test cases. This function should not be used in production code.
Parameters:
- None
Returns:
- None
Errors:
- None
Side Effects:
- None
Summary: Executes ForTestsOnlyResetLogger operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func GetLogger ¶
GetLogger returns the shared global logger instance.
If the logger has not yet been initialized through a call to `Init`, this function will initialize it with default settings: logging to `os.Stderr` at `slog.LevelInfo`.
Returns:
- *slog.Logger: The global `*slog.Logger` instance.
Side Effects:
- May initialize the default logger if not already set.
Summary: Retrieves GetLogger operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func HydrateFromFile ¶
HydrateFromFile reads the last N lines from the given log file, parses them (assuming JSON format), and populates the global broadcaster's history. This allows the UI to show historical logs upon connection, even after a server restart.
Parameters:
- path (string): The path parameter.
Returns:
- error: An error if the operation fails.
Errors:
- Returns an error if the operation fails or is invalid.
Side Effects:
- None
Summary: Executes HydrateFromFile operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func Init ¶
Init initializes the application's global logger with a specific log level and output destination.
This function is designed to be called only once, typically at the start of the application, to ensure a consistent logging setup.
Parameters:
- level (slog.Level): The minimum log level to be recorded.
- output (io.Writer): The output destination for logs.
- logFilePath (string): Optional path to a log file for JSON output.
- format (...string): Optional format string ("json" or "text"). Defaults to "text".
Side Effects:
- Sets the global logger instance.
- May open a file for writing.
Summary: Executes Init operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func SetLevel ¶
SetLevel updates the global log level dynamically.
Parameters:
- level (slog.Level): The new log level.
Side Effects:
- Updates the global log level atomic variable.
Summary: Updates SetLevel operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func ToSlogLevel ¶
func ToSlogLevel(level configv1.GlobalSettings_LogLevel) slog.Level
ToSlogLevel converts a string log level to a slog.Level.
Parameters:
- level (configv1.GlobalSettings_LogLevel): The log level from the configuration.
Returns:
- slog.Level: The corresponding slog.Level.
Side Effects:
- None.
Summary: Executes ToSlogLevel operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
Types ¶
type AuditHandler ¶
type AuditHandler struct {
// contains filtered or unexported fields
}
AuditHandler is a slog.Handler that exports logs to audit sinks.
Summary: Represents a AuditHandler.
func NewAuditHandler ¶
func NewAuditHandler(next slog.Handler, config *configv1.AuditConfig) *AuditHandler
NewAuditHandler creates a new AuditHandler.
Parameters:
- next (slog.Handler): The next parameter.
- config (*configv1.AuditConfig): The config parameter.
Returns:
- *AuditHandler: The resulting *AuditHandler.
Errors:
- None
Side Effects:
- None
Summary: Initializes NewAuditHandler operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*AuditHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level.
Parameters:
- ctx (context.Context): The context for the request.
- level (slog.Level): The level parameter.
Returns:
- bool: True if successful, false otherwise.
Errors:
- None
Side Effects:
- None
Summary: Executes Enabled operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*AuditHandler) Export ¶
Export sends the log record to the configued sinks.
Parameters:
- ctx (context.Context): The context for the request.
- r (slog.Record): The r parameter.
Returns:
- error: An error if the operation fails.
Errors:
- Returns an error if the operation fails or is invalid.
Side Effects:
- None
Summary: Executes Export operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*AuditHandler) Handle ¶
Handle handles the Record.
Parameters:
- ctx (context.Context): The context for the request.
- r (slog.Record): The r parameter.
Returns:
- error: An error if the operation fails.
Errors:
- Returns an error if the operation fails or is invalid.
Side Effects:
- None
Summary: Executes Handle operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*AuditHandler) WithAttrs ¶
func (h *AuditHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new generic Handler with the given attributes.
Parameters:
- attrs ([]slog.Attr): The attrs parameter.
Returns:
- slog.Handler: The resulting slog.Handler.
Errors:
- None
Side Effects:
- None
Summary: Executes WithAttrs operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*AuditHandler) WithGroup ¶
func (h *AuditHandler) WithGroup(name string) slog.Handler
WithGroup returns a new generic Handler with the given group.
Parameters:
- name (string): The name parameter.
Returns:
- slog.Handler: The resulting slog.Handler.
Errors:
- None
Side Effects:
- None
Summary: Executes WithGroup operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
type BroadcastHandler ¶
type BroadcastHandler struct {
// contains filtered or unexported fields
}
BroadcastHandler implements slog.Handler and sends logs to the Broadcaster.
Summary: Represents a BroadcastHandler.
func NewBroadcastHandler ¶
func NewBroadcastHandler(broadcaster *Broadcaster, level slog.Leveler) *BroadcastHandler
NewBroadcastHandler creates a new BroadcastHandler. broadcaster is the broadcaster. level is the minimum log level to broadcast. Returns the result.
Parameters:
- broadcaster (*Broadcaster): The broadcaster parameter.
- level (slog.Leveler): The level parameter.
Returns:
- *BroadcastHandler: The resulting *BroadcastHandler.
Errors:
- None
Side Effects:
- None
Summary: Initializes NewBroadcastHandler operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*BroadcastHandler) Enabled ¶
Enabled returns true if the level is greater than or equal to the handler's level. _ is an unused parameter. level is the log level. Returns true if successful.
Parameters:
- _ (context.Context): The _ parameter.
- level (slog.Level): The level parameter.
Returns:
- bool: True if successful, false otherwise.
Errors:
- None
Side Effects:
- None
Summary: Executes Enabled operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*BroadcastHandler) Handle ¶
Handle handles the log record by converting it to LogEntry and broadcasting it. _ is an unused parameter. r is the r. Returns an error if the operation fails.
Parameters:
- _ (context.Context): The _ parameter.
- r (slog.Record): The r parameter.
Returns:
- error: An error if the operation fails.
Errors:
- Returns an error if the operation fails or is invalid.
Side Effects:
- None
Summary: Executes Handle operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*BroadcastHandler) WithAttrs ¶
func (h *BroadcastHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new handler with the given attributes. attrs is the attrs. Returns the result.
Parameters:
- attrs ([]slog.Attr): The attrs parameter.
Returns:
- slog.Handler: The resulting slog.Handler.
Errors:
- None
Side Effects:
- None
Summary: Executes WithAttrs operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*BroadcastHandler) WithGroup ¶
func (h *BroadcastHandler) WithGroup(name string) slog.Handler
WithGroup returns a new handler with the given group. name is the name of the resource. Returns the result.
Parameters:
- name (string): The name parameter.
Returns:
- slog.Handler: The resulting slog.Handler.
Errors:
- None
Side Effects:
- None
Summary: Executes WithGroup operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
type Broadcaster ¶
type Broadcaster struct {
// contains filtered or unexported fields
}
Broadcaster manages a set of subscribers and broadcasts messages to them.
Summary: Represents a Broadcaster.
func NewBroadcaster ¶
func NewBroadcaster() *Broadcaster
NewBroadcaster creates a new Broadcaster. Returns the result.
Parameters:
- None
Returns:
- *Broadcaster: The resulting *Broadcaster.
Errors:
- None
Side Effects:
- None
Summary: Initializes NewBroadcaster operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) Broadcast ¶
func (b *Broadcaster) Broadcast(msg any)
Broadcast sends a message to all subscribers. This method is non-blocking; if a subscriber's channel is full, the message is dropped for that subscriber.
Parameters:
- msg (any): The msg parameter.
Returns:
- None
Errors:
- None
Side Effects:
- None
Summary: Executes Broadcast operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) ClearHistory ¶
func (b *Broadcaster) ClearHistory()
ClearHistory clears the history of the broadcaster without removing subscribers.
Summary: Executes ClearHistory operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) GetHistory ¶
func (b *Broadcaster) GetHistory() []any
GetHistory returns the current log history.
Parameters:
- None
Returns:
- []any: The resulting []any.
Errors:
- None
Side Effects:
- None
Summary: Retrieves GetHistory operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) Hydrate ¶
func (b *Broadcaster) Hydrate(messages []any)
Hydrate populates the history buffer with messages. It is intended to be called at startup. Messages are NOT broadcasted to subscribers, as subscribers shouldn't exist yet, or shouldn't receive old history as "new" events.
Parameters:
- messages ([]any): The messages parameter.
Returns:
- None
Errors:
- None
Side Effects:
- None
Summary: Executes Hydrate operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) Reset ¶
func (b *Broadcaster) Reset()
Reset clears the broadcaster history and subscribers. This is primarily for testing to ensure a clean state.
Parameters:
- None
Returns:
- None
Errors:
- None
Side Effects:
- None
Summary: Executes Reset operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) Subscribe ¶
func (b *Broadcaster) Subscribe() chan any
Subscribe returns a channel that will receive broadcast messages. The channel has a small buffer to prevent slow consumers from blocking the broadcaster. It is the caller's responsibility to read from the channel promptly.
Parameters:
- None
Returns:
- chanany: The resulting chanany.
Errors:
- None
Side Effects:
- None
Summary: Executes Subscribe operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) SubscribeBuffered ¶
func (b *Broadcaster) SubscribeBuffered(size int) chan any
SubscribeBuffered returns a channel that will receive broadcast messages with a custom buffer size. The channel has a buffer to prevent slow consumers from blocking the broadcaster. It is the caller's responsibility to read from the channel promptly.
Parameters:
- size (int): The size parameter.
Returns:
- chanany: The resulting chanany.
Errors:
- None
Side Effects:
- None
Summary: Executes SubscribeBuffered operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) SubscribeWithHistory ¶
func (b *Broadcaster) SubscribeWithHistory() (chan any, []any)
SubscribeWithHistory returns a channel that will receive broadcast messages, and the current history of messages. This is atomic to ensure no messages are missed or duplicated.
Parameters:
- None
Returns:
- any: The resulting any.
- []any: The resulting []any.
Errors:
- None
Side Effects:
- None
Summary: Executes SubscribeWithHistory operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) SubscribeWithHistoryBuffered ¶
func (b *Broadcaster) SubscribeWithHistoryBuffered(size int) (chan any, []any)
SubscribeWithHistoryBuffered returns a channel that will receive broadcast messages with a custom buffer size, and the current history of messages. This is atomic to ensure no messages are missed or duplicated.
Parameters:
- size (int): The size parameter.
Returns:
- any: The resulting any.
- []any: The resulting []any.
Errors:
- None
Side Effects:
- None
Summary: Executes SubscribeWithHistoryBuffered operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*Broadcaster) Unsubscribe ¶
func (b *Broadcaster) Unsubscribe(ch chan any)
Unsubscribe removes a subscriber channel. ch is the ch.
Parameters:
- ch (chanany): The ch parameter.
Returns:
- None
Errors:
- None
Side Effects:
- None
Summary: Executes Unsubscribe operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
type LogEntry ¶
type LogEntry struct {
ID string `json:"id"`
Timestamp string `json:"timestamp"`
Level string `json:"level"`
Message string `json:"message"`
Source string `json:"source,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
LogEntry is the structure for logs sent over WebSocket. It matches the frontend expectation.
Summary: Represents a LogEntry.
type RedactingWriter ¶
type RedactingWriter struct {
// contains filtered or unexported fields
}
RedactingWriter is an io.Writer that redacts sensitive information from JSON logs.
Summary: Represents a RedactingWriter.
func (*RedactingWriter) Write ¶
func (w *RedactingWriter) Write(p []byte) (n int, err error)
Write implements io.Writer.
Parameters:
- p ([]byte): The p parameter.
Returns:
- int: The resulting int.
- error: An error if the operation fails.
Errors:
- Returns an error if the operation fails or is invalid.
Side Effects:
- None
Summary: Updates Write operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
type TeeHandler ¶
type TeeHandler struct {
// contains filtered or unexported fields
}
TeeHandler is a slog.Handler that writes to multiple handlers.
Summary: Represents a TeeHandler.
func NewTeeHandler ¶
func NewTeeHandler(handlers ...slog.Handler) *TeeHandler
NewTeeHandler creates a new TeeHandler. handlers is the handlers. Returns the result.
Parameters:
- handlers (...slog.Handler): The handlers parameter.
Returns:
- *TeeHandler: The resulting *TeeHandler.
Errors:
- None
Side Effects:
- None
Summary: Initializes NewTeeHandler operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*TeeHandler) Enabled ¶
Enabled returns true if any of the handlers are enabled. ctx is the context for the request. level is the level. Returns true if successful.
Parameters:
- ctx (context.Context): The context for the request.
- level (slog.Level): The level parameter.
Returns:
- bool: True if successful, false otherwise.
Errors:
- None
Side Effects:
- None
Summary: Executes Enabled operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*TeeHandler) Handle ¶
Handle forwards the record to all enabled handlers. ctx is the context for the request. r is the r. Returns an error if the operation fails.
Parameters:
- ctx (context.Context): The context for the request.
- r (slog.Record): The r parameter.
Returns:
- error: An error if the operation fails.
Errors:
- Returns an error if the operation fails or is invalid.
Side Effects:
- None
Summary: Executes Handle operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*TeeHandler) WithAttrs ¶
func (h *TeeHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new TeeHandler with the attributes applied to all handlers. attrs is the attrs. Returns the result.
Parameters:
- attrs ([]slog.Attr): The attrs parameter.
Returns:
- slog.Handler: The resulting slog.Handler.
Errors:
- None
Side Effects:
- None
Summary: Executes WithAttrs operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.
func (*TeeHandler) WithGroup ¶
func (h *TeeHandler) WithGroup(name string) slog.Handler
WithGroup returns a new TeeHandler with the group applied to all handlers. name is the name of the resource. Returns the result.
Parameters:
- name (string): The name parameter.
Returns:
- slog.Handler: The resulting slog.Handler.
Errors:
- None
Side Effects:
- None
Summary: Executes WithGroup operation.
Parameters:
- TODO: Document parameters.
Returns:
- TODO: Document returns.
Errors:
- TODO: Document errors.
Side Effects:
- None.