Documentation
¶
Index ¶
- Variables
- func CancelContextAndLogOnError(ctx context.Context, cancelFn context.CancelFunc, ...) func() error
- func LogOnError(fn func() error, loggerProducer LoggerProducer) error
- type Category
- type DefaultLogger
- func (l *DefaultLogger) ChangeFilePath(newPath string) error
- func (l *DefaultLogger) IsClosed() bool
- func (l *DefaultLogger) IsRunning() bool
- func (l *DefaultLogger) NewProducer(tag string, debug bool) (LoggerProducer, error)
- func (l *DefaultLogger) Run(ctx context.Context, stopFn func()) error
- func (l *DefaultLogger) WaitUntilReady(ctx context.Context) error
- type DefaultLoggerProducer
- func (l *DefaultLoggerProducer) Close()
- func (l *DefaultLoggerProducer) Debug(content string)
- func (l *DefaultLoggerProducer) Error(err error)
- func (l *DefaultLoggerProducer) Info(content string)
- func (l *DefaultLoggerProducer) IsClosed() bool
- func (l *DefaultLoggerProducer) IsDebug() bool
- func (l *DefaultLoggerProducer) Log(content string, category Category)
- func (l *DefaultLoggerProducer) Tag() string
- func (l *DefaultLoggerProducer) Warning(content string)
- type Logger
- type LoggerProducer
- type Message
Constants ¶
This section is empty.
Variables ¶
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") )
var ( // CategoryNames maps a given Category to its string name CategoryNames = map[Category]string{ CategoryInfo: "INFO", CategoryWarning: "WARNING", CategoryError: "ERROR", CategoryDebug: "DEBUG", } )
Functions ¶
func CancelContextAndLogOnError ¶ added in v0.1.6
func CancelContextAndLogOnError( ctx context.Context, cancelFn context.CancelFunc, fn func(context.Context) error, loggerProducer LoggerProducer, ) func() error
CancelContextAndLogOnError cancels the context if an error is encountered and logs the error.
Parameters:
ctx: The context to be stopped. cancelFn: A function that cancels 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 cancels the context if an error occurs.
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.
Types ¶
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, ) (*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.
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) 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, debug bool, ) (LoggerProducer, error)
NewProducer returns a new LoggerProducer instance associated with this DefaultLogger.
Parameters:
tag: Tag to identify the logger instance. debug: Flag to indicate if the logger is in debug mode.
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,
debug bool,
) (LoggerProducer, error)
ChangeFilePath(newPath string) error
Run(ctx context.Context, stopFn func()) error
IsRunning() bool
IsClosed() 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 ¶
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.