logutil

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 14 Imported by: 1

Documentation

Overview

Package logutil is a utility package for github.com/sirupsen/logrus.

Index

Examples

Constants

View Source
const (
	// SourceKey is the field key used to store the type name of the logging
	// source.
	SourceKey = "source"

	// MethodKey is the field key used to store the method name where the logger
	// is being called.
	MethodKey = "method"

	// ComponentKey is the field key used to store the logging object's component
	// path.
	ComponentKey = "component"
)

Variables

This section is empty.

Functions

func NewLogger added in v0.6.2

func NewLogger(opts ...Option) (*logrus.Entry, error)

NewLogger creates an application-level logrus.Entry.

func NoopEntry

func NoopEntry() *logrus.Entry

NoopEntry returns a no-op logrus.NoopEntry.

Example
package main

import (
	"os"

	"github.com/sirupsen/logrus"
	"go.stevenxie.me/gopkg/logutil"
)

var _logrusFormatter = &logrus.TextFormatter{DisableTimestamp: true}

func main() {
	// Regular logger will produce output.
	log := logrus.New()
	log.SetOutput(os.Stdout)
	log.SetFormatter(_logrusFormatter)
	log.WithField("kind", "default").Info("Oh-ho, who's this?")

	// No-op logger will not produce output.
	noopLog := logutil.NoopLogger()
	noopLog.SetFormatter(_logrusFormatter)
	noopLog.WithField("kind", "noop").Info("Hey, it's a-me.")

}
Output:

level=info msg="Oh-ho, who's this?" kind=default

func NoopLogger

func NoopLogger() *logrus.Logger

NoopLogger returns a no-op logrus.NoopLogger.

func WithComponent added in v0.2.2

func WithComponent(e *logrus.Entry, component string) *logrus.Entry

WithComponent appends the component to the component path field of the logrus.Entry.

func WithMethod added in v0.3.0

func WithMethod(e *logrus.Entry, v zero.Interface) *logrus.Entry

WithMethod adds the name of the method v to the logrus.Entry.

Example
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetFormatter(_logrusFormatter)

ss := SomeStruct{log: logrus.NewEntry(l)}
ss.LogWithMethod()
Output:

level=info msg="Hello from method!" method=LogWithMethod

func WithSource added in v0.6.3

func WithSource(e *logrus.Entry, v zero.Interface) *logrus.Entry

WithSource adds the type name of v to a logrus.Entry.

Types

type Config added in v0.6.2

type Config struct {
	// Logging options.
	Level        logrus.Level
	ReportCaller bool

	// Output options.
	Output        io.Writer
	Format        Format
	TextFormatter logrus.TextFormatter
	JSONFormatter logrus.JSONFormatter

	// Extension options.
	Raven *raven.Client
}

A Config configures a logrus.Logger.

type Format added in v0.6.2

type Format uint8

A Format represents an output formatting type for Logrus.

const (
	UnknownFormat Format = iota
	JSONFormat
	TextFormat
)

The set of logrus formats.

func ParseFormat added in v0.6.3

func ParseFormat(fmt string) (Format, error)

ParseFormat parses a Format from the string fmt.

func (Format) String added in v0.6.2

func (fmt Format) String() string

func (*Format) UnmarshalMap added in v0.6.2

func (fmt *Format) UnmarshalMap(v zero.Interface) (err error)

UnmarshalMap unmarshals fmt from a mapstructure.

type Option added in v0.6.2

type Option func(*Config)

A Option modifies a LogrusConfig.

func WithCaller added in v0.6.3

func WithCaller(enable bool) Option

WithCaller configures a logrus.Logger to report callers.

func WithFormat added in v0.6.3

func WithFormat(fmt Format) Option

WithFormat configures the output format of a logrus.Logger.

func WithLevel added in v0.6.3

func WithLevel(lvl logrus.Level) Option

WithLevel configures a logrus.Logger to log at lvl.

func WithLevelString added in v0.6.3

func WithLevelString(lvl string) Option

WithLevelString is like LogrusWithLevel, but parses the level from lvl.

It panics if lvl is an invalid logrus.Level.

func WithOutput added in v0.6.3

func WithOutput(output io.Writer) Option

WithOutput configures a logrus.Logger to write to output.

func WithSentry added in v0.6.3

func WithSentry(client *raven.Client) Option

WithSentry adds a Sentry reporting hook to a logrus.Logger.

Jump to

Keyboard shortcuts

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