klog

package module
v0.0.0-...-67cb54a Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

klog wrapper of zap

inspired by istio/klog.

usage

import

import this package by github.com/xial-thu/klog

initialization

for example, in main.go

// Unchanged
klog.InitFlags(nil)
flag.StringVar(&kubeconfig, "kubeconfig", "", "path to Kubernetes config file")
flag.Parse()

// Append a new API
klog.Singleton()

If Singleton() is not called, the default global no-ops logger will work, which means you are not able to see any real log.

Due to some gaps between klog and zap, parameters shall be converted, and the conversion must be done after flag.Parse(). klog.Singleton() inits an unique global logger whose configuration is slightly different from default zap production configuration at:

  1. Timekey is set to "time"
  2. EncodeTime is set to ISO8601TimeEncoder
flags

Not all flags defined in klog is supported, or rather say, not all the flags still make sense. Only alsologtostderr and v is supported currently.

  • v: still supports klog.V(2).Info() syntax. But the level of klog.Info() is INFO; that klog.V(3).Info() is DEBUG. If v is set to zero, zap DEBUG log will be ignored. Default to 0
  • alsologtostderr: default to true. If set to false, INFO and DEBUG log will only output to stdout
structured logging

There're 3 APIs:

  • With(): parse each field and value from input. WithFields(struct{A string}{"hi"}) will output "A":"hi". If you care the fields in your struct and hope to extract them, use With()
  • WithAll(): sugar of zap.Any(). e.g. WithFields(struct{A string}{"hi"}) will output "":{"A":"hi"}. If you want to record the name of your struct, use WithAll()
  • WithFields(): e.g. WithFields("ID", 1, "name": "hi"), just another sugar of sugar.With()

Tips of With():

  1. Only struct or map will be accepted
  2. If arg is a struct, only exported field(which means "FieldName", not "fieldname") will be logged
  3. If arg is a map, only accept maps whose key is string
  4. If you have nested structs or maps, consider spliting them into several anomony parts
  5. If you don't want some fields to be logged automatically, unexport them

Some examples of With():

type S struct {
	A int
	B string
}
type Q struct {
	D ID
}
s := S{
	A: 10,
	B: "abc",
}
q := Q{
	D: ID(1),
}
c := ""
    
// struct args
With(s).Info(c) // "A":10,"B":"abc"
With(s, q).Info(c) // "A":10,"B":"abc","D":1
// anomony
With(struct {
	A int
	B int
}{1, 2}).Info(c) // "A":1,"B":2
// map args
With(map[string]int{"A":1}).Info(c) // "A":1
performance of With()
case ns/op B/op allocs/op
With every time 3121 2884 20
WithField every time 2239 1568 9
WithAll 3215 2642 14
With once 573 7 1

Due to reflect, With() is slow. It indicates us that it's better to write like this instead of parsing interfaces every time.

newLogger := klog.With(something)
newLogger.Info(something)

limitation

  1. default field is empty.
  2. structured logging still needs modification in business logic.
  3. support of flags needs more work.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(args ...interface{})

Error is a shim

func ErrorDepth

func ErrorDepth(depth int, args ...interface{})

ErrorDepth is a shim

func Errorf

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

Errorf is a shim

func Errorln

func Errorln(args ...interface{})

Errorln is a shim

func Exit

func Exit(args ...interface{})

Exit is a shim

func ExitDepth

func ExitDepth(depth int, args ...interface{})

ExitDepth is a shim

func Exitf

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

Exitf is a shim

func Exitln

func Exitln(args ...interface{})

Exitln is a shim

func Fatal

func Fatal(args ...interface{})

Fatal is a shim

func FatalDepth

func FatalDepth(depth int, args ...interface{})

FatalDepth is a shim

func Fatalf

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

Fatalf is a shim

func Fatalln

func Fatalln(args ...interface{})

Fatalln is a shim

func Flush

func Flush()

Flush is a shim

func Info

func Info(args ...interface{})

Info is a shim

func InfoDepth

func InfoDepth(depth int, args ...interface{})

InfoDepth is a shim

func Infof

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

Infof is a shim

func Infoln

func Infoln(args ...interface{})

Infoln is a shim

func InitFlags

func InitFlags(flagset *pflag.FlagSet)

InitFlags is a shim, only accepts

func SetLevel

func SetLevel(v Level)

SetLevel updates level on the fly

func Warning

func Warning(args ...interface{})

Warning is a shim

func WarningDepth

func WarningDepth(depth int, args ...interface{})

WarningDepth is a shim

func Warningf

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

Warningf is a shim

func Warningln

func Warningln(args ...interface{})

Warningln is a shim

Types

type Config

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

Config is the mixture of zap config and klog config

type Klogger

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

Klogger wraps a sugarlogger

func Singleton

func Singleton() *Klogger

Singleton inits an unique logger

func With

func With(args ...interface{}) *Klogger

With fills k-v of a struct into a logger, however it's relatively slow

func WithAll

func WithAll(args ...interface{}) *Klogger

WithAll fills each arg directly without parsing fields and values Only valid for exported fields

func WithFields

func WithFields(args ...interface{}) *Klogger

WithFields requires user to fill in k-v pairs

func (*Klogger) Error

func (k *Klogger) Error(args ...interface{})

Error is a shim

func (*Klogger) ErrorDepth

func (k *Klogger) ErrorDepth(depth int, args ...interface{})

ErrorDepth is a shim

func (*Klogger) Errorf

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

Errorf is a shim

func (*Klogger) Errorln

func (k *Klogger) Errorln(args ...interface{})

Errorln is a shim

func (*Klogger) Exit

func (k *Klogger) Exit(args ...interface{})

Exit is a shim

func (*Klogger) ExitDepth

func (k *Klogger) ExitDepth(depth int, args ...interface{})

ExitDepth is a shim

func (*Klogger) Exitf

func (k *Klogger) Exitf(format string, args ...interface{})

Exitf is a shim

func (*Klogger) Exitln

func (k *Klogger) Exitln(args ...interface{})

Exitln is a shim

func (*Klogger) Fatal

func (k *Klogger) Fatal(args ...interface{})

Fatal is a shim

func (*Klogger) FatalDepth

func (k *Klogger) FatalDepth(depth int, args ...interface{})

FatalDepth is a shim

func (*Klogger) Fatalf

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

Fatalf is a shim

func (*Klogger) Fatalln

func (k *Klogger) Fatalln(args ...interface{})

Fatalln is a shim

func (*Klogger) Info

func (k *Klogger) Info(args ...interface{})

Info is a shim

func (*Klogger) InfoDepth

func (k *Klogger) InfoDepth(depth int, args ...interface{})

InfoDepth is a shim

func (*Klogger) Infof

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

Infof is a shim

func (*Klogger) Infoln

func (k *Klogger) Infoln(args ...interface{})

Infoln is a shim

func (*Klogger) SetLevel

func (k *Klogger) SetLevel(v Level)

SetLevel updates level on the fly

func (*Klogger) V

func (k *Klogger) V(level Level) Verbose

V is a shim

func (*Klogger) Warning

func (k *Klogger) Warning(args ...interface{})

Warning is a shim

func (*Klogger) WarningDepth

func (k *Klogger) WarningDepth(depth int, args ...interface{})

WarningDepth is a shim

func (*Klogger) Warningf

func (k *Klogger) Warningf(format string, args ...interface{})

Warningf is a shim

func (*Klogger) Warningln

func (k *Klogger) Warningln(args ...interface{})

Warningln is a shim

func (*Klogger) With

func (k *Klogger) With(args ...interface{}) *Klogger

With fills k-v of a struct into a logger, however it's relatively slow Only struct and map will be accepted:

  • struct: only exported field will be added
  • map: only accept string type as key

func (*Klogger) WithAll

func (k *Klogger) WithAll(args ...interface{}) *Klogger

WithAll fills each arg directly without parsing fields and values Only valid for exported fields

func (*Klogger) WithFields

func (k *Klogger) WithFields(args ...interface{}) *Klogger

WithFields requires user to fill in k-v pairs

type Level

type Level int32

Level is a shim

const (
	// MinLevel 0: default level, forbids DEBUG log
	MinLevel Level = iota

	// MaxLevel 4: max level V(4)
	MaxLevel
)

type Verbose

type Verbose bool

Verbose is a shim

func V

func V(level Level) Verbose

V is a shim

func (Verbose) Info

func (v Verbose) Info(args ...interface{})

Info is a shim

func (Verbose) Infof

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

Infof is a shim

func (Verbose) Infoln

func (v Verbose) Infoln(args ...interface{})

Infoln is a shim

Jump to

Keyboard shortcuts

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