logging

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package logging provides structured logging utilities for reputer.

Three logging modes: CLI (user-friendly), text (debug), JSON (structured). All modes write to stderr and use log/slog under the hood.

Index

Constants

View Source
const (
	// EnvVarLogLevel is the environment variable name for setting the log level.
	EnvVarLogLevel = "LOG_LEVEL"
)
View Source
const LogPrefixEnvVar = "REPUTER_LOG_PREFIX"

LogPrefixEnvVar is the environment variable name for customizing the log prefix.

Variables

This section is empty.

Functions

func NewCLILogger

func NewCLILogger(level string) *slog.Logger

NewCLILogger creates a new logger with CLI-friendly output format. This logger outputs minimal, user-friendly messages: - Normal messages: just the message text - Error messages: message text in red color Parameters:

  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

Returns:

  • *slog.Logger: A pointer to the configured slog.Logger instance with CLI handler.

func NewLogLogger

func NewLogLogger(module, version, level string) *log.Logger

NewLogLogger creates a new standard library log.Logger that writes logs using the slog package with the specified log level. Parameters:

  • level: The log level as a slog.Level.

Returns:

  • *log.Logger: A pointer to the configured log.Logger instance.

func NewStructuredLogger

func NewStructuredLogger(module, version, level string) *slog.Logger

NewStructuredLogger creates a new structured logger with the specified log level Defined module name and version are included in the logger's context. AddSource is enabled for debug level logging only. Parameters:

  • module: The name of the module/application using the logger.
  • version: The version of the module/application (e.g., "v1.0.0").
  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

Returns:

  • *slog.Logger: A pointer to the configured slog.Logger instance.

func NewTextLogger

func NewTextLogger(module, version, level string) *slog.Logger

NewTextLogger creates a new text logger with the specified log level Defined module name and version are included in the logger's context. AddSource is enabled for debug level logging only. Parameters:

  • module: The name of the module/application using the logger.
  • version: The version of the module/application (e.g., "v1.0.0").
  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

Returns:

  • *slog.Logger: A pointer to the configured slog.Logger instance with text handler.

func ParseLogLevel

func ParseLogLevel(level string) slog.Level

ParseLogLevel converts a string representation of a log level into a slog.Level. Parameters:

  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

Returns:

  • slog.Level corresponding to the input string. Defaults to slog.LevelInfo for unrecognized strings.

func SetDefaultCLILogger

func SetDefaultCLILogger(level string)

SetDefaultCLILogger initializes the CLI logger with the appropriate log level and sets it as the default logger. Parameters:

  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

func SetDefaultLogger

func SetDefaultLogger(module, version string)

SetDefaultLogger initializes the text logger with the appropriate log level and sets it as the default logger. Defined module name and version are included in the logger's context. Parameters:

  • module: The name of the module/application using the logger.
  • version: The version of the module/application (e.g., "v1.0.0").

Derives log level from the LOG_LEVEL environment variable.

func SetDefaultLoggerWithLevel

func SetDefaultLoggerWithLevel(module, version, level string)

SetDefaultLoggerWithLevel initializes the text logger with the specified log level and sets it as the default logger. Defined module name and version are included in the logger's context. Parameters:

  • module: The name of the module/application using the logger.
  • version: The version of the module/application (e.g., "v1.0.0").
  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

func SetDefaultStructuredLogger

func SetDefaultStructuredLogger(module, version string)

SetDefaultStructuredLogger initializes the structured logger with the appropriate log level and sets it as the default logger. Defined module name and version are included in the logger's context. Parameters:

  • module: The name of the module/application using the logger.
  • version: The version of the module/application (e.g., "v1.0.0").

Derives log level from the LOG_LEVEL environment variable.

func SetDefaultStructuredLoggerWithLevel

func SetDefaultStructuredLoggerWithLevel(module, version, level string)

SetDefaultStructuredLoggerWithLevel initializes the structured logger with the specified log level Defined module name and version are included in the logger's context. Parameters:

  • module: The name of the module/application using the logger.
  • version: The version of the module/application (e.g., "v1.0.0").
  • level: The log level as a string (e.g., "debug", "info", "warn", "error").

Types

type CLIHandler

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

CLIHandler is a custom slog.Handler for CLI output. It formats log messages in a user-friendly way: - Non-error messages: just the message text - Error messages: message text in red

func NewCLIHandler

func NewCLIHandler(w io.Writer, level slog.Level) *CLIHandler

NewCLIHandler creates a new CLI handler that writes to the given writer.

func (*CLIHandler) Enabled

func (h *CLIHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled returns true if the handler handles records at the given level.

func (*CLIHandler) Handle

func (h *CLIHandler) Handle(_ context.Context, r slog.Record) error

Handle formats and writes the log record with attributes.

func (*CLIHandler) WithAttrs

func (h *CLIHandler) WithAttrs(_ []slog.Attr) slog.Handler

WithAttrs returns a new handler with the given attributes. For CLI handler, we ignore attributes for simplicity.

func (*CLIHandler) WithGroup

func (h *CLIHandler) WithGroup(_ string) slog.Handler

WithGroup returns a new handler with the given group. For CLI handler, we ignore groups for simplicity.

Jump to

Keyboard shortcuts

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