go_concurrent_logger

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: GPL-3.0 Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilSendFunction          = errors.New("send function cannot be nil")
	ErrNilCloseFunction         = errors.New("close function cannot be nil")
	ErrLoggerClosed             = errors.New("logger is closed")
	ErrNilLogger                = errors.New("logger cannot be nil")
	ErrLoggerAlreadyRunning     = errors.New("logger is already running")
	ErrEmptyFilePath            = errors.New("file path cannot be empty")
	ErrInvalidChannelBufferSize = errors.New("channel buffer size must be greater than zero")
	ErrInvalidFileBufferSize    = errors.New("file buffer size must be greater than zero")
	ErrEmptyTag                 = errors.New("tag cannot be empty")
	ErrLoggerNotRunning         = errors.New("logger is not running")
)
View Source
var (
	// CategoryNames maps a given Category to its string name
	CategoryNames = map[Category]string{
		CategoryInfo:    "INFO",
		CategoryWarning: "WARNING",
		CategoryError:   "ERROR",
		CategoryDebug:   "DEBUG",
	}
)

Functions

func LogOnError

func LogOnError(fn func() error, loggerProducer LoggerProducer) error

LogOnError logs the error if it's not nil.

Parameters:

fn: The function to execute that returns an error. loggerProducer: The logger producer to use for logging the error.

func StopContextAndLogOnError added in v0.1.1

func StopContextAndLogOnError(
	ctx context.Context,
	stopFn func(),
	fn func(context.Context) error,
	loggerProducer LoggerProducer,
) func() error

StopContextAndLogOnError stops the context if an error is encountered and logs the error.

Parameters:

ctx: The context to be stopped. stopFn: A function that stops the context with a given error. fn: A function that returns an error. loggerProducer: The logger producer to log messages.

Returns:

A function that executes the provided function and stops the context if an error occurs.

Types

type Category

type Category uint8

Category is an enum to define the category of log message.

const (
	CategoryNil Category = iota
	CategoryInfo
	CategoryWarning
	CategoryError
	CategoryDebug
)

func (Category) String

func (c Category) String() string

String returns the string representation of the Category

Returns:

The string representation of the Category enum

type DefaultLogger

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

DefaultLogger is the default Logger implementation to handle writing log messages to a file.

func NewDefaultLogger

func NewDefaultLogger(
	filePath string,
	gracefulShutdownTimeout time.Duration,
	timestampFormat string,
	channelBufferSize int,
	fileBufferSize int,
	tag string,
	debug bool,
) (*DefaultLogger, error)

NewDefaultLogger creates a new DefaultLogger instance.

Parameters:

filePath: Path to the log file. gracefulShutdownTimeout: Duration to wait for graceful shutdown. timestampFormat: Format for timestamps in log messages. channelBufferSize: Size of the message channel buffer. fileBufferSize: Size of the file write buffer. tag: Default tag for log messages. debug: Flag to indicate if the writer is in debug mode.

Returns:

A pointer to a DefaultLogger instance.

func (*DefaultLogger) ChangeFilePath

func (l *DefaultLogger) ChangeFilePath(newPath string) error

ChangeFilePath changes the log file path to a new path.

Parameters:

newPath: The new file path for the log file.

func (*DefaultLogger) IsClosed

func (l *DefaultLogger) IsClosed() bool

IsClosed returns true if the logger channel has been closed.

Returns:

True if the logger channel is closed, otherwise false.

func (*DefaultLogger) IsDebug

func (l *DefaultLogger) IsDebug() bool

IsDebug returns true if the logger is in debug mode.

Returns:

True if the logger is in debug mode, otherwise false.

func (*DefaultLogger) IsRunning

func (l *DefaultLogger) IsRunning() bool

IsRunning returns true if the logger is currently running.

Returns:

True if the logger is running, otherwise false.

func (*DefaultLogger) NewProducer

func (l *DefaultLogger) NewProducer(
	tag string,
) (LoggerProducer, error)

NewProducer returns a new LoggerProducer instance associated with this DefaultLogger.

Parameters:

tag: Tag to identify the logger instance.

Returns:

A pointer to a LoggerProducer instance or an error if the parameters are invalid.

func (*DefaultLogger) Run

func (l *DefaultLogger) Run(ctx context.Context, stopFn func()) error

Run processes and writes all received messages to the log file until the channel is closed or the context is cancelled.

Parameters:

ctx: The context to control cancellation and timeouts. stopFn: Function to call when stopping the handler.

Returns:

An error if any issues occur during message processing or file writing.

func (*DefaultLogger) WaitUntilReady added in v0.1.2

func (l *DefaultLogger) WaitUntilReady(ctx context.Context) error

WaitUntilReady blocks until the logger is ready or the context is done.

Parameters:

ctx: The context to control cancellation and timeouts.

Returns:

An error if the context is done before the logger is ready.

type DefaultLoggerProducer

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

DefaultLoggerProducer is the default LoggerProducer implementation for messages with different severity levels

func NewDefaultLoggerProducer

func NewDefaultLoggerProducer(
	timestampFormat string,
	sendFn func(*Message),
	closeFn func(),
	tag string,
	debug bool,
) (*DefaultLoggerProducer, error)

NewDefaultLoggerProducer creates a new DefaultLoggerProducer instance.

Parameters:

timestampFormat: Format for the timestamp. sendFn: Function to send messages. closeFn: Function to call when done. tag: Tag to identify the logger instance. debug: Flag to indicate if the logger is in debug mode.

Returns:

A pointer to a DefaultLoggerProducer instance or an error if the parameters are invalid.

func (*DefaultLoggerProducer) Close

func (l *DefaultLoggerProducer) Close()

Close signals that the logger is done and performs cleanup.

func (*DefaultLoggerProducer) Debug

func (l *DefaultLoggerProducer) Debug(content string)

Debug logs a debug message if the logger is in debug mode.

Parameters:

content: The content of the debug message.

func (*DefaultLoggerProducer) Error

func (l *DefaultLoggerProducer) Error(err error)

Error logs an error message.

Parameters:

err: The error to log.

func (*DefaultLoggerProducer) Info

func (l *DefaultLoggerProducer) Info(content string)

Info logs an informational message.

Parameters:

content: The content of the informational message.

func (*DefaultLoggerProducer) IsClosed

func (l *DefaultLoggerProducer) IsClosed() bool

IsClosed returns true if the logger producer has been closed.

Returns:

True if the logger producer is closed, otherwise false.

func (*DefaultLoggerProducer) IsDebug

func (l *DefaultLoggerProducer) IsDebug() bool

IsDebug returns true if the logger producer is in debug mode.

Returns:

True if the logger producer is in debug mode, otherwise false.

func (*DefaultLoggerProducer) Log

func (l *DefaultLoggerProducer) Log(content string, category Category)

Log logs a message with the specified content and category.

Parameters:

content: The content of the log message. category: The category of the log message.

func (*DefaultLoggerProducer) Tag

func (l *DefaultLoggerProducer) Tag() string

Tag returns the tag associated with the logger producer.

Returns:

The tag string.

func (*DefaultLoggerProducer) Warning

func (l *DefaultLoggerProducer) Warning(content string)

Warning logs a warning message.

Parameters:

content: The content of the warning message.

type Logger

type Logger interface {
	NewProducer(
		tag string,
	) (LoggerProducer, error)
	ChangeFilePath(newPath string) error
	Run(ctx context.Context, stopFn func()) error
	IsRunning() bool
	IsClosed() bool
	IsDebug() bool
	WaitUntilReady(ctx context.Context) error
}

Logger is an interface for writing log messages to a file

type LoggerProducer

type LoggerProducer interface {
	Log(content string, category Category)
	Info(content string)
	Error(err error)
	Warning(content string)
	Debug(content string)
	Close()
	IsClosed() bool
	Tag() string
	IsDebug() bool
}

LoggerProducer is an interface for logging messages with different severity levels

type Message

type Message struct {
	Category Category
	Content  string
	Tag      string
	// contains filtered or unexported fields
}

Message is the struct to handle log messages

func NewMessage

func NewMessage(
	category Category,
	content string,
	tag string,
	timestampFormat string,
) *Message

NewMessage creates a new Message instance.

Parameters:

category: Category of the log message. content: Content of the log message. tag: Tag for the log message. timestampFormat: Format for the timestamp.

Returns:

A pointer to a Message instance.

func (*Message) String

func (m *Message) String() string

String returns the representation of the log message.

Returns:

The formatted log message

Jump to

Keyboard shortcuts

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