Documentation
¶
Overview ¶
Example (Migration) ¶
Example_migration shows how to gradually migrate from CustomLogger to ZerologLogger
package main
import (
"context"
"errors"
"github.com/carwale/golibraries/gologger"
)
// ExampleServiceLogger shows how to use the logger in a service
type ExampleService struct {
logger gologger.ILogger
name string
}
func NewExampleService(logger gologger.ILogger, name string) *ExampleService {
return &ExampleService{
logger: logger,
name: name,
}
}
func (s *ExampleService) ProcessRequest(ctx context.Context, userID string) error {
defer s.logger.Toc(s.logger.Tic("ProcessRequest"))
s.logger.LogInfoWithContext(ctx, "Processing request started")
if userID == "" {
err := errors.New("user ID cannot be empty")
s.logger.LogErrorWithContext(ctx, "Invalid request", err)
return err
}
s.logger.LogInfoMessage("Request processed successfully",
gologger.Pair{"service", s.name},
gologger.Pair{"user_id", userID},
)
return nil
}
func main() {
// Step 1: Extract logger creation into a function
createLogger := func(useZerolog bool) gologger.ILogger {
if useZerolog {
return gologger.NewZerologLogger(
gologger.WithLogLevel("INFO"),
)
}
return gologger.NewLogger(
gologger.SetLogLevel("INFO"),
gologger.DisableGraylog(true),
)
}
// Step 2: Use feature flag or environment variable to control logger type
useZerolog := false // This could come from config/environment
logger := createLogger(useZerolog)
// Step 3: Use the logger through the interface
service := NewExampleService(logger, "migration-example")
ctx := context.Background()
if err := service.ProcessRequest(ctx, "user123"); err != nil {
logger.LogError("Service request failed", err)
}
}
Example (Usage) ¶
Example_usage demonstrates how to use the interface pattern to switch between logger implementations
package main
import (
"context"
"errors"
"github.com/carwale/golibraries/gologger"
)
func main() {
// Method 1: Direct constructor calls (existing approach)
// Create CustomLogger (original implementation)
customLogger := gologger.NewLogger(
gologger.SetLogLevel("INFO"),
gologger.ConsolePrintEnabled(true),
gologger.DisableGraylog(true),
)
// Create ZerologLogger (new high-performance implementation)
zerologLogger := gologger.NewZerologLogger(
gologger.WithLogLevel("INFO"),
gologger.WithConsoleWriter(),
)
// Method 2: Using the factory pattern
factory := gologger.NewLoggerFactory()
// Create CustomLogger using factory
factoryCustomLogger := factory.CreateCustomLogger(
gologger.SetLogLevel("INFO"),
gologger.DisableGraylog(true),
)
// Create ZerologLogger using factory
factoryZerologLogger := factory.CreateZerologLogger(
gologger.WithLogLevel("INFO"),
gologger.WithConsoleWriter(),
)
// Method 3: Using unified configuration
config := gologger.LoggerConfig{
LogLevel: "INFO",
DisableGraylog: true,
ConsolePrintEnabled: true,
}
// Create either logger type with same config
unifiedCustomLogger := factory.CreateLogger(gologger.CustomLoggerType, config)
unifiedZerologLogger := factory.CreateLogger(gologger.ZerologLoggerType, config)
// Method 4: Using convenience functions
devCustomLogger := gologger.NewDevelopmentCustomLogger()
devZerologLogger := gologger.NewDevelopmentZerologLogger()
// All loggers implement the same interface
testLogger(customLogger)
testLogger(zerologLogger)
testLogger(factoryCustomLogger)
testLogger(factoryZerologLogger)
testLogger(unifiedCustomLogger)
testLogger(unifiedZerologLogger)
testLogger(devCustomLogger)
testLogger(devZerologLogger)
}
// testLogger demonstrates that both implementations work identically through the interface
func testLogger(logger gologger.ILogger) {
logger.LogInfo("This is an info message")
logger.LogWarning("This is a warning message")
logger.LogError("This is an error message", errors.New("sample error"))
logger.LogInfof("User %s logged in from IP %s", "john_doe", "192.168.1.1")
logger.LogErrorWithoutErrorf("Invalid request: status %d", 400)
logger.LogInfoMessage("User action completed",
gologger.Pair{"user_id", "12345"},
gologger.Pair{"action", "login"},
gologger.Pair{"duration_ms", "150"},
)
ctx := context.Background()
logger.LogInfoWithContext(ctx, "Processing request with trace context")
defer logger.Toc(logger.Tic("example_operation"))
if logger.GetLogLevel() >= gologger.INFO {
logger.LogDebug("Debug information")
}
}
Index ¶
- func SetCustomZerologLevelNames()
- type CounterMetric
- func (msg *CounterMetric) AddValue(count int64, labels ...string)
- func (msg *CounterMetric) RemoveLogging(labels ...string)
- func (msg *CounterMetric) SetValue(count int64, labels ...string)
- func (msg *CounterMetric) SubValue(count int64, labels ...string)
- func (msg *CounterMetric) UpdateTime(elapsed int64, labels ...string)
- type CustomLogger
- func (l *CustomLogger) GetLogLevel() LogLevels
- func (l *CustomLogger) LogDebug(str string)
- func (l *CustomLogger) LogDebugWithContext(ctx context.Context, str string)
- func (l *CustomLogger) LogDebugf(str string, args ...interface{})
- func (l *CustomLogger) LogDebugfWithContext(ctx context.Context, str string, args ...interface{})
- func (l *CustomLogger) LogError(str string, err error)
- func (l *CustomLogger) LogErrorInterface(v ...interface{})
- func (l *CustomLogger) LogErrorMessage(str string, err error, pairs ...Pair)
- func (l *CustomLogger) LogErrorWithContext(ctx context.Context, str string, err error)
- func (l *CustomLogger) LogErrorWithoutError(str string)
- func (l *CustomLogger) LogErrorWithoutErrorf(str string, args ...interface{})
- func (l *CustomLogger) LogErrorfWithContext(ctx context.Context, str string, err error, args ...interface{})
- func (l *CustomLogger) LogInfo(str string)
- func (l *CustomLogger) LogInfoMessage(str string, pairs ...Pair)
- func (l *CustomLogger) LogInfoWithContext(ctx context.Context, str string)
- func (l *CustomLogger) LogInfof(str string, args ...interface{})
- func (l *CustomLogger) LogInfofWithContext(ctx context.Context, str string, args ...interface{})
- func (l *CustomLogger) LogMessage(message string)
- func (l *CustomLogger) LogMessageWithExtras(message string, level LogLevels, pairs ...Pair)
- func (l *CustomLogger) LogMessagef(message string, args ...interface{})
- func (l *CustomLogger) LogWarning(str string)
- func (l *CustomLogger) LogWarningMessage(str string, pairs ...Pair)
- func (l *CustomLogger) LogWarningWithContext(ctx context.Context, str string)
- func (l *CustomLogger) LogWarningf(str string, args ...interface{})
- func (l *CustomLogger) LogWarningfWithContext(ctx context.Context, str string, args ...interface{})
- func (l *CustomLogger) Tic(s string) (string, time.Time)
- func (l *CustomLogger) Toc(message string, startTime time.Time)
- type GaugeMetric
- func (msg *GaugeMetric) AddValue(count int64, labels ...string)
- func (msg *GaugeMetric) RemoveLogging(labels ...string)
- func (msg *GaugeMetric) SetValue(count int64, labels ...string)
- func (msg *GaugeMetric) SubValue(count int64, labels ...string)
- func (msg *GaugeMetric) UpdateTime(elapsed int64, labels ...string)
- type HistogramMetric
- func (msg *HistogramMetric) AddValue(count int64, labels ...string)
- func (msg *HistogramMetric) RemoveLogging(labels ...string)
- func (msg *HistogramMetric) SetValue(count int64, labels ...string)
- func (msg *HistogramMetric) SubValue(count int64, labels ...string)
- func (msg *HistogramMetric) UpdateTime(elapsed int64, labels ...string)
- type ILogger
- func NewCustomLoggerWithDefaults() ILogger
- func NewDevelopmentCustomLogger() ILogger
- func NewDevelopmentZerologLogger() ILogger
- func NewProductionCustomLogger(graylogHost string, graylogPort int, facility string) ILogger
- func NewProductionZerologLogger(facility string) ILogger
- func NewZerologLoggerWithDefaults() ILogger
- type IMetricVec
- type IMultiLogger
- type LogLevels
- type LoggerConfig
- type LoggerFactory
- type LoggerType
- type Option
- func ConsolePrintEnabled(flag bool) Option
- func DisableGraylog(flag bool) Option
- func GraylogFacility(facility string) Option
- func GraylogHost(hostName string) Option
- func GraylogPort(portNumber int) Option
- func SetK8sNamespace(k8sNamespace string) Option
- func SetLogLevel(level string) Option
- func TimeLoggingEnabled(flag bool) Option
- type Pair
- type RateLatencyLogger
- func (mgl *RateLatencyLogger) AddNewMetric(messageIdentifier string, newMessage IMetricVec)
- func (mgl *RateLatencyLogger) IncVal(value int64, identifier string, labels ...string)
- func (mgl *RateLatencyLogger) SetVal(value int64, identifier string, labels ...string)
- func (mgl *RateLatencyLogger) SubVal(value int64, identifier string, labels ...string)
- func (mgl *RateLatencyLogger) Tic() time.Time
- func (mgl *RateLatencyLogger) Toc(start time.Time, identifier string, labels ...string)
- type RateLatencyOption
- type ZerologLogger
- func (l *ZerologLogger) GetLogLevel() LogLevels
- func (l *ZerologLogger) LogDebug(str string)
- func (l *ZerologLogger) LogDebugWithContext(ctx context.Context, str string)
- func (l *ZerologLogger) LogDebugf(str string, args ...interface{})
- func (l *ZerologLogger) LogDebugfWithContext(ctx context.Context, str string, args ...interface{})
- func (l *ZerologLogger) LogError(str string, err error)
- func (l *ZerologLogger) LogErrorInterface(v ...interface{})
- func (l *ZerologLogger) LogErrorMessage(str string, err error, pairs ...Pair)
- func (l *ZerologLogger) LogErrorWithContext(ctx context.Context, str string, err error)
- func (l *ZerologLogger) LogErrorWithoutError(str string)
- func (l *ZerologLogger) LogErrorWithoutErrorf(str string, args ...interface{})
- func (l *ZerologLogger) LogErrorfWithContext(ctx context.Context, str string, err error, args ...interface{})
- func (l *ZerologLogger) LogInfo(str string)
- func (l *ZerologLogger) LogInfoMessage(str string, pairs ...Pair)
- func (l *ZerologLogger) LogInfoWithContext(ctx context.Context, str string)
- func (l *ZerologLogger) LogInfof(str string, args ...interface{})
- func (l *ZerologLogger) LogInfofWithContext(ctx context.Context, str string, args ...interface{})
- func (l *ZerologLogger) LogMessage(message string)
- func (l *ZerologLogger) LogMessageWithExtras(message string, level LogLevels, pairs ...Pair)
- func (l *ZerologLogger) LogMessagef(message string, args ...interface{})
- func (l *ZerologLogger) LogWarning(str string)
- func (l *ZerologLogger) LogWarningMessage(str string, pairs ...Pair)
- func (l *ZerologLogger) LogWarningWithContext(ctx context.Context, str string)
- func (l *ZerologLogger) LogWarningf(str string, args ...interface{})
- func (l *ZerologLogger) LogWarningfWithContext(ctx context.Context, str string, args ...interface{})
- func (l *ZerologLogger) Tic(s string) (string, time.Time)
- func (l *ZerologLogger) Toc(message string, startTime time.Time)
- type ZerologOption
- func WithConsoleWriter() ZerologOption
- func WithFacility(facility string) ZerologOption
- func WithJSONConsole() ZerologOption
- func WithK8sNamespace(k8sNamespace string) ZerologOption
- func WithLogLevel(level string) ZerologOption
- func WithOutput(writer io.Writer) ZerologOption
- func WithStderr() ZerologOption
- func WithTimeLogging(enabled bool) ZerologOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetCustomZerologLevelNames ¶ added in v1.16.0
func SetCustomZerologLevelNames()
Types ¶
type CounterMetric ¶
type CounterMetric struct {
// contains filtered or unexported fields
}
CounterMetric : Default histogram message type implementing IMetricVec
func NewCounterMetric ¶
func NewCounterMetric(counter *prometheus.CounterVec, logger ILogger) *CounterMetric
NewCounterMetric creates a new histrogram message and registers it to prometheus
func (*CounterMetric) AddValue ¶
func (msg *CounterMetric) AddValue(count int64, labels ...string)
AddValue will increment the counter by value
func (*CounterMetric) RemoveLogging ¶
func (msg *CounterMetric) RemoveLogging(labels ...string)
RemoveLogging will stop logging for specific labels
func (*CounterMetric) SetValue ¶
func (msg *CounterMetric) SetValue(count int64, labels ...string)
SetValue will not do anything. It is not allowed in counters
func (*CounterMetric) SubValue ¶
func (msg *CounterMetric) SubValue(count int64, labels ...string)
SubValue will not do anything. It is not allowed in counters
func (*CounterMetric) UpdateTime ¶
func (msg *CounterMetric) UpdateTime(elapsed int64, labels ...string)
UpdateTime is a do nothing operation for counter metric
type CustomLogger ¶
type CustomLogger struct {
// contains filtered or unexported fields
}
CustomLogger is a graylog logger for golang
func NewLogger ¶
func NewLogger(LoggerOptions ...Option) *CustomLogger
NewLogger : returns a new logger. When no options are given, it returns an error logger With graylog logging as default to a port 11100 which is not in use. So it is prety much useless. Please provide graylog host and port at the very least.
func (*CustomLogger) GetLogLevel ¶
func (l *CustomLogger) GetLogLevel() LogLevels
GetLogLevel is used to get the current Log level
func (*CustomLogger) LogDebug ¶
func (l *CustomLogger) LogDebug(str string)
LogDebug is used to log debug messages
func (*CustomLogger) LogDebugWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogDebugWithContext(ctx context.Context, str string)
LogDebugWithContext is used to log debug messages. It will also add trace_id and span_id in the log if it exists in the context
func (*CustomLogger) LogDebugf ¶
func (l *CustomLogger) LogDebugf(str string, args ...interface{})
LogDebugf is used to log debug messages
func (*CustomLogger) LogDebugfWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogDebugfWithContext(ctx context.Context, str string, args ...interface{})
LogDebugfWithContext is used to log debug messages with any interface type. It will also add trace_id and span_id in the log if it exists in the context
func (*CustomLogger) LogError ¶
func (l *CustomLogger) LogError(str string, err error)
LogError is used to log errors and a message along with the error
func (*CustomLogger) LogErrorInterface ¶
func (l *CustomLogger) LogErrorInterface(v ...interface{})
LogErrorInterface is used to log errors
func (*CustomLogger) LogErrorMessage ¶
func (l *CustomLogger) LogErrorMessage(str string, err error, pairs ...Pair)
LogErrorMessage is used to log extra fields to graylog along with the error
func (*CustomLogger) LogErrorWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogErrorWithContext(ctx context.Context, str string, err error)
LogErrorWithContext is used to log errors and a message along with the error It will also add trace_id and span_id in the log if it exists in the context.
func (*CustomLogger) LogErrorWithoutError ¶
func (l *CustomLogger) LogErrorWithoutError(str string)
LogErrorWithoutError is used to log only a message and not an error
func (*CustomLogger) LogErrorWithoutErrorf ¶
func (l *CustomLogger) LogErrorWithoutErrorf(str string, args ...interface{})
LogErrorWithoutErrorf is used to log only a message and not an error
func (*CustomLogger) LogErrorfWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogErrorfWithContext(ctx context.Context, str string, err error, args ...interface{})
LogErrorfWithContext is used to log errors, a message and interface of any type along with the error It will also add trace_id and span_id in the log if it exists in the context.
func (*CustomLogger) LogInfo ¶
func (l *CustomLogger) LogInfo(str string)
LogInfo is used to log info messages
func (*CustomLogger) LogInfoMessage ¶
func (l *CustomLogger) LogInfoMessage(str string, pairs ...Pair)
LogInfoMessage is used to log extra fields to graylog
func (*CustomLogger) LogInfoWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogInfoWithContext(ctx context.Context, str string)
LogInfoWithContext is used to log info messages. It will also add trace_id and span_id in the log if it exists in the context.
func (*CustomLogger) LogInfof ¶
func (l *CustomLogger) LogInfof(str string, args ...interface{})
LogInfof is used to log formatted info messages
func (*CustomLogger) LogInfofWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogInfofWithContext(ctx context.Context, str string, args ...interface{})
LogInfofWithContext is used to log info messages with any interface type. It will also add trace_id and span_id in the log if it exists in the context.
func (*CustomLogger) LogMessage ¶
func (l *CustomLogger) LogMessage(message string)
LogMessage is used to log plain message
func (*CustomLogger) LogMessageWithExtras ¶
func (l *CustomLogger) LogMessageWithExtras(message string, level LogLevels, pairs ...Pair)
func (*CustomLogger) LogMessagef ¶
func (l *CustomLogger) LogMessagef(message string, args ...interface{})
LogMessagef is used to log plain message
func (*CustomLogger) LogWarning ¶
func (l *CustomLogger) LogWarning(str string)
LogWarning is used to log warning messages
func (*CustomLogger) LogWarningMessage ¶
func (l *CustomLogger) LogWarningMessage(str string, pairs ...Pair)
LogWarningMessage is used to log warning messages along with extra fields to GrayLog
func (*CustomLogger) LogWarningWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogWarningWithContext(ctx context.Context, str string)
LogWarningWithContext is used to log warning messages. It will also add trace_id and span_id in the log if it exists in the context.
func (*CustomLogger) LogWarningf ¶
func (l *CustomLogger) LogWarningf(str string, args ...interface{})
LogWarningf is used to log warning messages
func (*CustomLogger) LogWarningfWithContext ¶ added in v1.10.0
func (l *CustomLogger) LogWarningfWithContext(ctx context.Context, str string, args ...interface{})
LogWarningfWithContext is used to log warning messages with any interface type. It will also add trace_id and span_id in the log if it exists in the context.
func (*CustomLogger) Tic ¶
func (l *CustomLogger) Tic(s string) (string, time.Time)
Tic is used to log time taken by a function. It should be used along with Toc function Tic will take an input as a string message (It can be the name of the function) And will return time and the message. For full used see the Toc funtion
func (*CustomLogger) Toc ¶
func (l *CustomLogger) Toc(message string, startTime time.Time)
Toc will log the time taken by the funtion. Its input is the output of the Tic function Here is an example code block for using Tic and Toc function
defer Toc(Tic("FunctionName"))
This will the first line of the function
type GaugeMetric ¶
type GaugeMetric struct {
// contains filtered or unexported fields
}
GaugeMetric : Default Gauge message type implementing IMetricVec
func NewGaugeMetric ¶
func NewGaugeMetric(counter *prometheus.GaugeVec, logger ILogger) *GaugeMetric
NewGaugeMetric creates a new gauge message and registers it to prometheus
func (*GaugeMetric) AddValue ¶
func (msg *GaugeMetric) AddValue(count int64, labels ...string)
AddValue will increment the counter by value
func (*GaugeMetric) RemoveLogging ¶
func (msg *GaugeMetric) RemoveLogging(labels ...string)
RemoveLogging will stop logging for specific labels
func (*GaugeMetric) SetValue ¶
func (msg *GaugeMetric) SetValue(count int64, labels ...string)
SetValue will set the counter to that value
func (*GaugeMetric) SubValue ¶
func (msg *GaugeMetric) SubValue(count int64, labels ...string)
SubValue will decrement the counter by value
func (*GaugeMetric) UpdateTime ¶
func (msg *GaugeMetric) UpdateTime(elapsed int64, labels ...string)
UpdateTime is a do nothing operation for counter metric
type HistogramMetric ¶
type HistogramMetric struct {
// contains filtered or unexported fields
}
HistogramMetric : Default histogram message type implementing IMetricVec
func NewHistogramMetric ¶
func NewHistogramMetric(hist *prometheus.HistogramVec, logger ILogger) *HistogramMetric
NewHistogramMetric creates a new histrogram message and registers it to prometheus
func (*HistogramMetric) AddValue ¶
func (msg *HistogramMetric) AddValue(count int64, labels ...string)
AddValue is a do nothing function for histogram
func (*HistogramMetric) RemoveLogging ¶
func (msg *HistogramMetric) RemoveLogging(labels ...string)
RemoveLogging will stop logging for specific labels
func (*HistogramMetric) SetValue ¶
func (msg *HistogramMetric) SetValue(count int64, labels ...string)
SetValue is a do nothing function for histogram
func (*HistogramMetric) SubValue ¶
func (msg *HistogramMetric) SubValue(count int64, labels ...string)
SubValue is a do nothing function for histogram
func (*HistogramMetric) UpdateTime ¶
func (msg *HistogramMetric) UpdateTime(elapsed int64, labels ...string)
UpdateTime the message with calculated latency
type ILogger ¶ added in v1.16.0
type ILogger interface {
// Basic logging methods
LogError(str string, err error)
LogErrorWithoutError(str string)
LogErrorWithoutErrorf(str string, args ...interface{})
LogErrorMessage(str string, err error, pairs ...Pair)
LogWarning(str string)
LogWarningf(str string, args ...interface{})
LogWarningMessage(str string, pairs ...Pair)
LogInfo(str string)
LogInfof(str string, args ...interface{})
LogInfoMessage(str string, pairs ...Pair)
LogDebug(str string)
LogDebugf(str string, args ...interface{})
// Context-aware logging methods
LogDebugWithContext(ctx context.Context, str string)
LogDebugfWithContext(ctx context.Context, str string, args ...interface{})
LogInfoWithContext(ctx context.Context, str string)
LogInfofWithContext(ctx context.Context, str string, args ...interface{})
LogWarningWithContext(ctx context.Context, str string)
LogWarningfWithContext(ctx context.Context, str string, args ...interface{})
LogErrorWithContext(ctx context.Context, str string, err error)
LogErrorfWithContext(ctx context.Context, str string, err error, args ...interface{})
// Utility methods
LogMessage(message string)
LogMessagef(message string, args ...interface{})
LogMessageWithExtras(message string, level LogLevels, pairs ...Pair)
LogErrorInterface(v ...interface{})
// Time logging methods
Tic(s string) (string, time.Time)
Toc(message string, startTime time.Time)
// Configuration methods
GetLogLevel() LogLevels
}
ILogger defines the interface that all logger implementations must follow
func NewCustomLoggerWithDefaults ¶ added in v1.16.0
func NewCustomLoggerWithDefaults() ILogger
NewCustomLoggerWithDefaults creates a CustomLogger with sensible defaults
func NewDevelopmentCustomLogger ¶ added in v1.16.0
func NewDevelopmentCustomLogger() ILogger
NewDevelopmentCustomLogger creates a CustomLogger configured for development
func NewDevelopmentZerologLogger ¶ added in v1.16.0
func NewDevelopmentZerologLogger() ILogger
NewDevelopmentZerologLogger creates a ZerologLogger configured for development
func NewProductionCustomLogger ¶ added in v1.16.0
NewProductionCustomLogger creates a CustomLogger configured for production
func NewProductionZerologLogger ¶ added in v1.16.0
NewProductionZerologLogger creates a ZerologLogger configured for production
func NewZerologLoggerWithDefaults ¶ added in v1.16.0
func NewZerologLoggerWithDefaults() ILogger
NewZerologLoggerWithDefaults creates a ZerologLogger with sensible defaults
type IMetricVec ¶
type IMetricVec interface {
UpdateTime(int64, ...string)
//Method to count increments or gauges
AddValue(int64, ...string)
// Method to subtract from the counter
SubValue(int64, ...string)
// Method to set the counter
SetValue(int64, ...string)
// Method to Remove the label from the metric
RemoveLogging(...string)
}
IMetricVec : Interface to implement for Message type
type IMultiLogger ¶
type IMultiLogger interface {
// To measure time elapsed between any two points in the code,
// Start the time logger by Tic(MessageDesc) and end the time logger by calling Toc(MessageDesc,time)
Tic() time.Time
Toc(time.Time, string, ...string)
IncVal(int64, string, ...string)
SubVal(int64, string, ...string)
SetVal(int64, string, ...string)
AddNewMetric(string, IMetricVec)
}
IMultiLogger : Interface for Multi message logger
func NewRateLatencyLogger ¶
func NewRateLatencyLogger(options ...RateLatencyOption) IMultiLogger
NewRateLatencyLogger : returns a new RateLatencyLogger. When no options are given, it returns a RateLatencyLogger with default settings. Default logger is default custom logger.
type LogLevels ¶
type LogLevels uint32
LogLevels are the log levels for logging
const ( // ERROR : All errors will be logged with this level ERROR LogLevels = 0 // WARN : All important events should be logged with a warn WARN LogLevels = 1 // INFO : All events other than the important ones to be logged here INFO LogLevels = 2 // DEBUG : This is for debug purposes only. Never use it on staging and production DEBUG LogLevels = 3 )
type LoggerConfig ¶ added in v1.16.0
type LoggerConfig struct {
GraylogHost string // Only used for CustomLogger
GraylogPort int // Only used for CustomLogger
GraylogFacility string
K8sNamespace string
LogLevel string
DisableGraylog bool // Only used for CustomLogger
ConsolePrintEnabled bool
TimeLoggingEnabled bool
}
LoggerConfig holds common configuration for both logger types
type LoggerFactory ¶ added in v1.16.0
type LoggerFactory struct{}
LoggerFactory creates loggers based on the specified type
func NewLoggerFactory ¶ added in v1.16.0
func NewLoggerFactory() *LoggerFactory
NewLoggerFactory creates a new logger factory instance
func (*LoggerFactory) CreateCustomLogger ¶ added in v1.16.0
func (f *LoggerFactory) CreateCustomLogger(options ...Option) ILogger
CreateCustomLogger creates a CustomLogger with the given options
func (*LoggerFactory) CreateLogger ¶ added in v1.16.0
func (f *LoggerFactory) CreateLogger(loggerType LoggerType, config LoggerConfig) ILogger
CreateLogger creates a logger of the specified type with common configuration This provides a unified way to create either logger type with similar settings
func (*LoggerFactory) CreateZerologLogger ¶ added in v1.16.0
func (f *LoggerFactory) CreateZerologLogger(options ...ZerologOption) ILogger
CreateZerologLogger creates a ZerologLogger with the given options
type LoggerType ¶ added in v1.16.0
type LoggerType string
LoggerType defines the type of logger to create
const ( // CustomLoggerType creates the original CustomLogger CustomLoggerType LoggerType = "custom" // ZerologLoggerType creates the new ZerologLogger ZerologLoggerType LoggerType = "zerolog" )
type Option ¶
type Option func(l *CustomLogger)
Option sets a parameter for the Logger
func ConsolePrintEnabled ¶
ConsolePrintEnabled enables console output for logging. To be used only for development.
func DisableGraylog ¶
DisableGraylog disables graylog logging. Defaults to false If graylog is disabled console logging will be enabled by default
func GraylogFacility ¶
GraylogFacility sets the graylog facility for the logger. Default is "ErrorLogger"
func GraylogHost ¶
GraylogHost sets the graylog host for the logger. Default is 127.0.0.1
func GraylogPort ¶
GraylogPort sets the graylog port for the logger. Default is 11100
func SetK8sNamespace ¶ added in v1.1.0
K8sNamespace sets the graylog k8sNamespace for the logger. Default is "dev" This option will have no effect if env variable K8S_NAMESPACE is set
func SetLogLevel ¶
SetLogLevel sets the logger level Possible values are ERROR, WARN, INFO, DEBUG. Default is ERROR
func TimeLoggingEnabled ¶
TimeLoggingEnabled enables logging of time (The use of tic toc functions). This can be used in functions and then disabled here when there is no need.
type RateLatencyLogger ¶
type RateLatencyLogger struct {
// contains filtered or unexported fields
}
RateLatencyLogger : Logger that tracks multiple messages & prints to console
func (*RateLatencyLogger) AddNewMetric ¶
func (mgl *RateLatencyLogger) AddNewMetric(messageIdentifier string, newMessage IMetricVec)
AddNewMetric sets New message initialisation function
func (*RateLatencyLogger) IncVal ¶
func (mgl *RateLatencyLogger) IncVal(value int64, identifier string, labels ...string)
IncVal is used for counters and gauges
func (*RateLatencyLogger) SetVal ¶
func (mgl *RateLatencyLogger) SetVal(value int64, identifier string, labels ...string)
SetVal is used for counters and gauges
type RateLatencyOption ¶
type RateLatencyOption func(rl *RateLatencyLogger)
RateLatencyOption sets a parameter for the RateLatencyLogger
func SetLogger ¶
func SetLogger(logger ILogger) RateLatencyOption
SetLogger sets the output logger. Default is stderr
type ZerologLogger ¶ added in v1.16.0
type ZerologLogger struct {
// contains filtered or unexported fields
}
ZerologLogger is a high-performance logger implementation using zerolog
func NewZerologLogger ¶ added in v1.16.0
func NewZerologLogger(options ...ZerologOption) *ZerologLogger
NewZerologLogger creates a new high-performance logger using zerolog
func (*ZerologLogger) GetLogLevel ¶ added in v1.16.0
func (l *ZerologLogger) GetLogLevel() LogLevels
GetLogLevel returns the current log level
func (*ZerologLogger) LogDebug ¶ added in v1.16.0
func (l *ZerologLogger) LogDebug(str string)
LogDebug logs debug messages
func (*ZerologLogger) LogDebugWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogDebugWithContext(ctx context.Context, str string)
LogDebugWithContext logs debug messages with context
func (*ZerologLogger) LogDebugf ¶ added in v1.16.0
func (l *ZerologLogger) LogDebugf(str string, args ...interface{})
LogDebugf logs formatted debug messages
func (*ZerologLogger) LogDebugfWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogDebugfWithContext(ctx context.Context, str string, args ...interface{})
LogDebugfWithContext logs formatted debug messages with context
func (*ZerologLogger) LogError ¶ added in v1.16.0
func (l *ZerologLogger) LogError(str string, err error)
LogError logs errors and a message along with the error
func (*ZerologLogger) LogErrorInterface ¶ added in v1.16.0
func (l *ZerologLogger) LogErrorInterface(v ...interface{})
LogErrorInterface logs errors with interface{} arguments
func (*ZerologLogger) LogErrorMessage ¶ added in v1.16.0
func (l *ZerologLogger) LogErrorMessage(str string, err error, pairs ...Pair)
LogErrorMessage logs extra fields to the log along with the error
func (*ZerologLogger) LogErrorWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogErrorWithContext(ctx context.Context, str string, err error)
LogErrorWithContext logs errors with context
func (*ZerologLogger) LogErrorWithoutError ¶ added in v1.16.0
func (l *ZerologLogger) LogErrorWithoutError(str string)
LogErrorWithoutError logs only a message without an error
func (*ZerologLogger) LogErrorWithoutErrorf ¶ added in v1.16.0
func (l *ZerologLogger) LogErrorWithoutErrorf(str string, args ...interface{})
LogErrorWithoutErrorf logs only a formatted message without an error
func (*ZerologLogger) LogErrorfWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogErrorfWithContext(ctx context.Context, str string, err error, args ...interface{})
LogErrorfWithContext logs formatted errors with context
func (*ZerologLogger) LogInfo ¶ added in v1.16.0
func (l *ZerologLogger) LogInfo(str string)
LogInfo logs info messages
func (*ZerologLogger) LogInfoMessage ¶ added in v1.16.0
func (l *ZerologLogger) LogInfoMessage(str string, pairs ...Pair)
LogInfoMessage logs extra fields
func (*ZerologLogger) LogInfoWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogInfoWithContext(ctx context.Context, str string)
LogInfoWithContext logs info messages with context
func (*ZerologLogger) LogInfof ¶ added in v1.16.0
func (l *ZerologLogger) LogInfof(str string, args ...interface{})
LogInfof logs formatted info messages
func (*ZerologLogger) LogInfofWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogInfofWithContext(ctx context.Context, str string, args ...interface{})
LogInfofWithContext logs formatted info messages with context
func (*ZerologLogger) LogMessage ¶ added in v1.16.0
func (l *ZerologLogger) LogMessage(message string)
LogMessage logs plain message
func (*ZerologLogger) LogMessageWithExtras ¶ added in v1.16.0
func (l *ZerologLogger) LogMessageWithExtras(message string, level LogLevels, pairs ...Pair)
LogMessageWithExtras logs message with specified level and extra fields
func (*ZerologLogger) LogMessagef ¶ added in v1.16.0
func (l *ZerologLogger) LogMessagef(message string, args ...interface{})
LogMessagef logs formatted plain message
func (*ZerologLogger) LogWarning ¶ added in v1.16.0
func (l *ZerologLogger) LogWarning(str string)
LogWarning logs warning messages
func (*ZerologLogger) LogWarningMessage ¶ added in v1.16.0
func (l *ZerologLogger) LogWarningMessage(str string, pairs ...Pair)
LogWarningMessage logs warning messages along with extra fields
func (*ZerologLogger) LogWarningWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogWarningWithContext(ctx context.Context, str string)
LogWarningWithContext logs warning messages with context
func (*ZerologLogger) LogWarningf ¶ added in v1.16.0
func (l *ZerologLogger) LogWarningf(str string, args ...interface{})
LogWarningf logs formatted warning messages
func (*ZerologLogger) LogWarningfWithContext ¶ added in v1.16.0
func (l *ZerologLogger) LogWarningfWithContext(ctx context.Context, str string, args ...interface{})
LogWarningfWithContext logs formatted warning messages with context
type ZerologOption ¶ added in v1.16.0
type ZerologOption func(l *ZerologLogger)
ZerologOption sets a parameter for the ZerologLogger
func WithConsoleWriter ¶ added in v1.16.0
func WithConsoleWriter() ZerologOption
WithConsoleWriter enables console-friendly output format with human-readable timestamps
func WithFacility ¶ added in v1.16.0
func WithFacility(facility string) ZerologOption
WithFacility sets the log facility for the logger. Default is "ErrorLogger"
func WithJSONConsole ¶ added in v1.16.0
func WithJSONConsole() ZerologOption
WithJSONConsole outputs structured JSON to console (default behavior)
func WithK8sNamespace ¶ added in v1.16.0
func WithK8sNamespace(k8sNamespace string) ZerologOption
WithK8sNamespace sets the k8sNamespace for the logger. Default is "dev" This option will have no effect if env variable K8S_NAMESPACE is set
func WithLogLevel ¶ added in v1.16.0
func WithLogLevel(level string) ZerologOption
WithLogLevel sets the logger level. Possible values are ERROR, WARN, INFO, DEBUG. Default is ERROR
func WithOutput ¶ added in v1.16.0
func WithOutput(writer io.Writer) ZerologOption
WithOutput sets the output writer for the logger
func WithStderr ¶ added in v1.16.0
func WithStderr() ZerologOption
WithStderr outputs to standard error
func WithTimeLogging ¶ added in v1.16.0
func WithTimeLogging(enabled bool) ZerologOption
WithTimeLogging enables logging of time (The use of tic toc functions).