Documentation
¶
Overview ¶
Package log pkg/cxo/node/log/log.go c2-net-cxo
Index ¶
Constants ¶
const ( Prefix string = "" // default prefix Debug bool = false // don't show debug logs by default All Pin = ^Pin(0) // default Debug pins (all pins) No Pin = 0 // no pins )
defaults
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Prefix string // log prefix
Debug bool // show debug logs
Pins Pin // debug pins
Output io.Writer // provide an output
// DebugIf, when set, is asked on every debug line, so debug output can be
// turned on and off while the logger runs; Debug is then ignored.
DebugIf func() bool
}
A Config represents configuration for logger
func (*Config) FromFlags ¶
func (c *Config) FromFlags()
FromFlags parses commandline flags. So, you need to call flag.Parse after this method. There is -log-prefix flag. And also, it provides -debug flag and -debug-pins. If the debug flag set to false, then -debug-pins ignored and the pins set to No.
type Logger ¶
type Logger interface {
// Pins of the Logger
Pins() Pin
SetPrefix(string) // set prefix of underlying log.Logger
SetFlags(int) // set Flags of undelrying log.Logger
SetOutput(io.Writer) // set Output of underlying log.Logger
Print(...interface{}) //
Println(...interface{}) //
Printf(string, ...interface{}) //
Panic(...interface{}) //
Panicln(...interface{}) //
Panicf(string, ...interface{}) //
Fatal(...interface{}) //
Fatalln(...interface{}) //
Fatalf(string, ...interface{}) //
Debug(pin Pin, args ...interface{}) //
Debugln(pin Pin, args ...interface{}) //
Debugf(pin Pin, format string, args ...interface{}) //
Error(err error, args ...interface{})
Errorln(err error, args ...interface{})
Errorf(err error, format string, args ...interface{})
}
A Logger is similar to log.Logger with Debug methods. The Debug methods uses Pin to show or not to show a message. Provided Pin compared with configured using logical AND. If result is not 0, then message will be printed. Thus you can separate debug messages printing only messages you want. For example:
const (
LogTransport log.Pin = 1 < iota
LogMessages
LogSoemthing
)
c := NewConfig()
c.Prefix = "[node] "
c.Debug = true // enable debug messages
c.Pins = log.All // show all debug messages
//
// show all debug logs
//
l := NewLogger(c)
l.Debug(LogTransport, "new connection from 127.0.0.1:9090")
l.Debug(LogMessage, "message X received")
l.Debug(LogSomething, "something happens")
// [node] new connection from 127.0.0.1:9090
// [node] message X received
// [node] something happens
//
// show only transport and "something" logs
//
// ...
c.Pins = LogTransport | LogSomething
l := NewLogger(c)
// ...
// [node] new connection from 127.0.0.1:9090
// [node] something happens
This way you can provide detailed logs without caring about big output. This feature applies only to debug logs. Set Debug field of the Config to turn all debug logs off