logger

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: MPL-2.0 Imports: 3 Imported by: 2

README

Logger

  • a wrapper over uber/zap logger
  • There are 4 levels that can be used; info, error, debug, and warning.

Usage

It's usually not ideal to pass logger instance across you application, this can be seen as unnecessary pollution. That's why global logger is provided by default with Info as its default log level.

import "github.com/weaveworks/policy-agent/pkg/logger"

logger.Info("this is a log")
Change log level

To change the log level of the global logger you can simply use the Config function

import "github.com/weaveworks/policy-agent/pkg/logger"

logger.Config(logger.DebugLevel)
logger.Debug("this is a debug log")
Create Custom logger

sometimes a custom logger is ideal. For example creating a logger that have request-id on all the logs. you can create custom logger by using With or New function

import "github.com/weaveworks/policy-agent/pkg/logger"
// using with will have the global logger log level
customLogger := logger.With("requestId", reqID)
customLogger.Info("this is a debug log")

l := logger.New(logger.DebugLevel)
l.Debug("this is a debug log")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Config

func Config(level Level)

Config sets configurations for global logger

func ConfigWriterSync

func ConfigWriterSync(level Level, w zapcore.WriteSyncer)

ConfigWriterSync sets configurations for global logger

func DPanic

func DPanic(args ...interface{})

DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicf

func DPanicf(template string, args ...interface{})

DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicw

func DPanicw(msg string, keysAndValues ...interface{})

DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.

func Debug

func Debug(args ...interface{})

Debug uses fmt.Sprint to construct and log a message.

func Debugf

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

Debugf uses fmt.Sprintf to log a templated message.

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

When debug-level logging is disabled, this is much faster than

s.With(keysAndValues).Debug(msg)

func Error

func Error(args ...interface{})

Error uses fmt.Sprint to construct and log a message.

func Errorf

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

Errorf uses fmt.Sprintf to log a templated message.

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Fatal

func Fatal(args ...interface{})

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func Fatalf

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

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.

func Info

func Info(args ...interface{})

Info uses fmt.Sprint to construct and log a message.

func Infof

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

Infof uses fmt.Sprintf to log a templated message.

func Infow

func Infow(msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func NewZapLogger

func NewZapLogger(level Level) *zap.Logger

func Panic

func Panic(args ...interface{})

Panic uses fmt.Sprint to construct and log a message, then panics.

func Panicf

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

Panicf uses fmt.Sprintf to log a templated message, then panics.

func Panicw

func Panicw(msg string, keysAndValues ...interface{})

Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.

func Print

func Print(args ...interface{})

Print uses fmt.Sprint to construct and log a message.

func Printf

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

Infof uses fmt.Sprintf to log a templated message.

func Println

func Println(args ...interface{})

Println uses fmt.Sprint to construct and log a message.

func Sync

func Sync() error

Sync flushes any buffered log entries.

func Warn

func Warn(args ...interface{})

Warn uses fmt.Sprint to construct and log a message.

func Warnf

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

Warnf uses fmt.Sprintf to log a templated message.

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func WithGlobal

func WithGlobal(args ...interface{})

WithGlobal adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.

For example,

 logger.With(
   "hello", "world",
   "failure", errors.New("oh no"),
   Stack(),
   "count", 42,
   "user", User{Name: "alice"},
)

Note that the keys in key-value pairs should be strings. If you pass a non-string key panics a separate error is logged, but the key-value pair is skipped and execution continues. Passing an orphaned key triggers similar behavior

Types

type Level

type Level int8
const (
	DebugLevel Level = iota - 1
	InfoLevel
	WarnLevel
	ErrorLevel
)

type Logger

type Logger = *zap.SugaredLogger

func New

func New(level Level) Logger

func With

func With(args ...interface{}) Logger

With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.

For example,

 logger.With(
   "hello", "world",
   "failure", errors.New("oh no"),
   Stack(),
   "count", 42,
   "user", User{Name: "alice"},
)

Note that the keys in key-value pairs should be strings. If you pass a non-string key panics a separate error is logged, but the key-value pair is skipped and execution continues. Passing an orphaned key triggers similar behavior

Jump to

Keyboard shortcuts

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