123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // Package logger provides logging functionality
- package logger
- import (
- "errors"
- "fmt"
- )
- // Common logger error types
- var (
- // ErrInvalidFormat indicates an invalid log format was specified
- ErrInvalidFormat = errors.New("invalid log format")
- // ErrInvalidLevel indicates an invalid log level was specified
- ErrInvalidLevel = errors.New("invalid log level")
- // ErrOutputFailed indicates a failure to set or write to the log output
- ErrOutputFailed = errors.New("failed to set or write to log output")
- // ErrFormatterFailed indicates a failure in the log formatter
- ErrFormatterFailed = errors.New("log formatter failed")
- // ErrInvalidConfig indicates missing or invalid logger configuration
- ErrInvalidConfig = errors.New("invalid logger configuration")
- )
- // Error represents a logger-specific error with detailed context
- type Error struct {
- // Op is the operation that failed (e.g., "format", "level", "output")
- Op string
- // Err is the underlying error
- Err error
- // Details contains additional error context
- Details string
- // Setting indicates the configuration setting involved (if applicable)
- Setting string
- // Value indicates the invalid value (if applicable)
- Value string
- }
- // Error returns a string representation of the error
- func (e *Error) Error() string {
- msg := fmt.Sprintf("logger %s failed", e.Op)
- if e.Setting != "" {
- msg = fmt.Sprintf("%s for setting %q", msg, e.Setting)
- }
- if e.Value != "" {
- msg = fmt.Sprintf("%s with value %q", msg, e.Value)
- }
- if e.Details != "" {
- msg = fmt.Sprintf("%s: %s", msg, e.Details)
- }
- if e.Err != nil {
- msg = fmt.Sprintf("%s: %v", msg, e.Err)
- }
- return msg
- }
- // Unwrap returns the underlying error
- func (e *Error) Unwrap() error {
- return e.Err
- }
- // Is reports whether target matches this error
- func (e *Error) Is(target error) bool {
- return errors.Is(e.Err, target)
- }
- // NewError creates a new logger error
- func NewError(op string, err error, details string) *Error {
- return &Error{
- Op: op,
- Err: err,
- Details: details,
- }
- }
- // NewConfigError creates a new logger configuration error
- func NewConfigError(op string, setting string, value string, err error, details string) *Error {
- return &Error{
- Op: op,
- Setting: setting,
- Value: value,
- Err: err,
- Details: details,
- }
- }
- // IsConfigError returns true if the error indicates a configuration problem
- func IsConfigError(err error) bool {
- var e *Error
- return errors.As(err, &e) && (errors.Is(e.Err, ErrInvalidFormat) ||
- errors.Is(e.Err, ErrInvalidLevel) ||
- errors.Is(e.Err, ErrInvalidConfig))
- }
- // IsOutputError returns true if the error indicates an output problem
- func IsOutputError(err error) bool {
- var e *Error
- return errors.As(err, &e) && errors.Is(e.Err, ErrOutputFailed)
- }
|