zaputil

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: Apache-2.0 Imports: 6 Imported by: 8

Documentation

Overview

Package zaputil provides some helpful functions for consistently setting up a zap logger, allowing for command-line configuration and some preferences we've developed. It is built to be easily adjusted and expanded if there are any additional preferences on specific projects.

The `go-log-utils` repository is part of the `open-cluster-management` community. For more information, visit: [open-cluster-management.io](https://open-cluster-management.io).

This example setup allows for command-line configuration, easy setup of the controller-runtime logger, and will make klog messages (from other packages your project might use) appear in the same format as your zap logs:

import (
	"flag"
	"fmt"

	ctrl "sigs.k8s.io/controller-runtime"
	"k8s.io/klog/v2"
	"github.com/go-logr/zapr"
	"github.com/stolostron/go-log-utils/zaputil"
)

func main() {
	zflags := zaputil.NewFlagConfig()
	zflags.Bind(flag.CommandLine)
	klog.InitFlags(flag.CommandLine)

	// ... define your custom flags here...

	flag.Parse()

	ctrlZap, err := zflags.BuildForCtrl()
	if err != nil {
		panic(fmt.Sprintf("Failed to build zap logger for controller: %v", err))
	}

	ctrl.SetLogger(zapr.NewLogger(ctrlZap))
	setupLog := ctrl.Log.WithName("setup")

	klogZap, err := zaputil.BuildForKlog(zflags.GetConfig(), flag.CommandLine)
	if err != nil {
		setupLog.Error(err, "Failed to build zap logger for klog, those logs will not go through zap")
	} else {
		klog.SetLogger(zapr.NewLogger(klogZap).WithName("klog"))
	}

	// ... the rest of your main function ...
}

Note: for consistent log annotations with the filename, line number, and function name, call the `WithName` or `WithValues` method in your function - do not just use a global variable for the logger directly, or the "caller" value will be off by one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildForKlog

func BuildForKlog(cfg zap.Config, klogFlagSet *flag.FlagSet) (*zap.Logger, error)

BuildForKlog returns a zap.Logger built from given config, made to work well with klog. The FlagSet should be the same one klog is bound to. Usually the given config should be created from `FlagConfig.GetConfig()`.

func SyncWithGlogFlags

func SyncWithGlogFlags(klogFlagSet *flag.FlagSet) error

SyncWithGlogFlags copies the values of all the flags on the command line with the given FlagSet. It is intended to help workaround problems with interoperability between glog and klog. See https://github.com/kubernetes/klog/blob/v2.40.1/examples/coexist_glog/coexist_glog.go#L18

Types

type FlagConfig

type FlagConfig struct {
	// LevelName specifies the flag to use for the zap level.
	LevelName string
	// EncoderName specifies the flag to use for the zap encoding.
	EncoderName string
	// contains filtered or unexported fields
}

A FlagConfig enables changing the command flags what will be used for zap configuration.

func NewFlagConfig

func NewFlagConfig() FlagConfig

NewFlagConfig returns a FlagConfig with the default flag names.

func (*FlagConfig) Bind

func (fc *FlagConfig) Bind(fs *flag.FlagSet)

Bind will register the flags on the flagset so they can be parsed. If not set, the level will default to 'info', and the encoding will default to 'console'.

func (*FlagConfig) BuildForCtrl

func (fc *FlagConfig) BuildForCtrl() (*zap.Logger, error)

BuildForCtrl returns a zap.Logger built to work well with the controller-runtime. Note: using the controller-runtime logger directly will not record the correct filename/line - instead, call `WithName` or `WithValues` in your function, and use the resulting logger.

func (*FlagConfig) GetConfig

func (fc *FlagConfig) GetConfig() zap.Config

GetConfig returns a Zap configuration based off of the "production" configuration from zap. It will have the level and encoder specified in the command flags, and it will use ISO-8601 timestamps. Note that it is configured with sampling, so it could drop some logs. See https://github.com/uber-go/zap/blob/master/FAQ.md#why-sample-application-logs

Jump to

Keyboard shortcuts

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