package logerrcapture

Import Path
	github.com/efficientgo/core/logerrcapture (on go.dev)

Dependency Relation
	imports 4 packages, and imported by one package

Involved Source Files do.go Package logerrcapture implements robust error handling in defer statements using provided logger. The Close a `io.Closer` interface or execute any function that returns error safely while logging error. It's often forgotten but it's a caller responsibility to close all implementations of `Closer`, such as *os.File or io.ReaderCloser. Commonly we would use: defer closer.Close() This is wrong. Close() usually return important error (e.g for os.File the actual file flush might happen and fail on `Close` method). It's very important to *always* check error. `logerrcapture` provides utility functions to capture error and log it via provided logger, while still allowing to put them in a convenient `defer` statement: func <...>(...) (err error) { ... defer logerrcapture.Do(logger, closer.Close, "log format message") ... } If Close returns error, `logerrcapture.Do` will capture it, add to input error if not nil and return by argument. Example: func DoAndClose(f *os.File, logger logerrcapture.Logger) error { defer logerrcapture.Do(logger, f.Close, "close file at the end") // Do something... if err := do(); err != nil { return err } return nil } The logerrcapture.ExhaustClose function provide the same functionality but takes an io.ReadCloser and exhausts the whole reader before closing. This is useful when trying to use http keep-alive connections because for the same connection to be re-used the whole response body needs to be exhausted. Recommended: Check https://pkg.go.dev/github.com/efficientgo/tools/pkg/errcapture if you want to return error instead of just logging (causing hard error).
Package-Level Type Names (only one)
/* sort by: | */
Logger interface compatible with go-kit/logger. ( Logger) Log(keyvals ...interface{}) error github.com/go-kit/log.Logger (interface) github.com/go-kit/log.LoggerFunc github.com/go-kit/log.StdlibAdapter *github.com/go-kit/log.SwapLogger Logger : github.com/go-kit/log.Logger func Do(logger Logger, doer doFunc, format string, a ...interface{}) func ExhaustClose(logger Logger, r io.ReadCloser, format string, a ...interface{})
Package-Level Functions (total 2)
Do is making sure we log every error, even those from best effort tiny functions.
ExhaustClose closes the io.ReadCloser with a log message on error but exhausts the reader before.