log

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 13 Imported by: 46

Documentation

Index

Constants

View Source
const (
	DefaultMaxCacheSize                = 100
	DefaultInterval                    = 10000
	ConfigParameterOptionsInterval     = "options.interval"
	ConfigParameterOptionsMaxCacheSize = "options.max_cache_size"
)

Variables

View Source
var CompositeLoggerDescriptor = refer.NewDescriptor("pip-services", "logger", "composite", "*", "1.0")
View Source
var ConsoleLoggerDescriptor = refer.NewDescriptor("pip-services", "logger", "console", "*", "1.0")
View Source
var LevelConverter = &_TLogLevelConverter{}

LevelConverter Helper class to convert log level values.

View Source
var NullLoggerDescriptor = refer.NewDescriptor("pip-services", "logger", "null", "*", "1.0")

Functions

func NewDefaultLoggerFactory

func NewDefaultLoggerFactory() *build.Factory

NewDefaultLoggerFactory create a new instance of the factory.

Returns: *build.Factory

Types

type CachedLogger

type CachedLogger struct {
	Logger
	Cache        []LogMessage
	Updated      bool
	LastDumpTime time.Time
	MaxCacheSize int
	Interval     int

	Overrides ICachedLoggerOverrides
	// contains filtered or unexported fields
}

func InheritCachedLogger

func InheritCachedLogger(overrides ICachedLoggerOverrides) *CachedLogger

InheritCachedLogger creates a new instance of the logger from ICachedLogSaver

Parameters:
	- overrides ICachedLoggerOverrides
Returns: CachedLogger

func (*CachedLogger) Clear

func (c *CachedLogger) Clear(ctx context.Context)

Clear (removes) all cached log messages.

Parameters:
	- ctx context.Context

func (*CachedLogger) Configure

func (c *CachedLogger) Configure(ctx context.Context, cfg *config.ConfigParams)

Configure configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration parameters to be set.

func (*CachedLogger) Dump

func (c *CachedLogger) Dump(ctx context.Context) error

Dump (writes) the currently cached log messages.

Parameters:
	- ctx context.Context

func (*CachedLogger) Update

func (c *CachedLogger) Update(ctx context.Context)

Update makes message cache as updated and dumps it when timeout expires.

Parameters:
	- ctx context.Context

func (*CachedLogger) Write

func (c *CachedLogger) Write(ctx context.Context, level LevelType, correlationId string, err error, message string)

Writes a log message to the logger destination. Parameters:

  • ctx context.Context
  • level LogLevel a log level.
  • correlationId string transaction id to trace execution through call chain.
  • err error an error object associated with this message.
  • message string a human-readable message to log.

type CompositeLogger

type CompositeLogger struct {
	*Logger
	// contains filtered or unexported fields
}

CompositeLogger aggregates all loggers from component references under a single component. It allows logging messages and conveniently send them to multiple destinations.

References:
	- *:logger:*:*:1.0 (optional) ILogger components to pass log messages

see ILogger
Example:
	type MyComponent {
		_logger CompositeLogger
	}
	func (mc* MyComponent) Configure(ctx context.Context, config ConfigParams) {
		mc._logger.Configure(ctx, config)
		...
	}

	func (mc* MyComponent) SetReferences(ctx context.Context, references IReferences) {
		mc._logger.SetReferences(ctx, references)
		...
	}

	func (mc* MyComponent) myMethod(ctx context.Context, string correlationId) {
		mc._logger.Debug(ctx context.Context, correlationId, "Called method mycomponent.mymethod")
		...
	}
	var mc MyComponent = MyComponent{}
	mc._logger = NewCompositeLogger()

func NewCompositeLogger

func NewCompositeLogger() *CompositeLogger

NewCompositeLogger creates a new instance of the logger.

Returns: *CompositeLogger

func NewCompositeLoggerFromReferences

func NewCompositeLoggerFromReferences(ctx context.Context, references refer.IReferences) *CompositeLogger

NewCompositeLoggerFromReferences creates a new instance of the logger.

Parameters:
	- ctx context.Context
	- refer.IReferences references to locate the component dependencies.
Returns: CompositeLogger

func (*CompositeLogger) SetReferences

func (c *CompositeLogger) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences sets references to dependent components.

Parameters:
	- ctx context.Context
	- refer.IReferences references to locate the component dependencies.

func (*CompositeLogger) Write

func (c *CompositeLogger) Write(ctx context.Context, level LevelType, correlationId string, err error, message string)

Writes a log message to the logger destination(s). Parameters:

  • ctx context.Context
  • level LogLevel a log level.
  • correlationId string transaction id to trace execution through call chain.
  • err error an error object associated with this message.
  • message string a human-readable message to log.

type ConsoleLogger

type ConsoleLogger struct {
	*Logger
}

ConsoleLogger is a logger that writes log messages to console. Errors are written to standard err stream and all other messages to standard out stream.

Configuration parameters:
	- level: maximum log level to capture
	- source: source (context) name

References:
	- *:context-info:*:*:1.0 (optional) ContextInfo to detect the context id and specify counters source
see Logger

Example:
	logger = NewConsoleLogger()
	logger.SetLevel(LogLevel.Debug)
	logger.Error(context.Background(), "123", ex, "Error occured: %s", ex.message)
	logger.Debug(context.Background(), "123", "Everything is OK.")

func NewConsoleLogger

func NewConsoleLogger() *ConsoleLogger

NewConsoleLogger creates a new instance of the logger.

Returns: ConsoleLogger

func (*ConsoleLogger) Write

func (c *ConsoleLogger) Write(ctx context.Context, level LevelType, correlationId string, err error, message string)

Write a log message to the logger destination.

Parameters:
	- ctx context.Context
	- level LevelType a log level.
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.

type ICachedLogSaver

type ICachedLogSaver interface {
	Save(ctx context.Context, messages []LogMessage) error
}

ICachedLogSaver abstract logger that caches captured log messages in memory and periodically dumps them. Child classes implement saving cached messages to their specified destinations.

Configuration parameters
	- level: maximum log level to capture
	- source: source (context) name
	- options:
		- interval: interval in milliseconds to save log messages (default: 10 seconds)
		- max_cache_size: maximum number of messages stored in this cache (default: 100)
References:
	- *:context-info:*:*:1.0 (optional) ContextInfo to detect the context id and specify counters source

type ICachedLoggerOverrides added in v1.0.4

type ICachedLoggerOverrides interface {
	ILoggerOverrides
	Save(ctx context.Context, messages []LogMessage) error
}

type ILogger

type ILogger interface {

	// Level gets the maximum log level. Messages with higher log level are filtered out.
	Level() LevelType

	// SetLevel set the maximum log level.
	SetLevel(value LevelType)

	// Log logs a message at specified log level.
	Log(ctx context.Context, level LevelType, correlationId string, err error, message string, args ...any)

	// Fatal logs fatal (unrecoverable) message that caused the process to crash.
	Fatal(ctx context.Context, correlationId string, err error, message string, args ...any)

	// Error logs recoverable application error.
	Error(ctx context.Context, correlationId string, err error, message string, args ...any)

	// Warn logs a warning that may or may not have a negative impact.
	Warn(ctx context.Context, correlationId string, message string, args ...any)

	// Info logs an important information message
	Info(ctx context.Context, correlationId string, message string, args ...any)

	// Debug logs a high-level debug information for troubleshooting.
	Debug(ctx context.Context, correlationId string, message string, args ...any)

	// Trace logs a low-level debug information for troubleshooting.
	Trace(ctx context.Context, correlationId string, message string, args ...any)
}

ILogger for logger components that capture execution log messages.

type ILoggerOverrides added in v1.0.4

type ILoggerOverrides interface {
	Write(ctx context.Context, level LevelType, correlationId string, err error, message string)
}

ILoggerOverrides abstract logger that captures and formats log messages. Child classes take the captured messages and write them to their specific destinations.

Configuration parameters to pass to the configure method for component configuration:
	- level: maximum log level to capture
	- source: source (context) name
References:
	- *:context-info:*:*:1.0 (optional) ContextInfo to detect the context id and specify counters source

type LevelType

type LevelType uint8

LevelType is a type to represent log level names

const (
	LevelNone  LevelType = 0
	LevelFatal LevelType = 1
	LevelError LevelType = 2
	LevelWarn  LevelType = 3
	LevelInfo  LevelType = 4
	LevelDebug LevelType = 5
	LevelTrace LevelType = 6
)

Standard log levels. Logs at debug and trace levels are usually captured only locally for troubleshooting and never sent to consolidated log services.

Log levels:
	- None  = 0 Nothing to log
	- Fatal = 1 Log only fatal errors that cause processes to crash
	- Error = 2 Log all errors.
	- Warn  = 3 Log errors and warnings
	- Info  = 4 Log errors and important information messages
	- Debug = 5 Log everything except traces
	- Trace = 6 Log everything.

func (LevelType) MarshalJSON

func (l LevelType) MarshalJSON() ([]byte, error)

func (*LevelType) UnmarshalJSON

func (l *LevelType) UnmarshalJSON(data []byte) (err error)

type LogMessage

type LogMessage struct {
	Time          time.Time               `json:"time"`
	Source        string                  `json:"source"`
	Level         LevelType               `json:"level"`
	CorrelationId string                  `json:"correlation_id"`
	Error         errors.ErrorDescription `json:"error"`
	Message       string                  `json:"message"`
}

LogMessage Data object to store captured log messages. This object is used by CachedLogger.

func NewLogMessage

func NewLogMessage(level LevelType, source string, correlationId string,
	err errors.ErrorDescription, message string) LogMessage

NewLogMessage create new log message object

Parameters:
	- level LevelType a log level
	- source string a source
	- correlationId string transaction id to trace execution through call chain.
	- err errors.ErrorDescription an error object associated with this message.
	- message string a human-readable message to log.
Returns: LogMessage

type Logger

type Logger struct {
	Overrides ILoggerOverrides
	// contains filtered or unexported fields
}

func InheritLogger

func InheritLogger(overrides ILoggerOverrides) *Logger

InheritLogger creates a new instance of the logger and inherit from ILogWriter.

Parameters:
	- overrides ILoggerOverrides
Returns: *Logger

func (*Logger) ComposeError

func (c *Logger) ComposeError(err error) string

ComposeError composes an human-readable error description

Parameters:
	- err error an error to format.
Returns string a human-readable error description.

func (*Logger) Configure

func (c *Logger) Configure(ctx context.Context, cfg *config.ConfigParams)

Configure configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config ConfigParams configuration parameters to be set.

func (*Logger) Debug

func (c *Logger) Debug(ctx context.Context, correlationId string, message string, args ...any)

Debug logs a high-level debug information for troubleshooting.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*Logger) Error

func (c *Logger) Error(ctx context.Context, correlationId string, err error, message string, args ...any)

Logs recoverable application error.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*Logger) Fatal

func (c *Logger) Fatal(ctx context.Context, correlationId string, err error, message string, args ...any)

Fatal logs fatal (unrecoverable) message that caused the process to crash.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*Logger) FormatAndWrite

func (c *Logger) FormatAndWrite(ctx context.Context, level LevelType,
	correlationId string, err error, message string, args []any)

FormatAndWrite formats the log message and writes it to the logger destination. Parameters:

  • ctx context.Context
  • level LevelType a log level
  • correlationId: string transaction id to trace execution through call chain
  • err error an error object associated with this message
  • message string a human-readable message to log
  • args []any arguments to parameterize the message

func (*Logger) Info

func (c *Logger) Info(ctx context.Context, correlationId string, message string, args ...any)

Info logs an important information message

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*Logger) Level

func (c *Logger) Level() LevelType

Level gets the maximum log level. Messages with higher log level are filtered out.

Returns int the maximum log level.

func (*Logger) Log

func (c *Logger) Log(ctx context.Context, level LevelType, correlationId string, err error, message string, args ...any)

Log a message at specified log level.

Parameters:
	- ctx context.Context
	- level LevelType a log level.
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*Logger) SetLevel

func (c *Logger) SetLevel(value LevelType)

SetLevel set the maximum log level.

Parameters: value int a new maximum log level.

func (*Logger) SetReferences

func (c *Logger) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences to dependent components.

Parameters:
	- ctx context.Context
	- references IReferences references to locate the component dependencies.

func (*Logger) SetSource

func (c *Logger) SetSource(value string)

SetSource sets the source (context) name.

Parameters: value string a new source (context) name.

func (*Logger) Source

func (c *Logger) Source() string

Source gets the source (context) name.

Returns: string the source (context) name.

func (*Logger) Trace

func (c *Logger) Trace(ctx context.Context, correlationId string, message string, args ...any)

Trace logs a low-level debug information for troubleshooting.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*Logger) Warn

func (c *Logger) Warn(ctx context.Context, correlationId string, message string, args ...any)

Warn logs a warning that may or may not have a negative impact.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

type NullLogger

type NullLogger struct{}

NullLogger dummy implementation of logger that doesn't do anything. It can be used in testing or in situations when logger is required but shall be disabled.

func NewNullLogger

func NewNullLogger() *NullLogger

NewNullLogger creates a new instance of the logger.

Returns *NullLogger

func (*NullLogger) Debug

func (c *NullLogger) Debug(ctx context.Context, correlationId string, message string, args ...any)

Debug logs a high-level debug information for troubleshooting.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*NullLogger) Error

func (c *NullLogger) Error(ctx context.Context, correlationId string, err error, message string, args ...any)

Logs recoverable application error.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*NullLogger) Fatal

func (c *NullLogger) Fatal(ctx context.Context, correlationId string, err error, message string, args ...any)

Fatal logs fatal (unrecoverable) message that caused the process to crash.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*NullLogger) Info

func (c *NullLogger) Info(ctx context.Context, correlationId string, message string, args ...any)

Info logs an important information message

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*NullLogger) Level

func (c *NullLogger) Level() LevelType

Level gets the maximum log level. Messages with higher log level are filtered out.

Returns: LevelType the maximum log level.

func (*NullLogger) Log

func (c *NullLogger) Log(ctx context.Context, level LevelType, correlationId string, err error, message string, args ...any)

Log a message at specified log level.

Parameters:
	- ctx context.Context
	- level LevelType a log level.
	- correlationId string transaction id to trace execution through call chain.
	- err error an error object associated with this message.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*NullLogger) SetLevel

func (c *NullLogger) SetLevel(value LevelType)

SetLevel set the maximum log level.

Parameters:
	- value int a new maximum log level.

func (*NullLogger) Trace

func (c *NullLogger) Trace(ctx context.Context, correlationId string, message string, args ...any)

Trace logs a low-level debug information for troubleshooting.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

func (*NullLogger) Warn

func (c *NullLogger) Warn(ctx context.Context, correlationId string, message string, args ...any)

Warn logs a warning that may or may not have a negative impact.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- message string a human-readable message to log.
	- args ...any arguments to parameterize the message.

Jump to

Keyboard shortcuts

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