Documentation ¶
Overview ¶
Package klog provides logging to stdout and kafka by wrapping zerolog and sarama via zerolog.LevelWriter. Every log event of klog.Logger will be published to kafka. The logger will automatically fall back to stdout logger if kafka is unreachable.
This package implements all zerolog functionalities that are documented here: https://github.com/rs/zerolog
Basic Usage:
log := klog.Logger{ Application: "kafka-logging-test", Environment: "development", KafkaBrokers: []string{"kafka-bootstrap.svc.cluster.local:9094"}, KafkaTopic: "kafka-logging-test", KafkaProtocolVersion: "2.7.0", }
// It will not error on kafka error. So every error here should terminate the program because there is a // problem with the stdout logger.
if err := log.Init(); err != nil { panic(err) }
log.Info().
Msg("application started")
log.Warn().
Msg("an instance failed to start")
err := fmt.Errorf("hard moods in package")
log.Err(err).
Msg("package moods failed")
Index ¶
- Variables
- type KafkaWriter
- type Logger
- func (l *Logger) Debug() *zerolog.Event
- func (l *Logger) Err(err error) *zerolog.Event
- func (l *Logger) Error() *zerolog.Event
- func (l *Logger) Fatal() *zerolog.Event
- func (l *Logger) Info() *zerolog.Event
- func (l *Logger) Init() error
- func (l *Logger) Log() *zerolog.Event
- func (l *Logger) Panic() *zerolog.Event
- func (l *Logger) Warn() *zerolog.Event
Constants ¶
This section is empty.
Variables ¶
var (
ErrSaramaConfigNil = errors.New("SaramaConfig is nil")
)
Functions ¶
This section is empty.
Types ¶
type KafkaWriter ¶
type KafkaWriter interface { zerolog.LevelWriter io.Closer }
KafkaWriter implements zerolog.LevelWriter interface
type Logger ¶
type Logger struct { // Application keyword which is added to the logger as string field // // Example: "payment-gateway" Application string // Environment keyword which is added to the logger as string field // // Examples: local, development, staging, production Environment string // Brokers list of kafka brokers // // Example: kafka1:9094, kafka2:9094, kafka3:9094, kafka-bootstrap:9094 KafkaBrokers []string // KafkaProtocolVersion to configure "github.com/Shopify/sarama" KafkaProtocolVersion string // KafkaTopic where to produce log messages. KafkaTopic string // SaramaConfig holds the configuration of Sarama. // It will be set by invoking Init if it's nil. // SaramaConfig *sarama.Config // AdditionalWriters that will be attached to the logger AdditionalWriters []zerolog.LevelWriter // Level sets the minimum logging level of zerolog // // Valid Options: TRACE, DEBUG, INFO, WARNING, ERROR Level string // Caller functions will be added as additional field to the global logger if true // // Note that With().Caller() can be added to every event of choice Caller bool // contains filtered or unexported fields }
Logger holds the log instance
func (*Logger) Debug ¶
Debug starts a new message with debug level.
You must call Msg on the returned event in order to send the event.
func (*Logger) Err ¶
Err starts a new message with error level with err as a field if not nil or with info level if err is nil.
You must call Msg on the returned event in order to send the event.
func (*Logger) Error ¶
Error starts a new message with error level.
You must call Msg on the returned event in order to send the event.
func (*Logger) Fatal ¶
Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.
You must call Msg on the returned event in order to send the event.
func (*Logger) Info ¶
Info starts a new message with info level.
You must call Msg on the returned event in order to send the event.
func (*Logger) Init ¶
Init will configure the Logger instance. If kafka producer can not be started the logger will fall back to stdout logger.
func (*Logger) Log ¶
Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.
You must call Msg on the returned event in order to send the event.