logger

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: Apache-2.0 Imports: 2 Imported by: 8

Documentation

Overview

Package logger contains logging related API.

It is possible to use a custom logger, but the builtin logger is used if one is not provided.

Log levels for the builtin logger are described with type Level. Following log levels are supported:

  • OffLevel : Do not log anything.
  • FatalLevel : A critical error occured, the client cannot continue running.
  • ErrorLevel : A severe error occured.
  • WarnLevel : There is a problem, but the client can continue running.
  • InfoLevel : Informational message.
  • DebugLevel : A log message which can help with diagnosing a problem. Should not be used in production.
  • TraceLevel : Potentially very detailed log message, which is usually logged when an important function is called. Should not be used in production.

Lower levels imply higher levels. For example, if you want to see logs of level WarnLevel or more critical (ErrorLevel, FatalLevel), you need to set the log level as WarnLevel.

config := hazelcast.Config{}
config.Logger.Level = logger.WarnLevel

Using a Custom Logger

You can provide a custom logger that implements logger.Logger:

type Logger interface {
	Log(weight Weight, f func() string)
}

weight is the numeric value that correspond to the log level. E.g., WeightInfo corresponds to InfoLevel. f is a function which produces and returns the log message. If it is not called, there is no performance penalty related to log production.

The Log method is called whenever the client needs to log. It's the custom logger's responsibility to decide whether to call f and log a message or avoid calling it to discard the message. Most custom loggers would have a log filtering logic in their Log methods similar to the one below:

func (c MyCustomLogger) Log(wantWeight logger.Weight, formatter func() string) {
	// Do not log if this is a more detailed log message than configured.
	if c.weight < wantWeight {
		return
	}
	// ...
}

In order to activate your custom logger, use config.Logger.CustomLogger configuration:

config := hazelcast.Config{}
config.Logger.CustomLogger = MyCustomLogger{}

See the example for a detailed custom logger implementation.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"strings"

	"github.com/hazelcast/hazelcast-go-client"
	"github.com/hazelcast/hazelcast-go-client/logger"
)

type CustomLogger struct{}

func (cl *CustomLogger) Log(weight logger.Weight, f func() string) {
	var logLevel logger.Level
	switch weight {
	case logger.WeightTrace:
		logLevel = logger.TraceLevel
	case logger.WeightDebug:
		logLevel = logger.DebugLevel
	case logger.WeightInfo:
		logLevel = logger.InfoLevel
	case logger.WeightWarn:
		logLevel = logger.WarnLevel
	case logger.WeightError:
		logLevel = logger.ErrorLevel
	case logger.WeightFatal:
		logLevel = logger.FatalLevel
	case logger.WeightOff:
		logLevel = logger.OffLevel
	default:
		return // unknown level, do not log anything.
	}

	s := fmt.Sprintf("[CustomApp %s]: %s", strings.ToUpper(logLevel.String()), f())
	log.Println(s)
}

func main() {
	// Create the configuration.
	config := hazelcast.Config{}
	// Set attributes as fit.
	config.Logger.CustomLogger = &CustomLogger{}
	// Start the client with the configuration provider.
	ctx := context.TODO()
	_, err := hazelcast.StartNewClientWithConfig(ctx, config)
	if err != nil {
		log.Fatal(err)
	}
	// Use created client on your application with custom logger.
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// CustomLogger is used to set a custom logger.
	// The configuration in this section does not apply to the custom logger.
	// The custom logger should handle its own log filtering.
	CustomLogger Logger `json:"-"`
	// Level is the log level for the builtin logger.
	Level Level `json:",omitempty"`
}

Config is the logging configuration. Using a CustomLogger and specifying a Level is not allowed.

func (Config) Clone

func (c Config) Clone() Config

Clone returns a copy of the logger configuration.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks the logger configuration for problems and updates it with default values.

type Level

type Level string

Level is the importance of a log message. It is used by the builtin logger. It can also be used by custom loggers.

const (
	// OffLevel disables logging.
	OffLevel Level = "off"
	// FatalLevel is for critical errors which halt the client.
	FatalLevel Level = "fatal"
	// ErrorLevel is for severe errors.
	ErrorLevel Level = "error"
	// WarnLevel is for noting problems.
	// The client can continue running.
	WarnLevel Level = "warn"
	// InfoLevel is for informational messages.
	InfoLevel Level = "info"
	// DebugLevel is for logs messages which can help with diagnosing a problem.
	// Should not be used in production.
	DebugLevel Level = "debug"
	// TraceLevel is for potentially very detailed log messages, which are usually logged when an important function is called.
	// Should not be used in production.
	TraceLevel Level = "trace"
)

func (Level) String

func (l Level) String() string

String converts a Level to a string.

type Logger added in v1.2.0

type Logger interface {
	Log(weight Weight, f func() string)
}

Logger interface is used to provide a custom logger to the client. weight of type Weight, describes the log level and f function returns the message to be logged when called.

type Weight added in v1.2.0

type Weight int

Weight is the importance of a log message, and used with custom loggers. Lower weights are more important than higher weights.

const (
	// WeightOff means: do not log anything.
	WeightOff Weight = 0
	// WeightFatal is for critical errors which halt the client.
	WeightFatal Weight = 100
	// WeightError is for severe errors.
	WeightError Weight = 200
	// WeightWarn is for noting problems.
	// The client can continue running.
	WeightWarn Weight = 300
	// WeightInfo is for informational messages.
	WeightInfo Weight = 400
	// WeightDebug is for logs messages which can help with diagnosing a problem.
	WeightDebug Weight = 500
	// WeightTrace is for potentially very detailed log messages, which are usually logged when an important function is called.
	// Should not be used in production.
	WeightTrace Weight = 600
)

func WeightForLogLevel added in v1.2.0

func WeightForLogLevel(logLevel Level) (Weight, error)

WeightForLogLevel returns the corresponding logger Weight with the given string if it exists, otherwise returns an error.

Jump to

Keyboard shortcuts

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