logutil

package
v0.4.0-b Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2019 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

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

Index

Examples

Constants

View Source
const (
	// ComponentKey is the field key used to store the component name.
	ComponentKey = "component"

	// MethodKey is the field key used to store the methodname.
	MethodKey = "method"
)

Variables

This section is empty.

Functions

func AddComponent added in v0.3.0

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

AddComponent appends the component name to the logrus.Entry.

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

log1 := logutil.AddComponent(log, (*Component1)(nil))
log1.Info("Added first component.")

log2 := logutil.AddComponent(log1, (*Component2)(nil))
log2.Info("Added second component.")

log1.Info("First logger components unchanged.")
Output:

level=info msg="Added first component." component=logutil_test.Component1
level=info msg="Added second component." component="[logutil_test.Component1 logutil_test.Component2]"
level=info msg="First logger components unchanged." component=logutil_test.Component1

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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