logging

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ANSICodeCyan         = ANSICode("\033[36m")
	ANSICodeLightGreen   = ANSICode("\033[92m")
	ANSICodeOrange       = ANSICode("\033[33m")
	ANSICodeRed          = ANSICode("\033[31m")
	ANSICodeLightBlue    = ANSICode("\033[94m")
	ANSICodeGray         = ANSICode("\033[90m")
	ANSICodeBrightYellow = ANSICode("\033[93m")
	ANSICodeMagenta      = ANSICode("\033[35m")
	ANSICodeReset        = ANSICode("\033[0m")
)

ANSICodes for colored and formatted output.

Functions

func AnyToJSONBytes

func AnyToJSONBytes(data any) ([]byte, error)

AnyToJSONString converts data to a JSON byte array. It supports converting a large variety of data types.

Parameters:

  • data: The data to convert to JSON.

Returns:

  • string: A JSON string representation of the data.
  • error: An error if the data cannot be converted.

func AnyToJSONString

func AnyToJSONString(data ...any) string

AnyToJSONString converts data to a JSON string. It supports converting a large variety of data types. It will panic if the data cannot be converted.

Parameters:

  • data: The data to convert to JSON.

Returns:

  • string: A JSON string representation of the data.

func Println

func Println(messages ...any)

Println formats and prints a debug colored message into stdout.

Parameters:

  • messages The messages to print.

func PrintlnBoard

func PrintlnBoard(messages ...any)

PrintlnBoard formats and prints a debug colored message into stdout with visible signage.

Parameters:

  • messages The messages to print.

func PrintlnJSON

func PrintlnJSON(messages ...any)

PrintlnJSON formats and prints a debug colored JSON message into stdout.

Parameters:

  • messages The messages to print.

func SetDefaultLogOpts

func SetDefaultLogOpts(opts LogOpts)

SetDefaultLogOpts overrides the the default logging configuration.

Types

type ANSICode

type ANSICode string

ANSICode represents a color and formatting code.

type CtxLogger

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

CtxLogger is a logger that takes a context.

func NewCtxLogger

func NewCtxLogger(ctx context.Context, opts *LogOpts) *CtxLogger

NewCtxLogger constructs a logger, using the package-level default options if none are passed in.

Parameters:

  • ctx The context to use.
  • opts The optional options to use. If nil, the default options are used.

Returns:

  • *ContextLogger: The logger.

func (*CtxLogger) Debug

func (cl *CtxLogger) Debug(messages ...any)

Debug prints a debug message. It will always print.

Parameters:

  • messages The messages to print.

func (*CtxLogger) Debugf

func (cl *CtxLogger) Debugf(format string, params ...any)

Debugf formats and prints a debug message. It will always print.

Parameters:

  • format The format string.
  • params The parameters to format.

func (*CtxLogger) Error

func (cl *CtxLogger) Error(messages ...any)

Error prints an error message if the logging level is high enough.

Parameters:

  • messages The messages to print.

func (*CtxLogger) Errorf

func (cl *CtxLogger) Errorf(format string, params ...any)

Errorf formats and prints an error message if the logging level is high enough.

Parameters:

  • format The format string.
  • params The parameters to format.

func (*CtxLogger) Fatal

func (cl *CtxLogger) Fatal(messages ...any)

Fatal prints a fatal message if the logging level is high enough.

Parameters:

  • messages The messages to print.

func (*CtxLogger) Fatalf

func (cl *CtxLogger) Fatalf(format string, params ...any)

Fatalf formats and prints a fatal message if the logging level is high enough.

Parameters:

  • format The format string.
  • params The parameters to format.

func (*CtxLogger) Info

func (cl *CtxLogger) Info(messages ...any)

Info prints an info message if the logging level is high enough.

Parameters:

  • messages The messages to print.

func (*CtxLogger) Infof

func (cl *CtxLogger) Infof(format string, params ...any)

Infof formats and prints an info message if the logging level is high enough.

Parameters:

  • format The format string.
  • params The parameters to format.

func (*CtxLogger) Log

func (cl *CtxLogger) Log(
	ansicode ANSICode, severity LogSeverity, messages ...any,
)

Log prints a message with custom ANSI code and severity. It will always print.

Parameters:

  • ansicode The ANSI code to use.
  • severity The severity of the message.
  • messages The messages to print.

func (*CtxLogger) Logf

func (cl *CtxLogger) Logf(
	ansicode ANSICode,
	severity LogSeverity,
	format string,
	params ...any,
)

Logf formats and prints a message. It will always print.

func (*CtxLogger) Trace

func (cl *CtxLogger) Trace(messages ...any)

Trace prints a trace message if the logging level is high enough.

Parameters:

  • messages The messages to print.

func (*CtxLogger) Tracef

func (cl *CtxLogger) Tracef(format string, params ...any)

Tracef formats and prints a trace message if the logging level is high enough.

Parameters:

  • format The format string.
  • params The parameters to format.

func (*CtxLogger) Warn

func (cl *CtxLogger) Warn(messages ...any)

Warn prints a warning message if the logging level is high enough.

Parameters:

  • messages The messages to print.

func (*CtxLogger) Warnf

func (cl *CtxLogger) Warnf(format string, params ...any)

Warnf formats and prints a warn message if the logging level is high enough.

Parameters:

  • format The format string.
  • params The parameters to format.

type CtxLoggerFactoryFn

type CtxLoggerFactoryFn func(ctx context.Context) ILogger

CtxLoggerFactoryFn is a function that returns a logger with context.

type DebugLogger

type DebugLogger interface {
	Debug(messages ...any)
	Debugf(message string, params ...any)
	Trace(messages ...any)
	Tracef(message string, params ...any)
}

DebugLogger defines debug and trace level logging.

type ErrorLogger

type ErrorLogger interface {
	Error(messages ...any)
	Errorf(message string, params ...any)
	Fatal(messages ...any)
	Fatalf(message string, params ...any)
}

ErrorLogger defines error and fatal level logging.

type ExtraData

type ExtraData struct {
	Time      *time.Time `json:"time,omitempty"`
	TimeStart *time.Time `json:"time_start,omitempty"`
	TimeDelta string     `json:"time_delta,omitempty"`
	TraceID   string     `json:"trace_id,omitempty"`
	SpanID    string     `json:"span_id,omitempty"`
}

ExtraData contains request metadata.

type GetExtraDataFunc

type GetExtraDataFunc func(ctx context.Context) *ExtraData

GetExtraDataFunc is a function that returns extra data for logging.

type ILogger

type ILogger interface {
	DebugLogger
	InfoLogger
	ErrorLogger
}

ILogger represents a logger with different logging levels.

type InfoLogger

type InfoLogger interface {
	Info(messages ...any)
	Infof(message string, params ...any)
	Warn(messages ...any)
	Warnf(message string, params ...any)
}

InfoLogger defines info and warning level logging.

type LogLevel

type LogLevel int

LogLevel represents the level of a log message.

func LoggingLevelStrToInt

func LoggingLevelStrToInt(level string) (LogLevel, error)

LoggingLevelStrToInt converts a string to a logging level integer. The string is trimmed and converted to uppercase. If the string is empty, the default logging level is returned. If the string is a valid integer, it is returned as the logging level. If the string is one of the predefined strings, the corresponding logging level is returned.

Parameters:

  • level The string to convert.

Returns:

  • int: The logging level integer.
  • error: An error if the conversion fails.

type LogLevelCfg

type LogLevelCfg struct {
	Level    LogLevel
	Severity LogSeverity
	Color    ANSICode
	Writer   io.Writer
	Callback func(data []byte)
}

LogLevelCfg holds logging level configuration.

type LogLevelOpts

type LogLevelOpts struct {
	Debug *LogLevelCfg
	Trace *LogLevelCfg
	Info  *LogLevelCfg
	Warn  *LogLevelCfg
	Error *LogLevelCfg
	Fatal *LogLevelCfg
}

LogLevelOpts holds logging level options.

func DefaultLogLevelOpts

func DefaultLogLevelOpts() *LogLevelOpts

DefaultLogLevelOpts returns the package-wide fallback log level settings.

type LogMessage

type LogMessage struct {
	Severity LogSeverity `json:"severity"`
	Message  any         `json:"message"`
	Data     any         `json:"data,omitempty"`
	Extra    any         `json:"extra,omitempty"`
}

LogMessage represents a log message.

func NewLogMessage

func NewLogMessage(
	severity LogSeverity, message any, data any, extra any,
) LogMessage

NewLogMessage creates a new LogMessage.

Parameters:

  • severity: The severity of the message.
  • message: The message to log.
  • data: The data to log.
  • extra: The extra data to log.

Returns:

  • LogMessage: The new LogMessage.

type LogOpts

type LogOpts struct {
	LoggingLevel LogLevel
	Compact      bool
	AnsiCodes    bool
	GetExtraData GetExtraDataFunc
	LogLevelOpts *LogLevelOpts
}

LogOpts holds shared logger configuration.

func DefaultLogOpts

func DefaultLogOpts() *LogOpts

DefaultLogOpts returns the package-wide fallback settings.

type LogSeverity

type LogSeverity string

LogSeverity represents the severity of a log message.

type LoggerFactoryFn

type LoggerFactoryFn func(params ...any) ILogger

LoggerFactoryFn is a function that returns a logger.

Jump to

Keyboard shortcuts

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