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

log

package
v1.3.100 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package log pkg/cxo/node/log/log.go c2-net-cxo

Index

Constants

View Source
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
}

A Config represents configuration for logger

func NewConfig

func NewConfig() (c Config)

NewConfig returns Config with default values

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

func NewLogger

func NewLogger(c Config) Logger

NewLogger create new Logger using given Config. By default flags of the Logger is log.Lshortfile|log.Ltime

type Pin

type Pin uint

A Pin of a debug log

Jump to

Keyboard shortcuts

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