logger

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: GPL-3.0 Imports: 12 Imported by: 45

README

andes-logger-go

Dharitri' logger subsystem written in go

CLI options

Logs producer (Dharitri Node)
  • log-level: comma-separated pairs of (loggerName, logLevel)
  • log-correlation: option to include correlation elements in the logs
  • log-logger-name: option to include logger name in the logs

Example:

--log-level="*:INFO,processor:DEBUG" --log-correlation --log-logger-name
Logs viewer
  • level: comma-separated pairs of (loggerName, logLevel)
  • correlation: option to include correlation elements in the logs
  • logger-name: option to include logger name in the logs

Example:

--level="*:INFO,processor:DEBUG" --correlation --logger-name

Documentation

Index

Constants

View Source
const ASCIILineFeed = byte('\r')
View Source
const ASCIINewLine = byte('\n')
View Source
const ASCIISpace = byte(' ')
View Source
const ASCIITab = byte('\t')

Variables

View Source
var ErrInvalidLogLevelPattern = errors.New("un-parsable log level and pattern provided")

ErrInvalidLogLevelPattern signals that an un-parsable log level and patter was provided

View Source
var ErrNilDisplayByteSliceHandler = errors.New("nil display byte slice handler")

ErrNilDisplayByteSliceHandler signals that a nil display byte slice handler has been provided

View Source
var ErrNilFormatter = errors.New("nil formatter provided")

ErrNilFormatter signals that a nil formatter has been provided

View Source
var ErrNilMarshalizer = errors.New("nil marshalizer")

ErrNilMarshalizer signals that a nil marshalizer has been provided

View Source
var ErrNilWriter = errors.New("nil writer provided")

ErrNilWriter signals that a nil writer has been provided

View Source
var ErrWriterNotFound = errors.New("writer not found while searching container")

ErrWriterNotFound signals that the provided writer was not found while searching container list

Levels contain all defined levels as a slice for an easier iteration

Functions

func AddLogObserver

func AddLogObserver(w io.Writer, formatter Formatter) error

AddLogObserver adds a new observer (writer + formatter) to the already built-in log observers queue This method is useful when adding a new output device for logs is needed (such as files, streams, API routes and so on)

func ClearLogObservers

func ClearLogObservers()

ClearLogObservers clears the observers lists

func DisplayByteSlice

func DisplayByteSlice(slice []byte) string

DisplayByteSlice converts the provided byte slice to its string representation using displayByteSlice function pointer

func GetCorrelation

func GetCorrelation() proto.LogCorrelationMessage

GetCorrelation gets global correlation elements

func GetLogLevelPattern

func GetLogLevelPattern() string

GetLogLevelPattern returns the last set log level pattern. The format returned is MATCHING_STRING1:LOG_LEVEL1,MATCHING_STRING2:LOG_LEVEL2".

func GetOrCreate

func GetOrCreate(name string) *logger

GetOrCreate returns a log based on the name provided, generating a new log if there is no log with provided name

func IsEnabledCorrelation

func IsEnabledCorrelation() bool

IsEnabledCorrelation returns whether correlation elements are enabled

func IsEnabledLoggerName

func IsEnabledLoggerName() bool

IsEnabledLoggerName returns whether logger name is enabled

func NewLogLineWrapperFormatter

func NewLogLineWrapperFormatter(marshalizer Marshalizer) (*logLineWrapperFormatter, error)

NewLogLineWrapperFormatter creates a new logLineWrapperFormatter that is able to marshalize the provided logLine

func NewLogOutputSubject

func NewLogOutputSubject() *logOutputSubject

NewLogOutputSubject returns an initialized, empty logOutputSubject with no observers

func NewLogger

func NewLogger(name string, logLevel LogLevel, logOutput LogOutputHandler) *logger

NewLogger create a new logger instance

func NewProfileChangeSubject

func NewProfileChangeSubject() *profileChangeSubject

NewProfileChangeSubject -

func NotifyProfileChange

func NotifyProfileChange()

NotifyProfileChange notifies observers about a profile change

func RemoveLogObserver

func RemoveLogObserver(w io.Writer) error

RemoveLogObserver removes an exiting observer by providing the writer pointer.

func SetCorrelationEpoch

func SetCorrelationEpoch(epoch uint32)

SetCorrelationEpoch sets the current epoch as a log correlation element

func SetCorrelationRound

func SetCorrelationRound(round int64)

SetCorrelationRound sets the current round as a log correlation element

func SetCorrelationShard

func SetCorrelationShard(shardID string)

SetCorrelationShard sets the current shard ID as a log correlation element

func SetCorrelationSubround

func SetCorrelationSubround(subRound string)

SetCorrelationSubround sets the current sub-round as a log correlation element

func SetDisplayByteSlice

func SetDisplayByteSlice(f func(slice []byte) string) error

SetDisplayByteSlice sets the converter function from byte slice to string default, this will call hex.EncodeToString

func SetLogLevel

func SetLogLevel(logLevelAndPattern string) error

SetLogLevel changes the log level of the contained loggers. The expected format is "MATCHING_STRING1:LOG_LEVEL1,MATCHING_STRING2:LOG_LEVEL2". If matching string is *, it will change the log levels of all contained loggers and will also set the defaultLogLevelProperty. Otherwise, the log level will be modified only on those loggers that will contain the matching string on any position. For example, having the parameter "DEBUG|process" will set the DEBUG level on all loggers that will contain the "process" string in their name ("process/sync", "process/interceptors", "process" and so on). The rules are applied in the exact manner as they are provided, starting from left to the right part of the string Example: *:INFO,p2p:ERROR,*:DEBUG,data:INFO will result in having the data package logger(s) on INFO log level and all other packages on DEBUG level

func SubscribeToProfileChange

func SubscribeToProfileChange(observer ProfileChangeObserver)

SubscribeToProfileChange subscribes an observer

func ToHex

func ToHex(slice []byte) string

ToHex converts the provided byte slice to its hex representation

func ToHexShort

func ToHexShort(slice []byte) string

ToHexShort generates a short-hand of provided bytes slice showing only the first 3 and the last 3 bytes as hex in total, the resulting string is maximum 13 characters long

func ToggleCorrelation

func ToggleCorrelation(enable bool)

ToggleCorrelation enables or disables correlation elements for log lines

func ToggleLoggerName

func ToggleLoggerName(enable bool)

ToggleLoggerName enables / disables logger name

func UnsubscribeFromProfileChange

func UnsubscribeFromProfileChange(observer ProfileChangeObserver)

UnsubscribeFromProfileChange unsubscribes an observer

Types

type ConsoleFormatter

type ConsoleFormatter struct {
}

ConsoleFormatter implements formatter interface and is used to format log lines to be written on the console It uses ANSI-color for colorized console/terminal output.

func (*ConsoleFormatter) IsInterfaceNil

func (cf *ConsoleFormatter) IsInterfaceNil() bool

IsInterfaceNil returns true if there is no value under the interface

func (*ConsoleFormatter) Output

func (cf *ConsoleFormatter) Output(line LogLineHandler) []byte

Output converts the provided LogLineHandler into a slice of bytes ready for output

type Formatter

type Formatter interface {
	Output(line LogLineHandler) []byte
	IsInterfaceNil() bool
}

Formatter describes what a log formatter should be able to do

type LogLevel

type LogLevel byte

LogLevel defines the priority level of a log line. Trace is the lowest priority level, Error is the highest

const (
	LogTrace   LogLevel = 0
	LogDebug   LogLevel = 1
	LogInfo    LogLevel = 2
	LogWarning LogLevel = 3
	LogError   LogLevel = 4
	LogNone    LogLevel = 5
)

These constants are the string representation of the package logging levels.

func GetLogLevel

func GetLogLevel(logLevelAsString string) (LogLevel, error)

GetLogLevel gets the corresponding log level from provided string. The search is case insensitive.

func GetLoggerLogLevel

func GetLoggerLogLevel(loggerName string) LogLevel

GetLoggerLogLevel gets the log level of the specified logger

func ParseLogLevelAndMatchingString

func ParseLogLevelAndMatchingString(logLevelAndPatterns string) ([]LogLevel, []string, error)

ParseLogLevelAndMatchingString can parse a string in the form "MATCHING_STRING1:LOG_LEVEL1,MATCHING_STRING2:LOG_LEVEL2" into its corresponding log level and matching string. Errors if something goes wrong. For example, having the parameter "DEBUG|process" will set the DEBUG level on all loggers that will contain the "process" string in their name ("process/sync", "process/interceptors", "process" and so on). The rules are applied in the exact manner as they are provided, starting from left to the right part of the string Example: *:INFO,p2p:ERROR,*:DEBUG,data:INFO will result in having the data package logger(s) on INFO log level and all other packages on DEBUG level

func (LogLevel) String

func (level LogLevel) String() string

type LogLine

type LogLine struct {
	LoggerName  string
	Correlation proto.LogCorrelationMessage
	Message     string
	LogLevel    LogLevel
	Args        []interface{}
	Timestamp   time.Time
}

LogLine is the structure used to hold a log line

type LogLineHandler

type LogLineHandler interface {
	GetLoggerName() string
	GetCorrelation() proto.LogCorrelationMessage
	GetMessage() string
	GetLogLevel() int32
	GetArgs() []string
	GetTimestamp() int64
	IsInterfaceNil() bool
}

LogLineHandler defines the get methods for a log line struct used by the formatter interface

type LogLineWrapper

type LogLineWrapper struct {
	proto.LogLineMessage
}

LogLineWrapper is a wrapper over protobuf.LogLineMessage that enables the structure to be used with protobuf marshaller

func (*LogLineWrapper) IsInterfaceNil

func (llw *LogLineWrapper) IsInterfaceNil() bool

IsInterfaceNil returns true if there is no value under the interface

type LogOutputHandler

type LogOutputHandler interface {
	Output(line *LogLine)
	AddObserver(w io.Writer, format Formatter) error
	RemoveObserver(w io.Writer) error
	ClearObservers()
	IsInterfaceNil() bool
}

LogOutputHandler defines the properties of a subject-observer component able to output log lines

func GetLogOutputSubject

func GetLogOutputSubject() LogOutputHandler

GetLogOutputSubject returns the default log output subject

type Logger

type Logger interface {
	Trace(message string, args ...interface{})
	Debug(message string, args ...interface{})
	Info(message string, args ...interface{})
	Warn(message string, args ...interface{})
	Error(message string, args ...interface{})
	LogIfError(err error, args ...interface{})
	Log(logLevel LogLevel, message string, args ...interface{})
	LogLine(line *LogLine)
	SetLevel(logLevel LogLevel)
	GetLevel() LogLevel
	IsInterfaceNil() bool
}

Logger defines the behavior of a data logger component

type Marshalizer

type Marshalizer interface {
	Marshal(obj interface{}) ([]byte, error)
	Unmarshal(obj interface{}, buff []byte) error
	IsInterfaceNil() bool
}

Marshalizer defines the 2 basic operations: serialize (marshal) and deserialize (unmarshal)

type PlainFormatter

type PlainFormatter struct {
}

PlainFormatter implements formatter interface and is used to format log lines to be written in the same form as ConsoleFormatter but it doesn't use the ANSI colors (useful when writing to a file, for example)

func (*PlainFormatter) IsInterfaceNil

func (pf *PlainFormatter) IsInterfaceNil() bool

IsInterfaceNil returns true if there is no value under the interface

func (*PlainFormatter) Output

func (pf *PlainFormatter) Output(line LogLineHandler) []byte

Output converts the provided LogLineHandler into a slice of bytes ready for output

type Profile

type Profile struct {
	LogLevelPatterns string
	WithCorrelation  bool
	WithLoggerName   bool
}

Profile holds global logger options

func GetCurrentProfile

func GetCurrentProfile() Profile

GetCurrentProfile gets the current logger profile

func UnmarshalProfile

func UnmarshalProfile(data []byte) (Profile, error)

UnmarshalProfile deserializes into a Profile object

func (*Profile) Apply

func (profile *Profile) Apply() error

Apply sets the global logger options

func (*Profile) Marshal

func (profile *Profile) Marshal() ([]byte, error)

Marshal serializes the Profile object

func (*Profile) String

func (profile *Profile) String() string

type ProfileChangeObserver

type ProfileChangeObserver interface {
	OnProfileChanged()
}

ProfileChangeObserver defines the interface for observing profile changes

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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