logger

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 13 Imported by: 90

README

logger

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is

func Is(e error, args ...string) bool

Is checks if the provided error message contains any of the specified substrings and returns true if a match is found.

Types

type Config

type Config struct {
	Format    string           // json, text, csv
	Level     int              // 100: DEBUG, 200: INFO, 300: NOTICE, 400: WARNING, 500: ERROR, 502: CRITICAL, 509: ALERT
	Output    string           // stdout, <filepath>
	Retries   int              // number of retries
	Wait      time.Duration    // wait time between retries
	StatusMap map[int][]string // status code to message map
}

Config defines the configuration options for logging, including format, level, output, retries, and wait duration.

type Error

type Error struct {
	UUID    string `json:"uuid"`
	Status  int    `json:"status"`
	Message string `json:"message"`
}

Error represents an error with a unique identifier, status code, and a message.

func ToError added in v1.2.0

func ToError(err error) *Error

ToError tries to cast an error to a SdkError. If the error is not an SdkError, it returns nil.

func (*Error) Error added in v1.1.13

func (e *Error) Error() string

Error serializes the Error struct into a JSON-formatted string and returns it as an error message.

func (*Error) GinError added in v1.1.4

func (e *Error) GinError(c *gin.Context)

GinError sets error details in the response headers and terminates the request with the associated status code.

func (*Error) IsAny added in v1.1.13

func (e *Error) IsAny(args ...string) bool

IsAny checks if the error message contains any of the provided substrings and returns true if a match is found.

type Log

type Log struct {
	Timestamp string `json:"timestamp"`
	Severity  string `json:"severity"`
	Path      string `json:"path"`
	Line      int    `json:"line"`
	Error
}

Log represents a structured log entry containing timestamp, severity, file path, line number, and error details.

func (*Log) ToCsv added in v1.0.6

func (l *Log) ToCsv() string

ToCsv converts a Log instance into a CSV-formatted single-line string and returns it. Returns an empty string on failure.

func (*Log) ToJSON

func (l *Log) ToJSON() string

ToJSON serializes the Log struct into a JSON-formatted string and returns it.

func (*Log) ToString

func (l *Log) ToString() string

ToString converts the Log struct into a single-line string with fields concatenated and separated by spaces.

type Logger

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

Logger provides logging functionalities with configurable format, level, output, retries, and wait time.

func NewLogger added in v1.0.3

func NewLogger(config *Config) *Logger

NewLogger initializes and returns a new Logger instance configured with the provided Config or default values.

func (*Logger) ErrorF

func (l *Logger) ErrorF(format string, args ...any) *Error

ErrorF logs an error message with a formatted string and arguments and determines the corresponding status code. Returns an error instance containing a UUID, status code, and message.

func (*Logger) Fatal added in v1.0.6

func (l *Logger) Fatal(format string, args ...any)

Fatal logs a message with a status code of 500 and terminates the application with os.Exit(1).

func (*Logger) InfiniteLoop added in v1.1.8

func (l *Logger) InfiniteLoop(f func() error, exception ...string)

InfiniteLoop continuously executes a provided function until it produces a matching error or a specified exception.

func (*Logger) InfiniteRetry added in v1.1.7

func (l *Logger) InfiniteRetry(f func() error, exception ...string) error

InfiniteRetry executes a function repeatedly until it succeeds or returns an error containing specified substrings. If a matching error occurs, it is returned immediately. Otherwise, it waits for a configured duration before retrying.

func (*Logger) InfiniteRetryIfXError added in v1.2.1

func (l *Logger) InfiniteRetryIfXError(f func() error, exception string) error

InfiniteRetryIfXError retries a function f() infinitely only if the error returned by f() matches "xError". If f() returns nil (no error), the function exits successfully. If f() returns an error that is different from "xError", it stops immediately and returns that error.

Additionally, to avoid log saturation: - It prints the "xError" message only ONCE, the first time it occurs. - If the issue is later resolved (f() stops returning an error), it logs that the problem was fixed.

func (*Logger) Info added in v1.0.4

func (l *Logger) Info(format string, args ...any) *Log

Info logs an informational message with a status code of 200, using formatted strings and optional arguments.

func (*Logger) LogF

func (l *Logger) LogF(statusCode int, format string, args ...any) *Log

LogF logs a message with a specific status code, format string, and arguments, returning a structured Log instance. Determines severity based on the status code and outputs in the configured format and destination.

func (*Logger) Retry added in v1.1.7

func (l *Logger) Retry(f func() error, exception ...string) error

Retry executes a function repeatedly until it succeeds or the maximum retries are reached, or a matching error is encountered.

Jump to

Keyboard shortcuts

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