log

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2021 License: Unlicense, Unlicense Imports: 13 Imported by: 0

README

log

Logs for concurrent and modular systems

How to use this

In the version/ directory is a template that you want to copy to the root of your repository.

Change the references in the update/ directory to point to your new repo URL version directory:

import (
	"cybriq.systems/git/x/base/log/version"
)

It is of course permissible to use this however you want, but the author's recommendation is to create a file like this, that you drop in any package you want to access this logger from:

package main

import (
	_l "cybriq.systems/git/x/base/pkg/log"
	
	"cybriq.systems/git/x/base/log/version"
)

var log = _l.Get(_l.Add(version.PathBase))

which will give you back a struct with memorable members pointing to log print functions that you can use like this:

var e error
if e = SomeFunction(); log.E.Chk(e) {
	return e
}

which will print the error at the location and pass it back.

There are other ways to use it, nothing stopping the direct creation of the logger inside an arbitrary file of a package, just that, like everything about this 'freedom' in the Go package specification, including especially init() and var declarations that invoke functions, it can get lost, thus the suggestion to put it in a file with a meaningful name so it's obvious where to look to change it.

And, whoever is using those init functions: Please, stop, and think about someone, you know, maintaining it after you. IMO Go should not allow it in any file not named 'init.go'. Well, I like 'main.go' also. It is not for nothing that a key goal of the cybriq packages author is to eliminate the manual structuring of a package folder, and just have it natively structured in the editor.

Regarding the use of the letter e instead of err in the code above. Except for certain transcendental math, e, like i, logically connects to the concept of error, especially in a language where err != nil is so frequent it's a meme. This is also why there is a 'check' function, as this ensures that the code

logs at the site of the error!!!!

so that you can jump straight to it and fix it, instead of spending 20 minutes tracing back from the hole it emerges from, through the little warren of imports and invocations where it originates.

Documentation

Index

Constants

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

Variables

View Source
var (
	App          = "  main"
	AppColorizer = color.Gray.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{
		Off,
		Fatal,
		Error,
		Check,
		Warn,
		Info,
		Debug,
		Trace,
	}
	LogChanDisabled = atomic.NewBool(true)
	LogChan         chan Entry
)

Functions

func Add

func Add(pathBase string) (subsystem string)

Add 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 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 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 SetLogWriteToFile

func SetLogWriteToFile(path, appName string) (e error)

func SetLogWriter

func SetLogWriter(wr io.Writer)

SetLogWriter atomically changes the log io.Writer interface

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

Types

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

type LevelSpec

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

type Logger

type Logger struct {
	F, E, W, I, D, T LevelPrinter
}

func Get

func Get(subsystem string) (l *Logger)

Get returns a set of LevelPrinter with their subsystem preloaded

Jump to

Keyboard shortcuts

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