logging

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

The logging package ...

Index

Examples

Constants

View Source
const (
	LevelTraceInt int = -8
	LevelDebugInt int = -4
	LevelInfoInt  int = 0
	LevelWarnInt  int = 4
	LevelErrorInt int = 8
	LevelFatalInt int = 12
	LevelPanicInt int = 16
)

Log levels as integers. Compatible with golang.org/x/exp/slog.

View Source
const (
	LevelDebugName = "DEBUG"
	LevelErrorName = "ERROR"
	LevelFatalName = "FATAL"
	LevelInfoName  = "INFO"
	LevelPanicName = "PANIC"
	LevelTraceName = "TRACE"
	LevelWarnName  = "WARN"
)

Strings representing the supported logging levels.

View Source
const (
	LevelDebugSlog = slog.LevelDebug
	LevelErrorSlog = slog.LevelError
	LevelFatalSlog = slog.Level(LevelFatalInt)
	LevelInfoSlog  = slog.LevelInfo
	LevelPanicSlog = slog.Level(LevelPanicInt)
	LevelTraceSlog = slog.Level(LevelTraceInt)
	LevelWarnSlog  = slog.LevelWarn
)

Existing and new log levels used with slog.Level.

Variables

View Source
var IdLevelRangesAsString = map[int]string{
	0000: LevelTraceName,
	1000: LevelDebugName,
	2000: LevelInfoName,
	3000: LevelWarnName,
	4000: LevelErrorName,
	5000: LevelFatalName,
	6000: LevelPanicName,
}

Message ID Low-bound for message levels i.e. a message in range 0 - 999 is a TRACE message.

Map from slog.Level to string representation.

Map from string representation to Log level as typed integer.

Map from string representation to Log level as typed integer. FIXME: Deprecated: Only needed until g2-sdk-go-* SetLevel() methods have been updated

Functions

func IsValidLogLevelName

func IsValidLogLevelName(logLevelName string) bool

The IsValidLogLevelName function checks the logLevelName to verify it is one of "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", or "PANIC".

Input

  • logLevelName: A name to be tested.

Output

  • boolean: True if name in "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", or "PANIC".

func SlogHandlerOptions

func SlogHandlerOptions(leveler slog.Leveler, options ...interface{}) *slog.HandlerOptions

The SlogHandlerOptions function returns a slog handler that includes TRACE, FATAL, and PANIC. See: https://go.googlesource.com/exp/+/refs/heads/master/slog/example_custom_levels_test.go

Types

type LoggingImpl

type LoggingImpl struct {
	Ctx context.Context // Not a preferred practice, but used to simplify Log() calls.
	// contains filtered or unexported fields
}

loggingImpl is an type-struct for an implementation of the loggingInterface.

func (*LoggingImpl) GetLogLevel

func (loggingImpl *LoggingImpl) GetLogLevel() string

The GetLogLevel method retrieves the current log level name.

Output

  • One of the following string values: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", or "PANIC"

func (*LoggingImpl) Is

func (loggingImpl *LoggingImpl) Is(logLevelName string) bool

The Is method is used to determine if a log message will be printed.

Output

  • True, if message would be logged at the logLevelName level.

func (*LoggingImpl) IsDebug

func (loggingImpl *LoggingImpl) IsDebug() bool

The IsDebug method is used to determine if DEBUG messages will be logged.

Output

  • If true, DEBUG, INFO, WARN, ERROR, FATAL, and PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("DEBUG")
if logger.IsTrace() {
	fmt.Println("TRACE active")
}
if logger.IsDebug() {
	fmt.Println("DEBUG active")
}
if logger.IsInfo() {
	fmt.Println("INFO active")
}
Output:
DEBUG active
INFO active

func (*LoggingImpl) IsError

func (loggingImpl *LoggingImpl) IsError() bool

The IsError method is used to determine if ERROR messages will be logged.

Output

  • If true, ERROR, FATAL, and PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("ERROR")
if logger.IsWarn() {
	fmt.Println("WARN active")
}
if logger.IsError() {
	fmt.Println("ERROR active")
}
if logger.IsFatal() {
	fmt.Println("FATAL active")
}
Output:
ERROR active
FATAL active

func (*LoggingImpl) IsFatal

func (loggingImpl *LoggingImpl) IsFatal() bool

The IsFatal method is used to determine if FATAL messages will be logged.

Output

  • If true, FATAL and PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("FATAL")
if logger.IsError() {
	fmt.Println("ERROR active")
}
if logger.IsFatal() {
	fmt.Println("FATAL active")
}
if logger.IsPanic() {
	fmt.Println("PANIC active")
}
Output:
FATAL active
PANIC active

func (*LoggingImpl) IsInfo

func (loggingImpl *LoggingImpl) IsInfo() bool

The IsInfo method is used to determine if INFO messages will be logged.

Output

  • If true, INFO, WARN, ERROR, FATAL, and PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("INFO")
if logger.IsDebug() {
	fmt.Println("DEBUG active")
}
if logger.IsInfo() {
	fmt.Println("INFO active")
}
if logger.IsWarn() {
	fmt.Println("WARN active")
}
Output:
INFO active
WARN active

func (*LoggingImpl) IsPanic

func (loggingImpl *LoggingImpl) IsPanic() bool

The IsPanic method is used to determine if PANIC messages will be logged.

Output

  • If true, PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("PANIC")
if logger.IsFatal() {
	fmt.Println("FATAL active")
}
if logger.IsPanic() {
	fmt.Println("PANIC active")
}
Output:
PANIC active

func (*LoggingImpl) IsTrace

func (loggingImpl *LoggingImpl) IsTrace() bool

The IsTrace method is used to determine if TRACE messages will be logged.

Output

  • If true, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, and PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("TRACE")
if logger.IsTrace() {
	fmt.Println("TRACE active")
}
if logger.IsDebug() {
	fmt.Println("DEBUG active")
}
Output:
TRACE active
DEBUG active

func (*LoggingImpl) IsWarn

func (loggingImpl *LoggingImpl) IsWarn() bool

The IsWarn method is used to determine if WARN messages will be logged.

Output

  • If true, WARN, ERROR, FATAL, and PANIC messages will be logged.
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.SetLogLevel("WARN")
if logger.IsInfo() {
	fmt.Println("INFO active")
}
if logger.IsWarn() {
	fmt.Println("WARN active")
}
if logger.IsError() {
	fmt.Println("ERROR active")
}
Output:
WARN active
ERROR active

func (*LoggingImpl) Json

func (loggingImpl *LoggingImpl) Json(messageNumber int, details ...interface{}) string

The Json method returns a JSON string based on the messageNumber and details.

Input

  • messageNumber: A message identifier which indexes into "idMessages".
  • details: Variadic arguments of any type to be added to the message.

Output

  • JSON string with message key/value pairs.

func (*LoggingImpl) Log

func (loggingImpl *LoggingImpl) Log(messageNumber int, details ...interface{})

The Log method writes a log record to the output specified at LoggingImpl creation

Input

  • messageNumber: A message identifier which indexes into "idMessages".
  • details: Variadic arguments of any type to be added to the message.
Example (New)
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.Log(2001, "Bob", "Jane") // Note that 2000's are INFO messages.
Example (NewSenzingToolsLogger)
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
componentId := 9999
idMessages := map[int]string{
	2001: "%s works with %s",
}
logger, err := NewSenzingToolsLogger(componentId, idMessages)
if err != nil {
	fmt.Println(err)
}
logger.Log(2001, "Bob", "Jane") // Note that 2000's are INFO messages.

func (*LoggingImpl) NewError added in v1.2.5

func (loggingImpl *LoggingImpl) NewError(messageNumber int, details ...interface{}) error

The Error method returns an error with a JSON message based on the messageNumber and details.

Input

  • messageNumber: A message identifier which indexes into "idMessages".
  • details: Variadic arguments of any type to be added to the message.

Output

  • error

func (*LoggingImpl) SetLogLevel

func (loggingImpl *LoggingImpl) SetLogLevel(logLevelName string) error

The SetLogLevel method changes the level of log messages generated.

Input

  • logLevelName: One of these strings: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", or "PANIC".

Output

  • error

type LoggingInterface

type LoggingInterface interface {
	GetLogLevel() string                                      // Get the current level of logging.
	Is(logLevelName string) bool                              // Returns true if logLevelName message will be logged.
	IsDebug() bool                                            // Returns true if a DEBUG message will be logged.
	IsError() bool                                            // Returns true if an ERROR message will be logged.
	IsFatal() bool                                            // Returns true if a FATAL message will be logged.
	IsInfo() bool                                             // Returns true if an INFO message will be logged.
	IsPanic() bool                                            // Returns true if a PANIC message will be logged.
	IsTrace() bool                                            // Returns true if a TRACE message will be logged.
	IsWarn() bool                                             // Returns true if a WARN message will be logged.
	Json(messageNumber int, details ...interface{}) string    // Return a JSON string with the message.
	Log(messageNumber int, details ...interface{})            // Log the message.
	NewError(messageNumber int, details ...interface{}) error // Return an error object with the message.
	SetLogLevel(logLevelName string) error                    // Set the level of logging.
}

The loggingInterface interface has methods for creating different representations of a message.

func New

func New(options ...interface{}) (LoggingInterface, error)

The New function creates a new instance of loggingInterface. Adding options can be used to modify subcomponents.

Input

  • options: A list of options (usually having type OptionXxxxx) used to configure the logger.

Output

  • A logger
  • error
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
logger, err := New()
if err != nil {
	fmt.Println(err)
}
logger.Log(2001)

func NewSenzingLogger added in v1.3.0

func NewSenzingLogger(messageIdTemplate string, idMessages map[int]string, options ...interface{}) (LoggingInterface, error)

The NewSenzingLogger function creates a new instance of loggingInterface for the general use.

Input

  • idTemplate: A string with a "%04d" in it. Used to generate unique messages. Example: "my-id-%04d"
  • idMessage: A map of integer to string message templates.
  • options: Variadic arguments listing the options (usually having type OptionXxxxx) used to configure the logger.

Output

  • A logger
  • error

func NewSenzingSdkLogger

func NewSenzingSdkLogger(componentId int, idMessages map[int]string, options ...interface{}) (LoggingInterface, error)

The NewSenzingSdkLogger function creates a new instance of loggingInterface specifically for use with g2-sdk-go-* packages.

Input

Output

  • A logger
  • error

func NewSenzingToolsLogger

func NewSenzingToolsLogger(componentId int, idMessages map[int]string, options ...interface{}) (LoggingInterface, error)

The NewSenzingToolsLogger function creates a new instance of loggingInterface specifically for use with senzing-tools.

Input

Output

  • A logger
  • error
Example
// For more information, visit https://github.com/Senzing/go-logging/blob/main/logging/logging_test.go
componentId := 9999
idMessages := map[int]string{
	2001: "My message",
}
logger, err := NewSenzingToolsLogger(componentId, idMessages)
if err != nil {
	fmt.Println(err)
}
logger.Log(2001)

type MessageDetails

type MessageDetails struct {
	Value interface{}
}

type MessageDuration

type MessageDuration struct {
	Value int64
}

type MessageId

type MessageId struct {
	Value string
}

type MessageLevel

type MessageLevel struct {
	Value string
}

type MessageLocation

type MessageLocation struct {
	Value string
}

type MessageStatus

type MessageStatus struct {
	Value string
}

type MessageText

type MessageText struct {
	Value interface{}
}

type MessageTime

type MessageTime struct {
	Value time.Time
}

type OptionCallerSkip

type OptionCallerSkip struct {
	Value int
}

type OptionIdMessages

type OptionIdMessages struct {
	Value map[int]string
}

type OptionIdStatuses

type OptionIdStatuses struct {
	Value map[int]string
}

type OptionLogLevel

type OptionLogLevel struct {
	Value string
}

type OptionMessageIdTemplate

type OptionMessageIdTemplate struct {
	Value string
}

type OptionOutput

type OptionOutput struct {
	Value io.Writer
}

type OptionSenzingComponentId

type OptionSenzingComponentId struct {
	Value int
}

type OptionTimeHidden

type OptionTimeHidden struct {
	Value bool
}

Jump to

Keyboard shortcuts

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