Version: v0.0.14 Latest Latest

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

Go to latest
Published: Apr 17, 2021 License: Unlicense, Unlicense Imports: 14 Imported by: 0



This is a very simple, but practical library for logging in applications. Currenty in process of superseding the logi library, once the pipe logger is converted, as it is integrated into the block sync progress logger.

To use it, create a file in a package you want to add the logger to with the name log.go, and then:

go run ./pkg/logg/deploy/.

from the root of the github.com/p9c/pod repository and it replicates its template with the altered package name to match the folder name - so you need to adhere to the rule that package names and the folder name are the same.

The library includes functions to toggle the filtering, highlight and filtering sets while it is running.




View Source
const (
	Off   = "off"
	Fatal = "fatal"
	Error = "error"
	Warn  = "warn"
	Info  = "info"
	Check = "check"
	Debug = "debug"
	Trace = "trace"


View Source
var (
	App          = "   pod"
	AppColorizer = color.White.Sprint

	// LevelSpecs specifies the id, string name and color-printing function
	LevelSpecs = []LevelSpec{
		{logLevels.Off, "off  ", color.Bit24(0, 0, 0, false).Sprintf},
		{logLevels.Fatal, "fatal", color.Bit24(128, 0, 0, false).Sprintf},
		{logLevels.Error, "error", color.Bit24(255, 0, 0, false).Sprintf},
		{logLevels.Check, "check", color.Bit24(255, 255, 0, false).Sprintf},
		{logLevels.Warn, "warn ", color.Bit24(0, 255, 0, false).Sprintf},
		{logLevels.Info, "info ", color.Bit24(255, 255, 0, false).Sprintf},
		{logLevels.Debug, "debug", color.Bit24(0, 128, 255, false).Sprintf},
		{logLevels.Trace, "trace", color.Bit24(128, 0, 255, false).Sprintf},
	Levels = []string{
	LogChanDisabled = uberatomic.NewBool(true)
	LogChan         chan Entry


func AddFilteredSubsystem

func AddFilteredSubsystem(hl string) struct{}

AddFilteredSubsystem adds a new subsystem Name to the highlighted list

func AddHighlightedSubsystem

func AddHighlightedSubsystem(hl string) struct{}

AddHighlightedSubsystem adds a new subsystem Name to the highlighted list

func AddLogChan

func AddLogChan() (ch chan Entry)

AddLogChan adds a channel that log entries are sent to

func AddLoggerSubsystem

func AddLoggerSubsystem(pathBase string) (subsystem string)

AddLoggerSubsystem adds a subsystem to the list of known subsystems and returns the string so it is nice and neat in the package logg.go file

func Caller

func Caller(comment string, skip int) string

func DirectionString

func DirectionString(inbound bool) string

DirectionString is a helper function that returns a string that represents the direction of a connection (inbound or outbound).

func FileExists

func FileExists(filePath string) bool

func LoadHighlightedSubsystems

func LoadHighlightedSubsystems() (o []string)

LoadHighlightedSubsystems returns a copy of the map of highlighted subsystems

func LoadSubsystemFilter

func LoadSubsystemFilter() (o []string)

LoadSubsystemFilter returns a copy of the map of filtered subsystems

func PickNoun

func PickNoun(n int, singular, plural string) string

func SetLogLevel

func SetLogLevel(l string)

SetLogLevel sets the log level via a string, which can be truncated down to one character, similar to nmcli's argument processor, as the first letter is unique. This could be used with a linter to make larger command sets.

func SetLogWriter

func SetLogWriter(wr *io.Writer)

SetLogWriter atomically changes the log io.Writer interface

func SortSubsystemsList

func SortSubsystemsList()

SortSubsystemsList sorts the list of subsystems, to keep the data read-only, call this function right at the top of the main, which runs after declarations and main/init. Really this is just here to alert the reader.

func StoreHighlightedSubsystems

func StoreHighlightedSubsystems(highlights []string) (found bool)

StoreHighlightedSubsystems sets the list of subsystems to highlight

func StoreSubsystemFilter

func StoreSubsystemFilter(filter []string)

StoreSubsystemFilter sets the list of subsystems to filter


type Entry

type Entry struct {
	Time         time.Time
	Level        string
	Package      string
	CodeLocation string
	Text         string

Entry is a log entry to be printed as json to the log file

type LevelPrinter

type LevelPrinter struct {
	// Ln prints lists of interfaces with spaces in between
	Ln func(a ...interface{})
	// F prints like fmt.Println surrounded by log details
	F func(format string, a ...interface{})
	// S prints a spew.Sdump for an interface slice
	S func(a ...interface{})
	// C accepts a function so that the extra computation can be avoided if it is not being viewed
	C func(closure func() string)
	// Chk is a shortcut for printing if there is an error, or returning true
	Chk func(e error) bool

LevelPrinter defines a set of terminal printing primitives that output with extra data, time, log logLevelList, and code location

func GetLogPrinterSet

func GetLogPrinterSet(subsystem string) (Fatal, Error, Warn, Info, Debug, Trace LevelPrinter)

GetLogPrinterSet returns a set of LevelPrinter with their subsystem preloaded

type LevelSpec

type LevelSpec struct {
	ID        int32
	Name      string
	Colorizer func(format string, a ...interface{}) string

Source Files

Jump to

Keyboard shortcuts

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