Documentation
¶
Overview ¶
The logging package ...
Index ¶
- Constants
- Variables
- func IsValidLogLevelName(logLevelName string) bool
- func SlogHandlerOptions(leveler slog.Leveler, options ...interface{}) *slog.HandlerOptions
- type LoggingImpl
- func (loggingImpl *LoggingImpl) GetLogLevel() string
- func (loggingImpl *LoggingImpl) Is(logLevelName string) bool
- func (loggingImpl *LoggingImpl) IsDebug() bool
- func (loggingImpl *LoggingImpl) IsError() bool
- func (loggingImpl *LoggingImpl) IsFatal() bool
- func (loggingImpl *LoggingImpl) IsInfo() bool
- func (loggingImpl *LoggingImpl) IsPanic() bool
- func (loggingImpl *LoggingImpl) IsTrace() bool
- func (loggingImpl *LoggingImpl) IsWarn() bool
- func (loggingImpl *LoggingImpl) Json(messageNumber int, details ...interface{}) string
- func (loggingImpl *LoggingImpl) Log(messageNumber int, details ...interface{})
- func (loggingImpl *LoggingImpl) NewError(messageNumber int, details ...interface{}) error
- func (loggingImpl *LoggingImpl) SetLogLevel(logLevelName string) error
- type LoggingInterface
- func New(options ...interface{}) (LoggingInterface, error)
- func NewSenzingLogger(messageIdTemplate string, idMessages map[int]string, options ...interface{}) (LoggingInterface, error)
- func NewSenzingSdkLogger(componentId int, idMessages map[int]string, options ...interface{}) (LoggingInterface, error)
- func NewSenzingToolsLogger(componentId int, idMessages map[int]string, options ...interface{}) (LoggingInterface, error)
- type MessageDetails
- type MessageDuration
- type MessageId
- type MessageLevel
- type MessageLocation
- type MessageStatus
- type MessageText
- type MessageTime
- type OptionCallerSkip
- type OptionIdMessages
- type OptionIdStatuses
- type OptionLogLevel
- type OptionMessageIdTemplate
- type OptionOutput
- type OptionSenzingComponentId
- type OptionTimeHidden
Examples ¶
Constants ¶
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.
const ( LevelDebugName = "DEBUG" LevelErrorName = "ERROR" LevelFatalName = "FATAL" LevelInfoName = "INFO" LevelPanicName = "PANIC" LevelTraceName = "TRACE" LevelWarnName = "WARN" )
Strings representing the supported logging levels.
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 ¶
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.
var LevelToTextMap = map[slog.Level]string{ LevelDebugSlog: LevelDebugName, LevelErrorSlog: LevelErrorName, LevelFatalSlog: LevelFatalName, LevelInfoSlog: LevelInfoName, LevelPanicSlog: LevelPanicName, LevelTraceSlog: LevelTraceName, LevelWarnSlog: LevelWarnName, }
Map from slog.Level to string representation.
var TextToLevelMap = map[string]slog.Level{ LevelDebugName: LevelDebugSlog, LevelErrorName: LevelErrorSlog, LevelFatalName: LevelFatalSlog, LevelInfoName: LevelInfoSlog, LevelPanicName: LevelPanicSlog, LevelTraceName: LevelTraceSlog, LevelWarnName: LevelWarnSlog, }
Map from string representation to Log level as typed integer.
var TextToLoggerLevelMap = map[string]logger.Level{ LevelTraceName: logger.LevelTrace, LevelDebugName: logger.LevelDebug, LevelInfoName: logger.LevelInfo, LevelWarnName: logger.LevelWarn, LevelErrorName: logger.LevelError, LevelFatalName: logger.LevelFatal, LevelPanicName: logger.LevelPanic, }
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 ¶
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
- componentId: See list at https://github.com/Senzing/knowledge-base/blob/main/lists/senzing-product-ids.md
- 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 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
- componentId: See list at https://github.com/Senzing/knowledge-base/blob/main/lists/senzing-product-ids.md
- 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
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 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 OptionCallerSkip ¶
type OptionCallerSkip struct {
Value int
}
type OptionIdMessages ¶
type OptionIdStatuses ¶
type OptionLogLevel ¶
type OptionLogLevel struct {
Value string
}
type OptionMessageIdTemplate ¶
type OptionMessageIdTemplate struct {
Value string
}
type OptionOutput ¶
type OptionSenzingComponentId ¶
type OptionSenzingComponentId struct {
Value int
}
type OptionTimeHidden ¶
type OptionTimeHidden struct {
Value bool
}