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

glogr

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: Apache-2.0 Imports: 3 Imported by: 26

README

Minimal Go logging using glog

This package implements the logr interface in terms of Google's glog. This provides a relatively minimalist API to logging in Go, backed by a well-proven implementation.

Documentation

Overview

Package glogr implements github.com/go-logr/logr.Logger in terms of github.com/golang/glog.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() logr.Logger

New returns a logr.Logger which is implemented by glog.

Example
package main

import (
	"errors"
	"flag"
	"os"
	"sync"

	"github.com/go-logr/glogr"
)

var glogInit = sync.Once{}

func initGlog() {
	glogInit.Do(func() {
		_ = flag.Set("v", "1")
		_ = flag.Set("logtostderr", "true")
		flag.Parse()
	})
	os.Stderr = os.Stdout
}

var errSome = errors.New("some error")

func main() {
	initGlog()
	log := glogr.New()
	log.Info("info message with default options")
	log.Error(errSome, "error message with default options")
	log.Info("invalid key", 42, "answer")
	log.Info("missing value", "answer")
	// I1015 08:59:26.952954 2385059 example_test.go:44] "level"=0 "msg"="info message with default options"
	// E1015 08:59:26.953000 2385059 example_test.go:45] "msg"="error message with default options" "error"="some error"
	// I1015 08:59:26.953005 2385059 example_test.go:46] "level"=0 "msg"="invalid key" "<non-string-key: 42>"="answer"
	// I1015 08:59:26.953013 2385059 example_test.go:47] "level"=0 "msg"="missing value" "answer"="<no-value>"
}
Example (WithName)
package main

import (
	"flag"
	"os"
	"sync"

	"github.com/go-logr/glogr"
)

var glogInit = sync.Once{}

func initGlog() {
	glogInit.Do(func() {
		_ = flag.Set("v", "1")
		_ = flag.Set("logtostderr", "true")
		flag.Parse()
	})
	os.Stderr = os.Stdout
}

func main() {
	initGlog()
	log := glogr.New()
	log.WithName("hello").WithName("world").Info("thanks for the fish")
	// I1015 08:59:26.953089 2385059 example_test.go:54] hello/world: "level"=0 "msg"="thanks for the fish"
}

func NewWithOptions added in v0.2.0

func NewWithOptions(opts Options) logr.Logger

NewWithOptions returns a logr.Logger which is implemented by glog.

Example
package main

import (
	"flag"
	"os"
	"sync"

	"github.com/go-logr/glogr"
)

var glogInit = sync.Once{}

func initGlog() {
	glogInit.Do(func() {
		_ = flag.Set("v", "1")
		_ = flag.Set("logtostderr", "true")
		flag.Parse()
	})
	os.Stderr = os.Stdout
}

func main() {
	initGlog()
	log := glogr.NewWithOptions(glogr.Options{LogCaller: glogr.Error})
	log.Info("Info log with LogCaller=Error")
	log.Error(nil, "Error log with LogCaller=All")
	// I1015 09:10:36.754010 2392854 example_test.go:64] "level"=0 "msg"="Info log with LogCaller=Error"
	// E1015 09:10:36.754057 2392854 example_test.go:65] "caller"={"file":"example_test.go","line":65} "msg"="Error log with LogCaller=All" "error"=null
}

Types

type MessageClass added in v0.4.0

type MessageClass int

MessageClass indicates which category or categories of messages to consider.

const (
	// None ignores all message classes.
	None MessageClass = iota
	// All considers all message classes.
	All
	// Info only considers info messages.
	Info
	// Error only considers error messages.
	Error
)

type Options added in v0.2.0

type Options struct {
	// Depth biases the assumed number of call frames to the "true" caller.
	// This is useful when the calling code calls a function which then calls
	// glogr (e.g. a logging shim to another API).  Values less than zero will
	// be treated as zero.
	Depth int

	// LogCaller tells glogr to add a "caller" key to some or all log lines.
	// The glog implementation always logs this information in its per-line
	// header, whether this option is set or not.
	LogCaller MessageClass
}

Options carries parameters which influence the way logs are generated.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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