std

package
v0.0.0-...-af2a51d Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 3 Imported by: 3

Documentation

Overview

Package std implements Tim Hockin logr.Logger using Golangs standard log.Logger.

Example
package main

import (
	stdlog "log"
	"os"

	log "github.com/corvus-ch/logr/std"
)

func main() {
	l := log.New(1, stdlog.New(os.Stdout, "", stdlog.Lshortfile))
	l.Info("Info level log message")
	l.Infof("%X", "Info level log message printed in hex values")
	l.Error("Error level log message")
	l.Errorf("%X", "Error level log message printed in hex values")
	l.NewWithPrefix("adipiscing").Info("This message is prefixed")
	l.V(1).Info("Debug level message")
	l.V(1).Infof("%X", "Debug level message in hex values")
	l.V(2).Info("This message will not be printed as its verbosity exceeds the maximum")
}
Output:

logger_test.go:17: Info level log message
logger_test.go:18: 496E666F206C6576656C206C6F67206D657373616765207072696E74656420696E206865782076616C756573
logger_test.go:19: Error level log message
logger_test.go:20: 4572726F72206C6576656C206C6F67206D657373616765207072696E74656420696E206865782076616C756573
adipiscinglogger_test.go:21: This message is prefixed
logger_test.go:22: Debug level message
logger_test.go:23: 4465627567206C6576656C206D65737361676520696E206865782076616C756573
Example (ThreeLoggers)
package main

import (
	"bytes"
	"fmt"

	stdlog "log"
	"os"

	log "github.com/corvus-ch/logr/std"
)

func main() {
	buf1 := &bytes.Buffer{}
	buf2 := &bytes.Buffer{}
	buf3 := &bytes.Buffer{}
	log1 := stdlog.New(buf1, "", 0)
	log2 := stdlog.New(buf2, "", 0)
	log3 := stdlog.New(buf3, "", 0)
	l := log.New(1, log1, log2, log3)
	l.Info("Info level log message")
	l.Infof("%X", "Info level log message printed in hex values")
	l.Error("Error level log message")
	l.Errorf("%X", "Error level log message printed in hex values")
	l.NewWithPrefix("adipiscing").Info("This message is prefixed")
	l.V(1).Info("This message will be printed with debug level")
	l.V(1).Infof("%X", "This message will be printed with debug level as hex values")
	l.V(2).Info("This message will not be printed as its verbosity exceeds the maximum")
	fmt.Fprintln(os.Stdout, "Output of first logger:")
	buf1.WriteTo(os.Stdout)
	fmt.Fprintln(os.Stdout, "Output of second logger:")
	buf2.WriteTo(os.Stdout)
	fmt.Fprintln(os.Stdout, "Output of third logger:")
	buf3.WriteTo(os.Stdout)
}
Output:

Output of first logger:
Error level log message
4572726F72206C6576656C206C6F67206D657373616765207072696E74656420696E206865782076616C756573
Output of second logger:
Info level log message
496E666F206C6576656C206C6F67206D657373616765207072696E74656420696E206865782076616C756573
adipiscingThis message is prefixed
Output of third logger:
This message will be printed with debug level
54686973206D6573736167652077696C6C206265207072696E7465642077697468206465627567206C6576656C206173206865782076616C756573
Example (TwoLoggers)
package main

import (
	"bytes"
	"fmt"

	stdlog "log"
	"os"

	log "github.com/corvus-ch/logr/std"
)

func main() {
	buf1 := &bytes.Buffer{}
	buf2 := &bytes.Buffer{}
	log1 := stdlog.New(buf1, "", 0)
	log2 := stdlog.New(buf2, "", 0)
	l := log.New(1, log1, log2)
	l.Info("Info level log message")
	l.Infof("%X", "Info level log message printed in hex values")
	l.Error("Error level log message")
	l.Errorf("%X", "Error level log message printed in hex values")
	l.NewWithPrefix("adipiscing").Info("This message is prefixed")
	l.V(1).Info("This message will be printed with debug level")
	l.V(1).Infof("%X", "This message will be printed with debug level as hex values")
	l.V(2).Info("This message will not be printed as its verbosity exceeds the maximum")
	fmt.Fprintln(os.Stdout, "Output of first logger:")
	buf1.WriteTo(os.Stdout)
	fmt.Fprintln(os.Stdout, "Output of second logger:")
	buf2.WriteTo(os.Stdout)
}
Output:

Output of first logger:
Error level log message
4572726F72206C6576656C206C6F67206D657373616765207072696E74656420696E206865782076616C756573
Output of second logger:
Info level log message
496E666F206C6576656C206C6F67206D657373616765207072696E74656420696E206865782076616C756573
adipiscingThis message is prefixed
This message will be printed with debug level
54686973206D6573736167652077696C6C206265207072696E7465642077697468206465627567206C6576656C206173206865782076616C756573

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	logr.Logger
	// contains filtered or unexported fields
}

func New

func New(verbosity int, ll ...*log.Logger) *Logger

New creates a new instance of logr.Logger.

The function takes one or several *log.Logger instances. If only one *log.Logger is provided, this logger will be used for all log levels. If two *log.Logger instances are provided, the first one will be used for logs with level error and the second for all info levels. If tree or more *log.Logger instances are provided, the third logger and any consecutive loggers are used for the verbose levels created with logr.Logger.V().

The verbosity defines the upper limit. When creating a new sub logger using logr.Logger.V(), if the level passed to V is greater than verbosity, the sub logger will be silenced.

If verbosity is smaller than the number of *log.Logger instances, any additional logger will be ignored. If verbosity is greater than the number of *log.Logger instances, the last logger will be used to fill in for any missing levels.

This implementation takes control over the prefix of *log.Logger. Any prefix set on instantiation, will be ignored. Flags are preserved.

Example:

l1 := log.New(os.Stderr, "", 0)
l2 := log.New(os.Stdout, "", 0)
l3 := log.New(&bytes.Buffer{}, "", 0)

// All output written to the same logger.
lgr1 := New(1, l2)
lgr1.Error("I will be written to STDERR")
lgr1.Info("I will be written to STDERR too")
lgr1.V(1).Info("And mee too")
lgr1.V(2).Info("I will be ignored")

// Output written to tree different loggers depending on the severity and verbosity.
lgr2 := New(1, l1, l2, l3)
lgr2.Error("I will be written to STDERR")
lgr2.Info("I will be written to STDOUT")
lgr2.V(1).Info("I will be buffered")
lgr2.V(2).Info("I will be ignored")

// Output written only to the first two loggers. The third one is ignored.
lgr3 := New(0, l1, l2, l3)
lgr3.Error("I will be written to STDERR")
lgr3.Info("I will be written to STDOUT")
lgr3.V(1).Info("I will be ignored")
lgr3.V(2).Info("And I will be ignored too")

func (Logger) Enabled

func (l Logger) Enabled() bool

Enabled implements logr.Logger.Enabled by checking if the current verbosity level is less or equal than the loggers maximum verbosity.

func (Logger) Error

func (l Logger) Error(args ...interface{})

Error implements logr.Logger.Error by writing to the first log.Logger.

func (Logger) Errorf

func (l Logger) Errorf(format string, args ...interface{})

Errorf implements logr.Logger.Errorf by writing to the first log.Logger.

func (Logger) Info

func (l Logger) Info(args ...interface{})

Info implements logr.Logger.Info by writing to log.Logger of with the matching level.

func (Logger) Infof

func (l Logger) Infof(format string, args ...interface{})

Infof implements logr.Logger.Infof by writing to log.Logger of with the matching level.

func (Logger) NewWithPrefix

func (l Logger) NewWithPrefix(prefix string) logr.Logger

NewWithPrefix implements logr.Logger.NewWithPrefix.

func (*Logger) SetCallDepth

func (l *Logger) SetCallDepth(depth int)

SetCallDepth sets the call depth passed to log.Logger.Output.

func (Logger) V

func (l Logger) V(level int) logr.InfoLogger

V implements logr.Logger.V.

Jump to

Keyboard shortcuts

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