Documentation
¶
Overview ¶
Package errorlogger implements error logging to a logrus log (or a standard library log) by providing a convenient way to log errors and to temporarily disable/enable logging.
A global Log and Err with default behaviors are supplied that may be aliased if you wish:
Log = errorlogger.Log Err = errorlogger.Err
If you do not intend to use any options or disable the logger, it may be more convenient to use a function alias to call the most common method, Err(), like this:
var Err = errorlogger.New().Err
then, just call the function:
err := someProcess(stuff)
if err != nil {
return Err(err)
}
Either way, the default ErrorLogger is enabled and ready to go:
EL := errorlogger.New() // enabled by default Err := EL.Err
If a private ErrorLogger is desired, or if name collisions with Err cause conflicts, you may implement your own.
myErr := errorlogger.New() err := myErr.Err
Example:
f, err := os.Open("somefile.txt")
if err != nil {
return nil, e.Err(err) // avoids additional logging steps
}
e.Disable() // can be disabled and enabled as desired
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Log is the default global ErrorLogger. It implements the // ErrorLogger interface as well as the logrus.Logger interface, // which is compatible with the standard library "log" package. Log = New() // Err is the logging function for the global ErrorLogger. Err = Log.Err )
var ErrInvalidWriter = os.ErrInvalid
ErrInvalidWriter is returned when a log output writer is passed that does not implement io.Writer.
Functions ¶
func SetJSONFormatter ¶ added in v0.2.0
func SetJSONFormatter()
SetJSONFormatter sets the log formatter to a logrus JSON formatter with default characteristics. Use
Log.SetJSONFormatter(defaultTextFormatter)
to return to default text formatting of logs.
Many third party logging formatters are available.
- FluentdFormatter. Formats entries that can be parsed by Kubernetes and Google Container Engine.
- GELF. Formats entries so they comply to Graylog's GELF 1.1 specification.
- logstash. Logs fields as Logstash Events.
- prefixed. Displays log entry source along with alternative layout.
- zalgo. Invoking the Power of Zalgo.
- nested-logrus-formatter. Converts logrus fields to a nested structure.
- powerful-logrus-formatter. get fileName, log's line number and the latest function's name when print log; Sava log to files.
- caption-json-formatter. logrus's message json formatter with human-readable caption added.
Reference: https://pkg.go.dev/github.com/sirupsen/logrus#JSONFormatter
func SetLogOutput ¶ added in v0.2.0
SetLogOutput sets the output writer for logging. The default is os.Stderr. Any io.Writer can be setup to receive messages.
func SetTextFormatter ¶ added in v0.2.0
func SetTextFormatter()
SetTextFormatter sets the log formatter to the default text log formatter for use with TTY logging. Use
Log.SetJSONFormatter(defaultJSONFormatter)
to return to set JSON formatting of logs.
Many third party logging formatters are available.
- FluentdFormatter. Formats entries that can be parsed by Kubernetes and Google Container Engine.
- GELF. Formats entries so they comply to Graylog's GELF 1.1 specification.
- logstash. Logs fields as Logstash Events.
- prefixed. Displays log entry source along with alternative layout.
- zalgo. Invoking the Power of Zalgo.
- nested-logrus-formatter. Converts logrus fields to a nested structure.
- powerful-logrus-formatter. get fileName, log's line number and the latest function's name when print log; Sava log to files.
- caption-json-formatter. logrus's message json formatter with human-readable caption added. Reference: https://pkg.go.dev/github.com/sirupsen/logrus#TextFormatter
Types ¶
type ErrorLogger ¶ added in v0.2.0
type ErrorLogger interface {
// Disable disables logging and sets a no-op function for
// Err() to prevent slowdowns while logging is disabled.
Disable()
// Enable enables logging and restores the Err() logging functionality.
Enable()
// EnableText enables text formatting of log errors (default)
EnableText()
// EnableJSON enables JSON formatting of log errors
EnableJSON()
// LogLevel sets the logging level from a string value.
// Allowed values: Panic, Fatal, Error, Warn, Info, Debug, Trace
SetLogLevel(lvl string) error
// Err logs an error to the provided logger, if it is enabled,
// and returns the error unchanged.
Err(err error) error
// SetLoggerFunc allows setting of the logger function.
// The default is log.Error(), which is compatible with
// the standard library log package and logrus.
SetLoggerFunc(fn LoggerFunc)
// SetErrorWrap allows ErrorLogger to wrap errors in a
// specified custom type. For example, if you want all errors
// returned to be of type *os.PathError
SetErrorWrap(wrap error)
logrus.Ext1FieldLogger
}
ErrorLogger implements error logging to a logrus log (or a standard library log) by providing convenience methods, advanced formatting options, more automated logging, a more efficient way to log errors within code, and methods to temporarily disable/enable logging, such as in the case of performance optimization or during critical code blocks.
func New ¶ added in v0.2.0
func New() ErrorLogger
New returns a new ErrorLogger with default options and logging enabled. Most users will not need to call this, since the default global ErrorLogger EL is provided.
func NewWithOptions ¶ added in v0.2.0
func NewWithOptions(enabled bool, fn LoggerFunc, wrap error) ErrorLogger
NewWithOptions returns a new ErrorLogger with options determined by parameters.
type LoggerFunc ¶ added in v0.2.0
type LoggerFunc = func(args ...interface{})
LoggerFunc defines the function signature used when logging errors.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
example/errorlogger
command
Example for package errorlogger.
|
Example for package errorlogger. |