log

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	None  = 0
	Fatal = 1
	Error = 2
	Warn  = 3
	Info  = 4
	Debug = 5
	Trace = 6
)

Standard log levels.

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

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.

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 NullLoggerDescriptor = refer.NewDescriptor("pip-services", "logger", "null", "*", "1.0")

Creates ILogger components by their descriptors.

Functions

func LogLevelFromString

func LogLevelFromString(value interface{}) int

Converts log level to a LogLevel. see LogLevel Parameters:

  • value interface{} a log level string to convert

Returns int log level value.

func LogLevelToString

func LogLevelToString(level int) string

Converts log level to a string. see LogLevel Parameters:

  • level int a log level to convert

Returns string log level name string.

func NewDefaultLoggerFactory

func NewDefaultLoggerFactory() *build.Factory

Create a new instance of the factory. Returns *build.Factory

Types

type CachedLogger

type CachedLogger struct {
	Logger
	// contains filtered or unexported fields
}

func InheritCachedLogger

func InheritCachedLogger(saver ICachedLogSaver) *CachedLogger

Creates a new instance of the logger from ICachedLogSaver Parameters:

  • saver ICachedLogSaver

Returns CachedLogger

func (*CachedLogger) Clear

func (c *CachedLogger) Clear()

Clears (removes) all cached log messages.

func (*CachedLogger) Configure

func (c *CachedLogger) Configure(cfg *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *config.ConfigParams configuration parameters to be set.

func (*CachedLogger) Dump

func (c *CachedLogger) Dump() error

Dumps (writes) the currently cached log messages.

func (*CachedLogger) Update

func (c *CachedLogger) Update()

Makes message cache as updated and dumps it when timeout expires.

func (*CachedLogger) Write

func (c *CachedLogger) Write(level int, correlationId string, err error, message string)

Writes a log message to the logger destination. Parameters:

  • 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
}

Aggregates all loggers from component references under a single component.

It allows to log 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(config: ConfigParams): void {
    mc._logger.Configure(config);
    ...
}

func (mc* MyComponent) SetReferences(references: IReferences): void {
    mc._logger.SetReferences(references);
    ...
}

func (mc* MyComponent)myMethod(string correlationId): void {
    mc._logger.Debug(correlationId, "Called method mycomponent.mymethod");
    ...
}

var mc MyComponent = MyComponent{} mc._logger = NewCompositeLogger();

func NewCompositeLogger

func NewCompositeLogger() *CompositeLogger

Creates a new instance of the logger. Returns *CompositeLogger

func NewCompositeLoggerFromReferences

func NewCompositeLoggerFromReferences(references refer.IReferences) *CompositeLogger

Creates a new instance of the logger. Parameters:

  • references refer.IReferences references to locate the component dependencies.

Returns CompositeLogger

func (*CompositeLogger) SetReferences

func (c *CompositeLogger) SetReferences(references refer.IReferences)

Sets references to dependent components. Parameters:

  • references IReferences references to locate the component dependencies.

func (*CompositeLogger) Write

func (c *CompositeLogger) Write(level int, correlationId string, err error, message string)

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

  • level int 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
}

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("123", ex, "Error occured: %s", ex.message); logger.Debug("123", "Everything is OK.");

func NewConsoleLogger

func NewConsoleLogger() *ConsoleLogger

Creates a new instance of the logger. Returns ConsoleLogger

func (*ConsoleLogger) Write

func (c *ConsoleLogger) Write(level int, correlationId string, err error, message string)

Writes a log message to the logger destination. Parameters:

  • level int 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(messages []*LogMessage) error
}

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 ILogWriter

type ILogWriter interface {
	Write(level int, correlationId string, err error, message string)
}

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

Configuration parameters 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 ILogger

type ILogger interface {

	// Gets the maximum log level. Messages with higher log level are filtered out.
	Level() int

	// Set the maximum log level.
	SetLevel(value int)

	// Logs a message at specified log level.
	Log(level int, correlationId string, err error, message string, args ...interface{})

	// Logs fatal (unrecoverable) message that caused the process to crash.
	Fatal(correlationId string, err error, message string, args ...interface{})

	// Logs recoverable application error.
	Error(correlationId string, err error, message string, args ...interface{})
	// Logs a warning that may or may not have a negative impact.
	Warn(correlationId string, message string, args ...interface{})

	// Logs an important information message
	Info(correlationId string, message string, args ...interface{})

	// Logs a high-level debug information for troubleshooting.
	Debug(correlationId string, message string, args ...interface{})

	// Logs a low-level debug information for troubleshooting.
	Trace(correlationId string, message string, args ...interface{})
}

Interface for logger components that capture execution log messages.

type LogMessage

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

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

func NewLogMessage

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

Create new log message object Parameters:

  • level int an log level
  • source string an source
  • correletionId 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 {
	// contains filtered or unexported fields
}

func InheritLogger

func InheritLogger(writer ILogWriter) *Logger

Creates a new instance of the logger and inherite from ILogerWriter. Parameters:

  • writer ILogWriter inherite from

Returns *Logger

func (*Logger) ComposeError

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

Composes an human-readable error description Parameters:

  • err error an error to format.

Returns string a human-reable error description.

func (*Logger) Configure

func (c *Logger) Configure(cfg *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config ConfigParams configuration parameters to be set.

func (*Logger) Debug

func (c *Logger) Debug(correlationId string, message string, args ...interface{})

Logs a high-level debug information for troubleshooting. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

func (*Logger) Error

func (c *Logger) Error(correlationId string, err error, message string, args ...interface{})

Logs recoverable application error. Parameters:

  • 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 ...interface{} arguments to parameterize the message.

func (*Logger) Fatal

func (c *Logger) Fatal(correlationId string, err error, message string, args ...interface{})

Logs fatal (unrecoverable) message that caused the process to crash. Parameters:

  • 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 ...interface{} arguments to parameterize the message.

func (*Logger) FormatAndWrite

func (c *Logger) FormatAndWrite(level int, correlationId string, err error, message string, args []interface{})

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

  • level int 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 []interface{} arguments to parameterize the message.

func (*Logger) Info

func (c *Logger) Info(correlationId string, message string, args ...interface{})

Logs an important information message Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

func (*Logger) Level

func (c *Logger) Level() int

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(level int, correlationId string, err error, message string, args ...interface{})

Logs a message at specified log level. Parameters:

  • level int 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 ...interface{} arguments to parameterize the message.

func (*Logger) SetLevel

func (c *Logger) SetLevel(value int)

Set the maximum log level. Parameters:

  • value int

a new maximum log level.

func (*Logger) SetReferences

func (c *Logger) SetReferences(references refer.IReferences)

Sets references to dependent components. Parameters:

  • references IReferences references to locate the component dependencies.

func (*Logger) SetSource

func (c *Logger) SetSource(value string)

Sets the source (context) name. Parameters:

  • value string a new source (context) name.

func (*Logger) Source

func (c *Logger) Source() string

Gets the source (context) name. Returns string the source (context) name.

func (*Logger) Trace

func (c *Logger) Trace(correlationId string, message string, args ...interface{})

Logs a low-level debug information for troubleshooting. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

func (*Logger) Warn

func (c *Logger) Warn(correlationId string, message string, args ...interface{})

Logs a warning that may or may not have a negative impact. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

type NullLogger

type NullLogger struct{}

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

Creates a new instance of the logger. Returns *NullLogger

func (*NullLogger) Debug

func (c *NullLogger) Debug(correlationId string, message string, args ...interface{})

Logs a high-level debug information for troubleshooting. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

func (*NullLogger) Error

func (c *NullLogger) Error(correlationId string, err error, message string, args ...interface{})

Logs recoverable application error. Parameters:

  • 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 ...interface{} arguments to parameterize the message.

func (*NullLogger) Fatal

func (c *NullLogger) Fatal(correlationId string, err error, message string, args ...interface{})

Logs fatal (unrecoverable) message that caused the process to crash. Parameters:

  • 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 ...interface{} arguments to parameterize the message.

func (*NullLogger) Info

func (c *NullLogger) Info(correlationId string, message string, args ...interface{})

Logs an important information message Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

func (*NullLogger) Level

func (c *NullLogger) Level() int

Gets the maximum log level. Messages with higher log level are filtered out. Returns int the maximum log level.

func (*NullLogger) Log

func (c *NullLogger) Log(level int, correlationId string, err error, message string, args ...interface{})

Logs a message at specified log level. Parameters:

  • level int 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 ...interface{} arguments to parameterize the message.

func (*NullLogger) SetLevel

func (c *NullLogger) SetLevel(value int)

Set the maximum log level. Parameters:

  • value int

a new maximum log level.

func (*NullLogger) Trace

func (c *NullLogger) Trace(correlationId string, message string, args ...interface{})

Logs a low-level debug information for troubleshooting. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

func (*NullLogger) Warn

func (c *NullLogger) Warn(correlationId string, message string, args ...interface{})

Logs a warning that may or may not have a negative impact. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • message string a human-readable message to log.
  • args ...interface{} arguments to parameterize the message

type TLogLevelConverter

type TLogLevelConverter struct{}

Helper class to convert log level values.

var LogLevelConverter *TLogLevelConverter = &TLogLevelConverter{}

func (*TLogLevelConverter) ToLogLevel

func (c *TLogLevelConverter) ToLogLevel(value interface{}) int

Converts numbers and strings to standard log level values. Parameters:

  • value interface{} a value to be converted

Returns int converted log level

func (*TLogLevelConverter) ToString

func (c *TLogLevelConverter) ToString(level int) string

Converts log level to a string. see LogLevel Parameters:

  • level int a log level to convert

Returns string log level name string.

Jump to

Keyboard shortcuts

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