logger

package
v1.1.0-rc7 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: Apache-2.0 Imports: 29 Imported by: 3

Documentation

Index

Constants

View Source
const (
	CEHMAC   ceSetIDType = "hmac" // message signature
	CEUUID   ceSetIDType = "uuid" // completely unique
	CEIncrID ceSetIDType = "incr" // incremental
	CEFuncID ceSetIDType = "func" // set by WithFields or FilterFunc
)

Types of cloudevents id fields

View Source
const (
	CEIDKey           = "id"              // Required - unique per producer
	CESourceKey       = "source"          // Required - URI-reference
	CESpecVersionKey  = "specversion"     // Required - current spec is "1.0"
	CETypeKey         = "type"            // Required - reverse-DNS name prefix
	CEDataContentType = "datacontenttype" // Optional - adheres to RFC2046
	CEDataSchemaKey   = "dataschema"      // Optional - URI-reference
	CESubjectKey      = "subject"         // Optional - possibly pass log level
	CETimeKey         = "time"            // Optional - adheres to RFC3339
	CEDataKey         = "data"            // Optional - no specific format
)

Keys for cloudevents fields, values must be non-empty strings

View Source
const (
	EnvConfig  configType = "env"
	FileConfig configType = "file"
	BothConfig configType = "both"
)

Supported configuration types

View Source
const (
	LogTestInitEnvName = "PRTEST_INIT"
	LogAutoInitEnvName = "PRLOG_AUTOINIT"
	ConfigTypeEnvName  = "PRLOG_CFGTYPE"
	ConfigFileEnvName  = "PRLOG_CFGFILE"
)

Supported auto init/config environment names

View Source
const (
	LogEnvPrefix         = "PRLOG"
	KafkaEnvPrefix       = "PRKAFKA"
	CloudEventsEnvPrefix = "PRCE"
	RotationEnvPrefix    = "PRROT"
)

Supported environment name prefixes

View Source
const (
	ConfigFileName       = "pr_log_config"
	ExportConfigFileName = "pr_export_config.yaml"
)

Default config file name without extension Config file name exported on SIGUSR1

View Source
const (
	RandomPartition     kafkaPartitionType = "random" // default
	HashPartition       kafkaPartitionType = "hash"
	RoundRobinPartition kafkaPartitionType = "roundrobin"
)

Types of kafka partitioning to map to sarama

View Source
const (
	LevelKey          kafkaKeyType = "level" // default
	TimeSecondKey     kafkaKeyType = "second"
	TimeNanoSecondKey kafkaKeyType = "nanosecond"
	FixedKey          kafkaKeyType = "fixed"
	ExtractedKey      kafkaKeyType = "extracted"
	FunctionKey       kafkaKeyType = "function"
)

Types of kafka keys to generate

View Source
const (
	CompressionNone   compressionType = "none" // default
	CompressionGZIP   compressionType = "gzip"
	CompressionSnappy compressionType = "snappy"
	CompressionLZ4    compressionType = "lz4"
	CompressionZSTD   compressionType = "zstd"
)

Types of compression to map to sarama

View Source
const (
	// WaitForNone does not wait for any response
	WaitForNone ackWaitType = "none"
	// WaitForLocal waits for only the local commit to succeed
	WaitForLocal ackWaitType = "local" // default
	// WaitForAll waits for all in-sync replicas to commit
	WaitForAll ackWaitType = "all"
)

Types of ack waiting to map to sarama

View Source
const TopicKey string = "topic"

TopicKey is LogFields key to pass topic through WithFields Example: log.WithFields(LogFields{TopicKey: "mytopic"}).Infof(...)

Variables

View Source
var ErrFatal = errors.New("fatal")
View Source
var ErrNonFatal = errors.New("nonfatal")

Functions

func Debug

func Debug(args ...interface{})

Debug emulates function from go log pkg

func Debugf

func Debugf(format string, args ...interface{})

Debugf emulates function from go log pkg

func Debugln

func Debugln(args ...interface{})

Debugln emulates function from go log pkg

func Error

func Error(args ...interface{})

Error emulates function from go log pkg

func Errorf

func Errorf(format string, args ...interface{})

Errorf emulates function from go log pkg

func Errorln

func Errorln(args ...interface{})

Errorln emulates function from go log pkg

func ExportConfiguration

func ExportConfiguration(file string, config LoggerConfiguration) error

func Fatal

func Fatal(args ...interface{})

Fatal emulates function from go log pkg

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf emulates function from go log pkg

func Fatalln

func Fatalln(args ...interface{})

Fatalln emulates function from go log pkg

func FillConfiguration

func FillConfiguration(defaultCfg interface{}, config interface{},
	cfgType configType, filename string, prefix string) error

FillConfiguration fills config from defaults, config file and environment

func Info

func Info(args ...interface{})

Info emulates function from go log pkg

func Infof

func Infof(format string, args ...interface{})

Infof emulates function from go log pkg

func Infoln

func Infoln(args ...interface{})

Infoln emulates function from go log pkg

func Panic

func Panic(args ...interface{})

Panic emulates function from go log pkg

func Panicf

func Panicf(format string, args ...interface{})

Panicf emulates function from go log pkg

func Panicln

func Panicln(args ...interface{})

Panicln emulates function from go log pkg

func Print

func Print(args ...interface{})

Print emulates function from go log pkg

func Printf

func Printf(format string, args ...interface{})

Printf emulates function from go log pkg

func Println

func Println(args ...interface{})

Println emulates function from go log pkg

func Warn

func Warn(args ...interface{})

Warn emulates function from go log pkg

func Warnf

func Warnf(format string, args ...interface{})

Warnf emulates function from go log pkg

func Warnln

func Warnln(args ...interface{})

Warnln emulates function from go log pkg

Types

type CloudEvents

type CloudEvents struct {
	// contains filtered or unexported fields
}

CloudEvents provides the cloudevents object type

type CloudEventsConfiguration

type CloudEventsConfiguration struct {
	SetID           ceSetIDType
	HMACKey         string
	Source          string
	SpecVersion     string
	Type            string
	SetSubjectLevel bool
}

CloudEventsConfiguration provides cloudevents configuration type

func DefaultCloudEventsCfg

func DefaultCloudEventsCfg() CloudEventsConfiguration

DefaultCloudEventsCfg returns default cloudevents configuration

type ConsoleType

type ConsoleType string

ConsoleType provided to select logger format

const (
	Stdout ConsoleType = "stdout" // default
	Stderr ConsoleType = "stderr"
)

Types of logger formats

type FilterFunc

type FilterFunc func(*map[string]interface{})

FilterFunc func to add/modify/remove message map entries and return kafka key

type FormatType

type FormatType string

FormatType provided to select logger format

const (
	JSONFormat FormatType = "json"
	TextFormat FormatType = "text" // default
	CEFormat   FormatType = "cloudevents"
)

Types of logger formats

type KafkaProducer

type KafkaProducer struct {
	// contains filtered or unexported fields
}

KafkaProducer wraps sarama producer with config

type KeyFunc

type KeyFunc func(*map[string]interface{}) string

KeyFunc func to return key calculated from kafka message contents

type LevelType

type LevelType string

LevelType provided to select log level

const (
	DebugType LevelType = "debug"
	InfoType  LevelType = "info" // default
	WarnType  LevelType = "warn"
	ErrorType LevelType = "error"
	FatalType LevelType = "fatal"
	PanicType LevelType = "panic"
)

Supported log levels

type LogFields

type LogFields map[string]interface{}

LogFields provided for calls to WithFields for structured logging

type Logger

type Logger interface {
	Print(args ...interface{})

	Printf(format string, args ...interface{})

	Println(args ...interface{})

	Debug(args ...interface{})

	Debugf(format string, args ...interface{})

	Debugln(args ...interface{})

	Info(args ...interface{})

	Infof(format string, args ...interface{})

	Infoln(args ...interface{})

	Warn(args ...interface{})

	Warnf(format string, args ...interface{})

	Warnln(args ...interface{})

	Error(args ...interface{})

	Errorf(format string, args ...interface{})

	Errorln(args ...interface{})

	Fatal(args ...interface{})

	Fatalf(format string, args ...interface{})

	Fatalln(args ...interface{})

	Panic(args ...interface{})

	Panicf(format string, args ...interface{})

	Panicln(args ...interface{})

	WithFields(keyValues LogFields) Logger

	WithKafkaFilterFn(filter FilterFunc) Logger

	WithKafkaKeyFn(filter KeyFunc) Logger
}

Logger is the contract for the logger interface

func NewLogger

func NewLogger(config LoggerConfiguration) (Logger, error)

NewLogger returns a Logger instance

type LoggerConfiguration

type LoggerConfiguration struct {
	LogPackage        PackageType
	LogLevel          LevelType
	EnableTimeStamps  bool
	EnableColorLevels bool
	EnableCloudEvents bool
	CloudEventsCfg    CloudEventsConfiguration
	EnableKafka       bool
	KafkaFormat       FormatType
	KafkaProducerCfg  ProducerConfiguration
	EnableConsole     bool
	ConsoleFormat     FormatType
	ConsoleWriter     ConsoleType
	EnableFile        bool
	FileFormat        FormatType
	FileLocation      string
	EnableRotation    bool
	RotationCfg       RotationConfiguration
	EnableDebug       bool
}

LoggerConfiguration stores the config for the logger

func DefaultCompleteCfg

func DefaultCompleteCfg() *LoggerConfiguration

DefaultLoggerCfg returns default log configuration

func DefaultLoggerCfg

func DefaultLoggerCfg() LoggerConfiguration

DefaultLoggerCfg returns default log configuration

func GetLoggerConfiguration

func GetLoggerConfiguration(cfgType configType,
	cfgFileName string) (LoggerConfiguration, error)

GetLoggerConfiguration generates config from defaults/config-file/environment

type LogrusConsoleHook

type LogrusConsoleHook struct {
	// contains filtered or unexported fields
}

LogrusConsoleHook provides a console hook

func (*LogrusConsoleHook) Fire

func (h *LogrusConsoleHook) Fire(entry *logrus.Entry) error

Fire writes the log message exactly the same as logrus console

func (*LogrusConsoleHook) Levels

func (h *LogrusConsoleHook) Levels() []logrus.Level

Levels returns all log levels that are enabled

type LogrusDebugHook

type LogrusDebugHook struct{}

LogrusDebugHook provides a debug hook

func (*LogrusDebugHook) Fire

func (h *LogrusDebugHook) Fire(entry *logrus.Entry) error

Fire prints the entry

func (*LogrusDebugHook) Levels

func (h *LogrusDebugHook) Levels() []logrus.Level

Levels returns all log levels that are enabled

type LogrusKafkaHook

type LogrusKafkaHook struct {
	// contains filtered or unexported fields
}

LogrusKafkaHook provides a kafka producer hook

func (*LogrusKafkaHook) Fire

func (h *LogrusKafkaHook) Fire(entry *logrus.Entry) error

Fire writes the entry as a message on Kafka

func (*LogrusKafkaHook) Levels

func (h *LogrusKafkaHook) Levels() []logrus.Level

Levels returns all log levels that are enabled

type PackageType

type PackageType string

PackageType provided to select underlying log package

const (
	ZapType    PackageType = "zap"
	LogrusType PackageType = "logrus"
)

Supported log packages

type ProducerConfiguration

type ProducerConfiguration struct {
	Brokers       []string
	Topic         string
	Partition     kafkaPartitionType
	Key           kafkaKeyType
	KeyName       string
	Compression   compressionType
	AckWait       ackWaitType
	ProdFlushFreq time.Duration
	ProdRetryMax  int
	ProdRetryFreq time.Duration
	MetaRetryMax  int
	MetaRetryFreq time.Duration
	EnableTLS     bool
	TLSCfg        *tls.Config
	EnableDebug   bool
	// contains filtered or unexported fields
}

ProducerConfiguration provides kafka producer configuration type

func DefaultProducerCfg

func DefaultProducerCfg() ProducerConfiguration

DefaultProducerCfg returns default kafka configuration

type RotationConfiguration

type RotationConfiguration struct {
	MaxSize    int
	MaxAge     int
	MaxBackups int
	LocalTime  bool
	Compress   bool
}

RotationConfiguration stores the config for log rotation

func DefaultRotationCfg

func DefaultRotationCfg() RotationConfiguration

DefaultRotationCfg returns default cloudevents configuration

type ZapKafkaWriter

type ZapKafkaWriter struct {
	// contains filtered or unexported fields
}

ZapKafkaWriter is a zap WriteSyncer (io.Writer) that writes messages to Kafka

func (*ZapKafkaWriter) Close

func (zw *ZapKafkaWriter) Close() (err error)

Close must be called when the writer is no longer needed (Thread-safe)

func (*ZapKafkaWriter) Closed

func (zw *ZapKafkaWriter) Closed() bool

Closed returns true if the writer is closed, false otherwise (Thread-safe)

func (*ZapKafkaWriter) Sync

func (zw *ZapKafkaWriter) Sync() error

Sync satisfies zapcore.WriteSyncer interface, zapcore.AddSync works as well

func (*ZapKafkaWriter) Write

func (zw *ZapKafkaWriter) Write(msg []byte) (int, error)

Write sends byte slices to Kafka ignoring error responses (Thread-safe) Write might block if the Input() channel of the AsyncProducer is full

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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