c4hgol

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: MIT Imports: 7 Imported by: 14

README

c4hgol

Test Coverage Go Report Card GoDoc

import "git.fractalqb.de/fractalqb/c4hgol"


Configuration for Heterogeneous Go Loggers

Though Go has a logging package in its standard library this package is rather basic. IMHO this is the reason why there are several additional and different logging libraries in the Go community which lead to some irritation when one wants to write an application that ends up with having several different logging frameworks included due to package dependencies.

This package does not address the desire to have one unified logger or at least a unified logging interface. – Let the packages choose their loggers… for good or for bad. However, what I want to be able to do is configure all those loggers in my application in a unified way! At least configure some common things that one often wants to configure:

  • Switching loggers for some “topics” on and off
  • Setting the log level of loggers
  • Set a writer as the target for the logging messages
  • (to come: log format?)

Note that the first bullet in the list gives the hint that loggers shall be grouped into topics so that configuration becomes simpler.

Log Packages Architecture Proposition

Make a virtue of necessity…

Theoretical Approach

First of all one has to question why it could come to the situation that a single executable includes different loggin libraries! If each component (library), an executable is made of, returns detailed information about the coutome of each of its offered services (functions/methods) the executable can decide if and how to log those outcomes.—No need for a component to do logging at all, i.e. only the executable uses logging and therefere can arrange to use one logging library.

In theory this could be done for exceptional situations, aka errors. Here one could start to collect context information at the point of failure. This information then propagates back to the executable while beeing enriched with relevant context information. Again, it would be up to the executable to decide what to do with this.

For “small” components and when executables have a rather shallow dependecy graph this approach does not only work well, it is also a desirable approach. – It keeps thigs simple!

Facing the Real World…

…at least when things become more and more complex. In reality we often see requiremenst that break the Theoretical Approach:

  • In practice error/exception handling is rarely perfect and most often only comes with a text message. This makes reasonable decisions difficult for executables.

  • As soon as one also wants information about the happy case details from a deeply “buiried” component it gets difficult, if not impossible, for top-level executable's code to get that.

For short: One has to expect that sufficiently sofisticated components come with their own logging included.

Would that be a problem? IMHO not, as long as you have some means to configure the loggers to your need. I.e. make c4hgol a concept! This would address the need of application developers and gives reasonable choices to component developers:

  1. Component developers can chose their logging API

  2. Logging calls can be static calls, i.e. no dynamic dispatching through interfaces

Used by

Documentation

Overview

Though Go has a logging package in its standard library this package is rather basic. IMHO this is the reason why there are several additional and different logging libraries in the Go community which lead to some irritation when one wants to write an application that ends up with having several different logging frameworks included due to package dependencies.

This package does not address the desire to have one unified logger or at least a unified logging interface. – Let the packages choose their loggers… for good or for bad. However, what I want to be able to do is configure all those loggers in my application in a unified way! At least configure some common things that one often wants to configure:

- Switching loggers for some “topics” on and off

- Setting the log level of loggers

- (may be other things to come in the future)

Note that the first bullet in the list gives the hint that loggers shall be grouped into topics so that configuration becomes simpler.

Index

Constants

View Source
const (
	DefaultFlagLevel = "log"
	DefaultFlagList  = "logs"
)
View Source
const PathSep byte = '/'

Variables

This section is empty.

Functions

func ForLogs added in v0.10.0

func ForLogs(cfg Configurer, do func(current Configurer, path []string))

func LevelCfgDoc

func LevelCfgDoc(levelMap map[string]Level) string

func ListAllLogs added in v0.10.1

func ListAllLogs(cfg Configurer, wr io.Writer, prefix string)

func ListLogs added in v0.10.0

func ListLogs(cfg Configurer, wr io.Writer, prefix string)

func SetLevel

func SetLevel(cfg Configurer, cfgStr string, lvlMap map[string]Level)

TODO describe howto use cfgStr: [[~]log:]level[+(s|S)] (,-sep list of them)

func ShowSource added in v0.10.3

func ShowSource(cfg Configurer, on, filepath bool) bool

Types

type Configurer

type Configurer interface {
	Name() string
	Enabled() bool
	Enable(setEnabled bool)
	Level() Level
	SetLevel(l Level)
	Output() io.Writer
	SetOutput(wr io.Writer)
}

func Config added in v0.10.1

func Config(parent Configurer, sub ...Configurer) Configurer

type Group added in v0.10.0

type Group struct {
	// contains filtered or unexported fields
}

func NewGroup added in v0.10.0

func NewGroup(title string, subtopics ...Configurer) *Group

func (Group) Enable added in v0.10.0

func (t Group) Enable(setEnabled bool)

func (*Group) Enabled added in v0.10.0

func (t *Group) Enabled() (res bool)

func (Group) Level added in v0.10.0

func (t Group) Level() (l Level)

func (*Group) Name added in v0.10.0

func (g *Group) Name() string

func (*Group) Output added in v0.10.0

func (g *Group) Output() io.Writer

func (Group) SetLevel added in v0.10.0

func (t Group) SetLevel(l Level)

func (Group) SetOutput added in v0.10.0

func (t Group) SetOutput(wr io.Writer)

func (Group) ShowSrc added in v0.11.0

func (t Group) ShowSrc(on, filepath bool)

type Level

type Level = int32

Type Level denotes the importance of a log message. It sets 0 to be the normal log message most commonly identified as Info. More important messages have higher values and less important levels get lower values. These values have to be mapped to specific logging packages by the respective Configurer implementation.

Note that there are no predefined constants for Panic and Fatal as these also imply specific behaviour that gose beyond logging a message. For c4hgol we consider Panic and Fatal to be "a separate story".

const (
	LeastImportant Level = -2147483647
	Trace          Level = -1431655764
	Debug          Level = -715827882
	Info           Level = 0
	Warn           Level = 715827882 // ~ 1/3 of 2^31
	Error          Level = 1431655764
	MostImportant  Level = 2147483647
)

type LevelMap

type LevelMap struct {
	Std  []Level
	Impl []int32
}

func (LevelMap) FromStd

func (lm LevelMap) FromStd(level Level) int32

func (LevelMap) ToStd

func (lm LevelMap) ToStd(level int32) Level

type ShowSrcConfigurer added in v0.10.0

type ShowSrcConfigurer interface {
	Configurer
	ShowSrc(on, filepath bool)
}

type Topic

type Topic struct {
	// contains filtered or unexported fields
}

func NewTopic

func NewTopic(represents Configurer, subtopics ...Configurer) *Topic

func (*Topic) Enable

func (t *Topic) Enable(setEnabled bool)

func (*Topic) Enabled

func (t *Topic) Enabled() bool

func (*Topic) Level

func (t *Topic) Level() (l Level)

Level returns the most irrelevant level of all Configurers in this topic. If the topic has no Configurer Info is always returned.

func (*Topic) Name

func (t *Topic) Name() string

func (*Topic) Output

func (t *Topic) Output() io.Writer

func (*Topic) SetLevel

func (t *Topic) SetLevel(l Level)

func (*Topic) SetOutput

func (t *Topic) SetOutput(wr io.Writer)

func (Topic) ShowSrc added in v0.10.0

func (t Topic) ShowSrc(on, filepath bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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