Help us shape the future of Go! Take the 2026 Go Developer Survey

errorlogger

package module
v0.1.1-1619100759822343 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2021 License: MIT Imports: 4 Imported by: 0

README

errorlogger

Tricky and fun utilities for Go programs.


GitHub Workflow Status Codecov

Contributor Covenant

Twitter Follow GitHub followers


Getting Started

Prerequisites

Developed with $( go version; ). Go is extremely backwards compatible and semver stable. Nearly any v1.x should work fine.


Installation

To use this repo as a template for your own project:

gh repo create -y --public --template "https://github.com/skeptycal/errorlogger"

Clone this repo to test and contribute:

# add repo to $GOPATH (xxxxxx is your computer login username)
go get github.com/xxxxxx/errorlogger

cd ${GOPATH}/src/github.com/xxxxxx/errorlogger

# test results and coverage info
./go.test.sh

# install as a utility package
go install

Use the Issues and PR templates on the GitHub repo page to contribute.


Basic Usage

This is a copy of the example script available in the cmd/example/errorlogger folder:

package main

import "github.com/skeptycal/errorlogger"

func main() {
    errorlogger.Example()
}

To try it out:

# change to the sample folder
cd cmd/example/errorlogger

# run the main.go program
go run ./main.go

# to compile as an executable
go build

Code of Conduct and Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us. Please read the Code of Conduct for details before submitting anything.


Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.


Contributors and Inspiration

  • Michael Treanor (GitHub / Twitter) - Initial work, updates, maintainer
  • Francesc Campoy - Inspiration and great YouTube videos!

See also the list of contributors who participated in this project.


License

Licensed under the MIT https://opensource.org/licenses/MIT - see the LICENSE file for details.

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 EL and Err with default behaviors are supplied that may be aliased if you wish:

EL = errorlogger.EL
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

View Source
var (
	DefaultLogger = &logrus.Logger{

		Out: os.Stderr,

		Formatter: defaultFormatter,

		Hooks: make(logrus.LevelHooks),

		Level: defaultLogLevel,
	}
)

Defaults for ErrorLogger

View Source
var EL = New()
View Source
var Err = EL.Err

Log implements the default logrus error logger

Reference: https://github.com/sirupsen/logrus/

Functions

func Example

func Example()

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)
}

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.

func New added in v0.2.0

func New() ErrorLogger

New returns a new ErrorLogger with logging enabled.

func NewWithOptions added in v0.2.0

func NewWithOptions(enabled bool, fn loggerFunc, wrap error) ErrorLogger

type Level added in v0.2.0

type Level = logrus.Level

Level type values: Panic, Fatal, Error, Warn, Info, Debug, Trace

Directories

Path Synopsis
cmd
example/errorlogger command
Example for package errorlogger.
Example for package errorlogger.

Jump to

Keyboard shortcuts

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